From ab02ffa8592c43b3eb9a2b8fd7163fbfecdaff12 Mon Sep 17 00:00:00 2001 From: ddorn Date: Sun, 10 May 2020 17:30:45 +0200 Subject: [PATCH] :sparkles: print direct messages --- bot.py | 5 ++--- src/__init__.py | 2 +- src/cogs/dev.py | 12 +++++++++++- src/core.py | 14 +++++++++----- src/tfjm_discord_bot.py | 29 +++++++++++++++++------------ src/utils.py | 7 +++++++ 6 files changed, 47 insertions(+), 22 deletions(-) diff --git a/bot.py b/bot.py index 565ef24..f472b66 100644 --- a/bot.py +++ b/bot.py @@ -1,5 +1,4 @@ -from src import bot -from src.constants import TOKEN +from src import start if __name__ == "__main__": - bot.run(TOKEN) + start() diff --git a/src/__init__.py b/src/__init__.py index d19a14b..e2eacaa 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -1 +1 @@ -from src.tfjm_discord_bot import bot +from src.tfjm_discord_bot import start diff --git a/src/cogs/dev.py b/src/cogs/dev.py index 83b81d3..e1ee8c3 100644 --- a/src/cogs/dev.py +++ b/src/cogs/dev.py @@ -2,7 +2,7 @@ import asyncio from pprint import pprint import discord -from discord import TextChannel, PermissionOverwrite, Message +from discord import TextChannel, PermissionOverwrite, Message, ChannelType from discord.ext.commands import ( command, has_role, @@ -16,6 +16,7 @@ from ptpython.repl import embed from src.constants import * from src.core import CustomBot +from src.utils import fg COGS_SHORTCUTS = { "bt": "src.base_tirage", @@ -203,6 +204,15 @@ class DevCog(Cog, name="Dev tools"): await channel.delete_messages(to_delete) await ctx.message.delete() + @Cog.listener() + async def on_message(self, msg: Message): + ch: TextChannel = msg.channel + if ch.type == ChannelType.private: + m = f"""{fg(msg.author.name)}: {msg.content} +MSG_ID: {fg(msg.id, 0x03A678)} +CHA_ID: {fg(msg.channel.id, 0x03A678)}""" + print(m) + def setup(bot: CustomBot): bot.add_cog(DevCog(bot)) diff --git a/src/core.py b/src/core.py index 370f1c9..dae73f6 100644 --- a/src/core.py +++ b/src/core.py @@ -2,7 +2,7 @@ import asyncio import sys from importlib import reload -from discord import User, Message, Reaction, NotFound +from discord import User, Message, Reaction, NotFound, Forbidden from discord.ext.commands import Bot __all__ = ["CustomBot"] @@ -60,8 +60,12 @@ class CustomBot(Bot): reaction, u = await bot.wait_for( "reaction_add", check=check, timeout=timeout ) - the_msg = get(msgs, id=reaction.message.id) - await the_msg.delete() + + the_msg: Message = get(msgs, id=reaction.message.id) + try: + await the_msg.delete() + except NotFound: + pass # message was deleted msgs.remove(the_msg) except asyncio.TimeoutError: pass @@ -69,6 +73,6 @@ class CustomBot(Bot): for m in msgs: try: await m.clear_reaction(Emoji.BIN) - except NotFound: - # Message or reaction deleted + except (NotFound, Forbidden): + # Message or reaction deleted / in dm channel pass diff --git a/src/tfjm_discord_bot.py b/src/tfjm_discord_bot.py index 6d26380..23d7205 100644 --- a/src/tfjm_discord_bot.py +++ b/src/tfjm_discord_bot.py @@ -5,7 +5,8 @@ from src.core import CustomBot # We allow "! " to catch people that put a space in their commands. # It must be in first otherwise "!" always match first and the space is not recognised -bot = CustomBot(("! ", "!")) +from src.utils import fg + # Global variable to hold the tirages. # We *want* it to be global so we can reload the tirages cog without @@ -13,19 +14,23 @@ bot = CustomBot(("! ", "!")) tirages = {} -@bot.event -async def on_ready(): - print(f"{bot.user} has connected to Discord!") +def start(): + bot = CustomBot(("! ", "!")) + @bot.event + async def on_ready(): + print(f"{bot.user} has connected to Discord!") -bot.remove_command("help") -bot.load_extension("src.cogs.dev") -bot.load_extension("src.cogs.errors") -bot.load_extension("src.cogs.misc") -bot.load_extension("src.cogs.teams") -bot.load_extension("src.cogs.tirages") -bot.load_extension("src.utils") + bot.remove_command("help") + bot.load_extension("src.cogs.dev") + bot.load_extension("src.cogs.errors") + bot.load_extension("src.cogs.misc") + bot.load_extension("src.cogs.teams") + bot.load_extension("src.cogs.tirages") + bot.load_extension("src.utils") + + bot.run(TOKEN) if __name__ == "__main__": - bot.run(TOKEN) + start() diff --git a/src/utils.py b/src/utils.py index 4442d3d..b5a14e9 100644 --- a/src/utils.py +++ b/src/utils.py @@ -4,6 +4,13 @@ import psutil from discord.ext.commands import Bot +def fg(text, color: int = 0xFFA500): + r = color >> 16 + g = color >> 8 & 0xFF + b = color & 0xFF + return f"\033[38;2;{r};{g};{b}m{text}\033[m" + + def french_join(l): l = list(l) start = ", ".join(l[:-1])