tfjm-discord-bot/src/tfjm_discord_bot.py

73 lines
2.3 KiB
Python
Raw Normal View History

#!/bin/python
import code
import random
import sys
import traceback
from pprint import pprint
import discord
from discord.ext import commands
from discord.ext.commands import Context
2020-04-27 10:28:30 +00:00
from src.cogs import TfjmHelpCommand
from src.constants import *
from src.errors import TfjmError, UnwantedCommand
# 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 = commands.Bot(("! ", "!"), help_command=TfjmHelpCommand())
2020-04-27 09:43:16 +00:00
# Variable globale qui contient les tirages.
tirages = {}
@bot.event
async def on_ready():
print(f"{bot.user} has connected to Discord!")
@bot.event
async def on_command_error(ctx: Context, error, *args, **kwargs):
if isinstance(error, commands.CommandInvokeError):
if isinstance(error.original, UnwantedCommand):
await ctx.message.delete()
author: discord.Message
await ctx.author.send(
"J'ai supprimé ton message:\n> "
+ ctx.message.clean_content
+ "\nC'est pas grave, c'est juste pour ne pas encombrer "
"le chat lors du tirage."
)
await ctx.author.send("Raison: " + error.original.msg)
return
else:
msg = (
error.original.__class__.__name__
+ ": "
+ (str(error.original) or str(error))
)
traceback.print_tb(error.original.__traceback__, file=sys.stderr)
elif isinstance(error, commands.CommandNotFound):
# Here we just take adventage that the error is formatted this way:
# 'Command "NAME" is not found'
name = str(error).partition('"')[2].rpartition('"')[0]
msg = f"La commande {name} n'éxiste pas. Pour un liste des commandes, envoie `!help`."
2020-04-28 18:08:57 +00:00
elif isinstance(error, commands.MissingRole):
msg = f"Il te faut le role de {error.missing_role} pour utiliser cette commande"
else:
msg = repr(error)
print(repr(error), dir(error), file=sys.stderr)
await ctx.send(msg)
2020-04-29 11:42:49 +00:00
bot.remove_command("help")
2020-04-27 09:43:16 +00:00
bot.load_extension("src.cogs.tirages")
2020-04-27 12:46:04 +00:00
bot.load_extension("src.cogs.teams")
bot.load_extension("src.cogs.dev")
2020-04-28 18:25:27 +00:00
bot.load_extension("src.cogs.misc")
2020-04-27 09:43:16 +00:00
if __name__ == "__main__":
bot.run(TOKEN)