print direct messages

This commit is contained in:
ddorn 2020-05-10 17:30:45 +02:00
parent 4492a5785d
commit ab02ffa859
6 changed files with 47 additions and 22 deletions

5
bot.py
View File

@ -1,5 +1,4 @@
from src import bot from src import start
from src.constants import TOKEN
if __name__ == "__main__": if __name__ == "__main__":
bot.run(TOKEN) start()

View File

@ -1 +1 @@
from src.tfjm_discord_bot import bot from src.tfjm_discord_bot import start

View File

@ -2,7 +2,7 @@ import asyncio
from pprint import pprint from pprint import pprint
import discord import discord
from discord import TextChannel, PermissionOverwrite, Message from discord import TextChannel, PermissionOverwrite, Message, ChannelType
from discord.ext.commands import ( from discord.ext.commands import (
command, command,
has_role, has_role,
@ -16,6 +16,7 @@ from ptpython.repl import embed
from src.constants import * from src.constants import *
from src.core import CustomBot from src.core import CustomBot
from src.utils import fg
COGS_SHORTCUTS = { COGS_SHORTCUTS = {
"bt": "src.base_tirage", "bt": "src.base_tirage",
@ -203,6 +204,15 @@ class DevCog(Cog, name="Dev tools"):
await channel.delete_messages(to_delete) await channel.delete_messages(to_delete)
await ctx.message.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): def setup(bot: CustomBot):
bot.add_cog(DevCog(bot)) bot.add_cog(DevCog(bot))

View File

@ -2,7 +2,7 @@ import asyncio
import sys import sys
from importlib import reload 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 from discord.ext.commands import Bot
__all__ = ["CustomBot"] __all__ = ["CustomBot"]
@ -60,8 +60,12 @@ class CustomBot(Bot):
reaction, u = await bot.wait_for( reaction, u = await bot.wait_for(
"reaction_add", check=check, timeout=timeout "reaction_add", check=check, timeout=timeout
) )
the_msg = get(msgs, id=reaction.message.id)
the_msg: Message = get(msgs, id=reaction.message.id)
try:
await the_msg.delete() await the_msg.delete()
except NotFound:
pass # message was deleted
msgs.remove(the_msg) msgs.remove(the_msg)
except asyncio.TimeoutError: except asyncio.TimeoutError:
pass pass
@ -69,6 +73,6 @@ class CustomBot(Bot):
for m in msgs: for m in msgs:
try: try:
await m.clear_reaction(Emoji.BIN) await m.clear_reaction(Emoji.BIN)
except NotFound: except (NotFound, Forbidden):
# Message or reaction deleted # Message or reaction deleted / in dm channel
pass pass

View File

@ -5,7 +5,8 @@ from src.core import CustomBot
# We allow "! " to catch people that put a space in their commands. # 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 # 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. # Global variable to hold the tirages.
# We *want* it to be global so we can reload the tirages cog without # We *want* it to be global so we can reload the tirages cog without
@ -13,11 +14,13 @@ bot = CustomBot(("! ", "!"))
tirages = {} tirages = {}
def start():
bot = CustomBot(("! ", "!"))
@bot.event @bot.event
async def on_ready(): async def on_ready():
print(f"{bot.user} has connected to Discord!") print(f"{bot.user} has connected to Discord!")
bot.remove_command("help") bot.remove_command("help")
bot.load_extension("src.cogs.dev") bot.load_extension("src.cogs.dev")
bot.load_extension("src.cogs.errors") bot.load_extension("src.cogs.errors")
@ -26,6 +29,8 @@ bot.load_extension("src.cogs.teams")
bot.load_extension("src.cogs.tirages") bot.load_extension("src.cogs.tirages")
bot.load_extension("src.utils") bot.load_extension("src.utils")
bot.run(TOKEN)
if __name__ == "__main__": if __name__ == "__main__":
bot.run(TOKEN) start()

View File

@ -4,6 +4,13 @@ import psutil
from discord.ext.commands import Bot 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): def french_join(l):
l = list(l) l = list(l)
start = ", ".join(l[:-1]) start = ", ".join(l[:-1])