Hot reload extension and dev cog

This commit is contained in:
ddorn 2020-04-28 12:41:26 +02:00
parent 3cf27d82df
commit 3a231eda10
3 changed files with 72 additions and 35 deletions

64
src/cogs/dev.py Normal file
View File

@ -0,0 +1,64 @@
import code
from pprint import pprint
from discord.ext.commands import command, has_role, Bot
from discord.ext.commands import Cog
from src.constants import *
class DevCog(Cog, name="Dev tools"):
def __init__(self, bot: Bot):
self.bot = bot
@command(name="interrupt")
@has_role(Role.DEV)
async def interrupt_cmd(self, ctx):
"""
(dev) Ouvre une console un @dev m'a lancé. :warning:
A utiliser en dernier recours:
- le bot sera inactif pendant ce temps.
- toutes les commandes seront executées à sa reprise.
"""
await ctx.send(
"J'ai été arrêté et une console interactive a été ouverte là où je tourne. "
"Toutes les commandes rateront tant que cette console est ouverte.\n"
"Soyez rapides, je déteste les opérations à coeur ouvert... :confounded:"
)
# Utility functions
local = {
**globals(),
**locals(),
"pprint": pprint,
"_show": lambda o: print(*dir(o), sep="\n"),
"__name__": "__console__",
"__doc__": None,
}
code.interact(
banner="Ne SURTOUT PAS FAIRE Ctrl+C !\n(TFJM² debugger)", local=local
)
await ctx.send("Tout va mieux !")
@command(name="reload")
@has_role(Role.DEV)
async def reload_cmd(self, ctx, name):
if name in ("dev", "teams", "tirages"):
name = f"src.cogs.{name}"
try:
self.bot.reload_extension(name)
except:
await ctx.send(f":grimacing: **{name}** n'a pas pu être rechargée.")
raise
else:
await ctx.send(f":tada: L'extension **{name}** a bien été rechargée.")
def setup(bot: Bot):
bot.add_cog(DevCog(bot))

View File

@ -58,7 +58,7 @@ class TirageCog(Cog, name="Tirages"):
await ctx.send(f"Le problème tiré est... **{problem}**")
@commands.command(
name="oui", aliases=["accept", "yes", "o", "accepte", "ouiiiiiii"],
name="oui", aliases=["accept", "yes", "o", "oh-yeaaah", "accepte", "ouiiiiiii"],
)
async def accept_cmd(self, ctx):
"""

View File

@ -43,38 +43,6 @@ async def choose(ctx: Context, *args):
await ctx.send(f"J'ai choisi... **{choice}**")
@bot.command(name="interrupt")
@commands.has_role(Role.DEV)
async def interrupt_cmd(ctx):
"""
:warning: Ouvre une console un @dev m'a lancé. :warning:
A utiliser en dernier recours:
- le bot sera inactif pendant ce temps.
- toutes les commandes seront executées à sa reprise.
"""
await ctx.send(
"J'ai été arrêté et une console interactive a été ouverte là où je tourne. "
"Toutes les commandes rateront tant que cette console est ouverte.\n"
"Soyez rapides, je déteste les opérations à coeur ouvert... :confounded:"
)
# Utility function
local = {
**globals(),
**locals(),
"pprint": pprint,
"_show": lambda o: print(*dir(o), sep="\n"),
"__name__": "__console__",
"__doc__": None,
}
code.interact(banner="Ne SURTOUT PAS FAIRE Ctrl+C !\n(TFJM² debugger)", local=local)
await ctx.send("Tout va mieux !")
@bot.event
async def on_command_error(ctx: Context, error, *args, **kwargs):
if isinstance(error, commands.CommandInvokeError):
@ -90,7 +58,11 @@ async def on_command_error(ctx: Context, error, *args, **kwargs):
await ctx.author.send("Raison: " + error.original.msg)
return
else:
msg = str(error.original) or str(error)
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:
@ -98,7 +70,7 @@ async def on_command_error(ctx: Context, error, *args, **kwargs):
name = str(error).partition('"')[2].rpartition('"')[0]
msg = f"La commande {name} n'éxiste pas. Pour un liste des commandes, envoie `!help`."
else:
msg = str(error)
msg = repr(error)
print(repr(error), dir(error), file=sys.stderr)
await ctx.send(msg)
@ -106,6 +78,7 @@ async def on_command_error(ctx: Context, error, *args, **kwargs):
bot.load_extension("src.cogs.tirages")
bot.load_extension("src.cogs.teams")
bot.load_extension("src.cogs.dev")
if __name__ == "__main__":