Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
cf.error("Please only add one sound at a time."))
return
url = ""
filename = ""
if attach:
a = attach[0]
url = a["url"]
filename = a["filename"]
elif link:
url = "".join(link)
filename = os.path.basename(
"_".join(url.split()).replace("%20", "_"))
else:
await self.bot.reply(
cf.error("You must provide either a Discord attachment or a"
" direct link to a sound."))
return
filepath = os.path.join(self.sound_base, server.id, filename)
if os.path.splitext(filename)[0] in self.list_sounds(server.id):
await self.bot.reply(
cf.error("A sound with that filename already exists."
" Please change the filename and try again."))
return
async with aiohttp.get(url) as new_sound:
f = open(filepath, "wb")
f.write(await new_sound.read())
f.close()
filename = a["filename"]
elif link:
url = "".join(link)
filename = os.path.basename(
"_".join(url.split()).replace("%20", "_"))
else:
await self.bot.reply(
cf.error("You must provide either a Discord attachment or a"
" direct link to a sound."))
return
filepath = os.path.join(self.sound_base, server.id, filename)
if os.path.splitext(filename)[0] in self.list_sounds(server.id):
await self.bot.reply(
cf.error("A sound with that filename already exists."
" Please change the filename and try again."))
return
async with aiohttp.get(url) as new_sound:
f = open(filepath, "wb")
f.write(await new_sound.read())
f.close()
self.settings[server.id][
os.path.splitext(filename)[0]] = {"volume": default_volume}
dataIO.save_json(self.settings_path, self.settings)
await self.bot.reply(
cf.info("Sound {} added.".format(os.path.splitext(filename)[0])))
await self.bot.type()
server = ctx.message.server
if server.id not in os.listdir(self.sound_base):
os.makedirs(os.path.join(self.sound_base, server.id))
if server.id not in self.settings:
self.settings[server.id] = {}
dataIO.save_json(self.settings_path, self.settings)
attach = ctx.message.attachments
if len(attach) > 1 or (attach and link):
await self.bot.reply(
cf.error("Please only add one sound at a time."))
return
url = ""
filename = ""
if attach:
a = attach[0]
url = a["url"]
filename = a["filename"]
elif link:
url = "".join(link)
filename = os.path.basename(
"_".join(url.split()).replace("%20", "_"))
else:
await self.bot.reply(
cf.error("You must provide either a Discord attachment or a"
" direct link to a sound."))
if server.id not in os.listdir(self.sound_base):
os.makedirs(os.path.join(self.sound_base, server.id))
if server.id not in self.settings:
self.settings[server.id] = {}
dataIO.save_json(self.settings_path, self.settings)
f = glob.glob(os.path.join(
self.sound_base, server.id, soundname + ".*"))
if len(f) < 1:
await self.bot.reply(cf.error(
"Sound file not found. Try `{}allsounds` for a list.".format(
ctx.prefix)))
return
elif len(f) > 1:
await self.bot.reply(cf.error(
"There are {} sound files with the same name, but different"
" extensions, and I can't deal with it. Please make filenames"
" (excluding extensions) unique.".format(len(f))))
return
soundname = os.path.splitext(os.path.basename(f[0]))[0]
if soundname in self.settings[server.id]:
if "volume" in self.settings[server.id][soundname]:
vol = self.settings[server.id][soundname]["volume"]
else:
vol = default_volume
self.settings[server.id][soundname]["volume"] = vol
dataIO.save_json(self.settings_path, self.settings)
else:
vol = default_volume
self.settings[server.id][soundname] = {"volume": vol}
server = ctx.message.server
if server.id not in os.listdir(self.sound_base):
os.makedirs(os.path.join(self.sound_base, server.id))
f = glob.glob(os.path.join(self.sound_base, server.id,
soundname + ".*"))
if len(f) < 1:
await self.bot.say(cf.error(
"Sound file not found. Try `{}allsounds` for a list.".format(
ctx.prefix)))
return
elif len(f) > 1:
await self.bot.say(cf.error(
"There are {} sound files with the same name, but different"
" extensions, and I can't deal with it. Please make filenames"
" (excluding extensions) unique.".format(len(f))))
return
await self.bot.upload(f[0])
async def _playsound(self, ctx: commands.Context, soundname: str):
"""Plays the specified sound."""
server = ctx.message.server
if server.id not in os.listdir(self.sound_base):
os.makedirs(os.path.join(self.sound_base, server.id))
if server.id not in self.settings:
self.settings[server.id] = {}
dataIO.save_json(self.settings_path, self.settings)
f = glob.glob(os.path.join(
self.sound_base, server.id, soundname + ".*"))
if len(f) < 1:
await self.bot.reply(cf.error(
"Sound file not found. Try `{}allsounds` for a list.".format(
ctx.prefix)))
return
elif len(f) > 1:
await self.bot.reply(cf.error(
"There are {} sound files with the same name, but different"
" extensions, and I can't deal with it. Please make filenames"
" (excluding extensions) unique.".format(len(f))))
return
soundname = os.path.splitext(os.path.basename(f[0]))[0]
if soundname in self.settings[server.id]:
if "volume" in self.settings[server.id][soundname]:
vol = self.settings[server.id][soundname]["volume"]
else:
vol = default_volume