def _addimage(self, ctx: commands.Context, category: str,
image_url: str=None):
"""Adds a new image to the specified category."""
await self.bot.type()
server = ctx.message.server
if server.id not in os.listdir(self.base):
self.settings[server.id] = default_settings
dataIO.save_json(self.settings_path, self.settings)
os.makedirs(os.path.join(self.base, server.id))
if category not in self._list_image_dirs(server.id):
await self.bot.reply(cf.error("Category not found."))
return
attach = ctx.message.attachments
if len(attach) > 1 or (attach and image_url):
await self.bot.reply(cf.error("Please only provide one file."))
return
url = ""
filename = ""
if attach:
a = attach[0]
url = a["url"]
filename = a["filename"]
elif image_url:
url = image_url
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 an image."))
return
new_id = str(self.settings[server.id]["next_ids"][category])
self.settings[server.id]["next_ids"][category] += 1
dataIO.save_json(self.settings_path, self.settings)
ext = os.path.splitext(filename)[1]
filepath = os.path.join(
self.base, server.id, category, "{}_{}.{}".format(
category, new_id, ext))
async with aiohttp.get(url) as new_sound:
f = open(filepath, "wb")
f.write(await new_sound.read())
f.close()
await self.bot.reply(cf.info("Image added."))
评论列表
文章目录