2020-04-27 08:05:17 +00:00
|
|
|
#!/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
|
2020-04-27 08:05:17 +00:00
|
|
|
from src.constants import *
|
|
|
|
from src.errors import TfjmError, UnwantedCommand
|
|
|
|
|
2020-04-27 20:26:04 +00:00
|
|
|
# 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 08:05:17 +00:00
|
|
|
|
2020-04-27 09:43:16 +00:00
|
|
|
# Variable globale qui contient les tirages.
|
|
|
|
tirages = {}
|
2020-04-27 08:05:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
@bot.event
|
|
|
|
async def on_ready():
|
|
|
|
print(f"{bot.user} has connected to Discord!")
|
|
|
|
|
|
|
|
|
|
|
|
@bot.command(
|
|
|
|
name="choose",
|
2020-04-27 10:28:30 +00:00
|
|
|
usage='choix1 choix2 "choix 3"...',
|
2020-04-27 08:05:17 +00:00
|
|
|
aliases=["choice", "choix", "ch"],
|
|
|
|
)
|
|
|
|
async def choose(ctx: Context, *args):
|
2020-04-27 10:28:30 +00:00
|
|
|
"""
|
|
|
|
Choisit une option parmi tous les arguments.
|
2020-04-27 08:05:17 +00:00
|
|
|
|
2020-04-27 10:28:30 +00:00
|
|
|
Pour les options qui contiennent une espace,
|
|
|
|
il suffit de mettre des guillemets (`"`) autour.
|
|
|
|
"""
|
2020-04-27 08:05:17 +00:00
|
|
|
|
2020-04-27 10:28:30 +00:00
|
|
|
choice = random.choice(args)
|
|
|
|
await ctx.send(f"J'ai choisi... **{choice}**")
|
2020-04-27 08:05:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
@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:
|
2020-04-28 10:41:26 +00:00
|
|
|
msg = (
|
|
|
|
error.original.__class__.__name__
|
|
|
|
+ ": "
|
|
|
|
+ (str(error.original) or str(error))
|
|
|
|
)
|
2020-04-27 08:05:17 +00:00
|
|
|
traceback.print_tb(error.original.__traceback__, file=sys.stderr)
|
2020-04-27 20:26:04 +00:00
|
|
|
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"
|
2020-04-27 08:05:17 +00:00
|
|
|
else:
|
2020-04-28 10:41:26 +00:00
|
|
|
msg = repr(error)
|
2020-04-27 08:05:17 +00:00
|
|
|
|
|
|
|
print(repr(error), dir(error), file=sys.stderr)
|
|
|
|
await ctx.send(msg)
|
|
|
|
|
|
|
|
|
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")
|
2020-04-28 10:41:26 +00:00
|
|
|
bot.load_extension("src.cogs.dev")
|
2020-04-27 09:43:16 +00:00
|
|
|
|
|
|
|
|
2020-04-27 08:05:17 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
bot.run(TOKEN)
|