1
0
mirror of https://gitlab.com/ddorn/tfjm-discord-bot.git synced 2025-07-28 00:25:24 +02:00

CustomBot with full reload

This commit is contained in:
ddorn
2020-04-30 17:26:33 +02:00
parent 3bfad6e0a9
commit 3b9f4aae95
9 changed files with 149 additions and 34 deletions

View File

@ -1,4 +1,6 @@
import code
import sys
from importlib import reload
from pprint import pprint
import discord
@ -8,13 +10,20 @@ from discord.ext.commands import Cog
from discord.utils import get
from src.constants import *
from src.core import CustomBot
COGS_SHORTCUTS = {"d": "dev", "ts": "teams", "t": "tirages", "m": "misc", "e": "errors"}
COGS_SHORTCUTS = {
"d": "tirages",
"e": "errors",
"m": "misc",
"t": "teams",
"u": "src.utils",
"v": "dev",
}
class DevCog(Cog, name="Dev tools"):
def __init__(self, bot: Bot):
def __init__(self, bot: CustomBot):
self.bot = bot
@command(name="interrupt")
@ -69,6 +78,11 @@ class DevCog(Cog, name="Dev tools"):
possibles: `teams`, `tirages`, `dev`.
"""
if name is None:
self.bot.reload()
await ctx.send(":tada: The bot was reloaded !")
return
names = [name] if name else list(COGS_SHORTCUTS.values())
for name in names:

View File

@ -5,8 +5,8 @@ import discord
from discord.ext.commands import *
from discord.utils import maybe_coroutine
from src.errors import UnwantedCommand
from src.core import CustomBot
from src.errors import UnwantedCommand, TfjmError
# Global variable and function because I'm too lazy to make a metaclass
handlers = {}
@ -29,6 +29,9 @@ def handles(error_type):
class ErrorsCog(Cog):
"""This cog defines all the handles for errors."""
def __init__(self, bot: CustomBot):
self.bot = bot
@Cog.listener()
async def on_command_error(self, ctx: Context, error: CommandError):
print(repr(error), file=sys.stderr)
@ -47,10 +50,11 @@ class ErrorsCog(Cog):
msg = await maybe_coroutine(handler, self, ctx, error)
if msg:
await ctx.send(msg)
message = await ctx.send(msg)
await self.bot.wait_for_bin(ctx.message.author, message)
@handles(UnwantedCommand)
async def on_unwanted_command(self, ctx, error):
async def on_unwanted_command(self, ctx, error: UnwantedCommand):
await ctx.message.delete()
author: discord.Message
await ctx.author.send(
@ -59,14 +63,19 @@ class ErrorsCog(Cog):
+ "\nC'est pas grave, c'est juste pour ne pas encombrer "
"le chat lors du tirage."
)
await ctx.author.send("Raison: " + error.original.msg)
await ctx.author.send("Raison: " + error.msg)
@handles(TfjmError)
async def on_tfjm_error(self, ctx: Context, error: TfjmError):
msg = await ctx.send(error.msg)
await self.bot.wait_for_bin(ctx.author, msg)
@handles(CommandInvokeError)
async def on_command_invoke_error(self, ctx, error):
specific_handler = handlers.get(type(error.original))
if specific_handler:
return await specific_handler(self, ctx, error)
return await specific_handler(self, ctx, error.original)
traceback.print_tb(error.original.__traceback__, file=sys.stderr)
return (
@ -91,4 +100,4 @@ class ErrorsCog(Cog):
def setup(bot):
bot.add_cog(ErrorsCog())
bot.add_cog(ErrorsCog(bot))

View File

@ -17,13 +17,15 @@ from discord.ext.commands import (
Group,
)
from src import utils
from src.constants import *
from src.constants import Emoji
from src.utils import has_role
from src.core import CustomBot
from src.utils import has_role, start_time
class MiscCog(Cog, name="Divers"):
def __init__(self, bot: Bot):
def __init__(self, bot: CustomBot):
self.bot = bot
self.show_hidden = False
self.verify_checks = True
@ -55,6 +57,7 @@ class MiscCog(Cog, name="Divers"):
await message.add_reaction(Emoji.JOY)
await message.add_reaction(Emoji.SOB)
await self.bot.wait_for_bin(ctx.message.author, message)
@command(name="status")
@commands.has_role(Role.CNO)
@ -65,7 +68,7 @@ class MiscCog(Cog, name="Divers"):
benevoles = [g for g in guild.members if has_role(g, Role.BENEVOLE)]
participants = [g for g in guild.members if has_role(g, Role.PARTICIPANT)]
no_role = [g for g in guild.members if g.top_role == guild.default_role]
uptime = datetime.timedelta(seconds=round(time() - START_TIME))
uptime = datetime.timedelta(seconds=round(time() - start_time()))
infos = {
"Bénévoles": len(benevoles),

View File

@ -7,13 +7,14 @@ from discord.ext.commands import Cog, Bot, group, Context
from discord.utils import get, find
from src.constants import *
from src.core import CustomBot
from src.utils import has_role
Team = namedtuple("Team", ["name", "trigram", "tournoi", "secret", "status"])
class TeamsCog(Cog, name="Teams"):
def __init__(self, bot: Bot):
def __init__(self, bot: CustomBot):
self.bot = bot
self.teams = self.load_teams()

View File

@ -14,6 +14,7 @@ from discord.ext.commands import group, Cog, Context
from discord.utils import get
from src.constants import *
from src.core import CustomBot
from src.errors import TfjmError, UnwantedCommand
__all__ = ["Tirage", "TirageCog"]
@ -606,7 +607,7 @@ class TirageOrderPhase(OrderPhase):
class TirageCog(Cog, name="Tirages"):
def __init__(self, bot):
self.bot: commands.Bot = bot
self.bot: CustomBot = bot
# We retrieve the global variable.
# We don't want tirages to be ust an attribute
@ -627,13 +628,21 @@ class TirageCog(Cog, name="Tirages"):
if channel in self.tirages:
await self.tirages[channel].dice(ctx, n)
else:
if n == 0:
raise TfjmError(f"Un dé sans faces ? Le concept m'intéresse...")
if n < 1:
raise TfjmError(f"Je ne peux pas lancer un dé à {n} faces, désolé.")
raise TfjmError(
f"Je ne peux pas lancer un dé avec un "
f"nombre négatif faces, désolé."
)
if len(str(n)) > 1900:
raise TfjmError(
"Oulà... Je sais que la taille ça ne compte pas, "
"mais là il est vraiment gros ton dé !"
)
dice = random.randint(1, n)
await ctx.send(
f"Le dé à {n} face{'s' * (n > 1)} s'est arrêté sur... **{dice}**"
)
await ctx.send(f"{ctx.author.mention} : {Emoji.DICE} {dice}")
@commands.command(
name="random-problem",