From 7d736f76c44056001cb075f02730f700ab6ab2e7 Mon Sep 17 00:00:00 2001 From: klemek Date: Mon, 13 Apr 2020 16:22:08 +0200 Subject: [PATCH 1/2] added more info to templates --- discord_bot/__main__.py | 11 ++++++++--- meme_otron/meme_db.py | 1 + meme_otron/types.py | 22 +++++++--------------- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/discord_bot/__main__.py b/discord_bot/__main__.py index d5ce27b..6ed617c 100644 --- a/discord_bot/__main__.py +++ b/discord_bot/__main__.py @@ -83,8 +83,8 @@ async def on_message(message): return async with message.channel.typing(): left_wmark_text = None - if not direct and len(args) > 1: - f"By {message.author.display_name}" + if len(args) > 1 and message.author.display_name is not None: + left_wmark_text = f"By {message.author.display_name}" img = meme_otron.compute(*args, left_wmark_text=left_wmark_text) if img is None: await message.channel.send(f"Template `{args[0]}` not found\n" @@ -95,7 +95,12 @@ async def on_message(message): img.save(output, format="JPEG") response = None if len(args) == 1: - response = f"Template `{args[0]}`:" + meme = db.get_meme(args[0]) + response = f"Template `{meme.id}`:" + if len(meme.aliases) > 0: + response += f"\n- Aliases: `{'`, `'.join(meme.aliases)}`" + if meme.info is not None: + response += f"\n- More info: <{meme.info}>" elif not direct: response = f"A meme by {message.author.mention}:" await message.channel.send(response, diff --git a/meme_otron/meme_db.py b/meme_otron/meme_db.py index f3a8839..0003cd0 100644 --- a/meme_otron/meme_db.py +++ b/meme_otron/meme_db.py @@ -57,6 +57,7 @@ def load_item(i, item): meme = Meme(item_id) meme.abstract = utils.read_key_safe(item, "abstract", False, types=[bool]) meme.aliases = utils.read_key_safe(item, "aliases", [], types=[str], is_list=True) + meme.info = utils.read_key_safe(item, "info", types=[str]) meme.text_base = load_text(0, item, meme.text_base) if not meme.abstract: meme.template = utils.read_key(item, "template", meme.template, types=[str]) diff --git a/meme_otron/types.py b/meme_otron/types.py index f3f94ae..abd1b85 100644 --- a/meme_otron/types.py +++ b/meme_otron/types.py @@ -25,22 +25,14 @@ class Meme: TODO """ - def __init__(self, meme_id, aliases=None, abstract=False, template=None, text_base=None, texts=None): + def __init__(self, meme_id): self.id = meme_id - if aliases is None: - self.aliases = [] - else: - self.aliases = aliases - self.abstract = abstract - self.template = template - if text_base is None: - self.text_base = Text() - else: - self.text_base = text_base - if texts is None: - self.texts = None - else: - self.texts = copy.deepcopy(texts) + self.aliases = [] + self.abstract = None + self.info = None + self.template = None + self.text_base = Text() + self.texts = None def clone(self): return copy.deepcopy(self) From 91bf80adc7de630faad0b351591e0fda5fea7903 Mon Sep 17 00:00:00 2001 From: klemek Date: Mon, 13 Apr 2020 21:04:20 +0200 Subject: [PATCH 2/2] deleting message on command --- discord_bot/__main__.py | 50 ++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/discord_bot/__main__.py b/discord_bot/__main__.py index 6ed617c..1e5b084 100644 --- a/discord_bot/__main__.py +++ b/discord_bot/__main__.py @@ -30,6 +30,8 @@ db.load_memes() client = discord.Client() +SENT = {} + def debug(message, txt): """ @@ -57,6 +59,23 @@ async def on_ready(): logging.info(f'- {guild.name}(id: {guild.id})') +async def delete(message): + """ + TODO + + :param (discord.Message) message: + :rtype: bool + """ + try: + await message.delete() + return True + except discord.Forbidden: + pass + except discord.NotFound: + pass + return False + + @client.event async def on_message(message): """ @@ -67,7 +86,14 @@ async def on_message(message): # Ignore self messages if message.author == client.user: return + direct = message.channel.type == discord.ChannelType.private + + if not direct: + mid = f'{message.guild.id}/{message.channel.id}/{message.author.id}' + else: + mid = message.author.id + if direct or client.user in message.mentions: message.content = re.sub(r'<@[^>]+>', '', message.content).strip() args = utils.parse_arguments(message.content) @@ -81,13 +107,20 @@ async def on_message(message): f"You can find a more detailed help and a list of templates at:\n" f"") return + if len(args) > 0 and args[0].lower().strip() == "delete": + if mid in SENT and len(SENT[mid]) > 0 and await delete(SENT[mid][-1]): + if not direct: + await delete(message) + else: + await message.add_reaction("⚠") + return async with message.channel.typing(): left_wmark_text = None if len(args) > 1 and message.author.display_name is not None: left_wmark_text = f"By {message.author.display_name}" img = meme_otron.compute(*args, left_wmark_text=left_wmark_text) if img is None: - await message.channel.send(f"Template `{args[0]}` not found\n" + await message.channel.send(f":warning: Template `{args[0]}` not found\n" f"You can find a more detailed help and a list of templates at:\n" f"") return @@ -103,20 +136,17 @@ async def on_message(message): response += f"\n- More info: <{meme.info}>" elif not direct: response = f"A meme by {message.author.mention}:" - await message.channel.send(response, - file=discord.File(filename="meme.jpg", fp=output.name)) + if mid not in SENT: + SENT[mid] = [] + response = await message.channel.send(response, + file=discord.File(filename="meme.jpg", fp=output.name)) + SENT[mid] += [response] try: os.remove(output.name) except PermissionError: pass if not direct: - try: - await message.delete() - except discord.Forbidden: - pass - except discord.NotFound: - pass - + await delete(message) # Launch client and rerun on errors while True: