feat(commands/bot): add help command and fix nested reloading
This commit is contained in:
parent
d0402a58e9
commit
e4af64fa99
@ -6,6 +6,15 @@ import utils
|
||||
from state import start_time
|
||||
|
||||
|
||||
async def help(message):
|
||||
await utils.reply(
|
||||
message,
|
||||
", ".join(
|
||||
[f"`{command.value}`" for command in commands.Command.__members__.values()]
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def uptime(message):
|
||||
tokens = commands.tokenize(message.content)
|
||||
parser = arguments.ArgumentParser(
|
||||
|
@ -6,6 +6,7 @@ import constants
|
||||
class Command(enum.Enum):
|
||||
CLEAR = "clear"
|
||||
EXECUTE = "execute"
|
||||
HELP = "help"
|
||||
JOIN = "join"
|
||||
LEAVE = "leave"
|
||||
PAUSE = "pause"
|
||||
|
@ -178,10 +178,7 @@ async def volume(message):
|
||||
|
||||
if args.volume:
|
||||
message.guild.voice_client.source.volume = float(args.volume) / 100.0
|
||||
await utils.reply(
|
||||
message,
|
||||
f"{args.volume}",
|
||||
)
|
||||
await utils.add_check_reaction(message)
|
||||
else:
|
||||
await utils.reply(
|
||||
message,
|
||||
|
12
constants.py
12
constants.py
@ -1,10 +1,18 @@
|
||||
import os
|
||||
import sys
|
||||
|
||||
EMBED_COLOR = 0xFF6600
|
||||
OWNERS = [531392146767347712]
|
||||
PREFIX = "%"
|
||||
RELOAD_BLACKLISTED_MODULES = [*sys.builtin_module_names]
|
||||
RELOADABLE_MODULES = [
|
||||
"arguments",
|
||||
"commands",
|
||||
"constants",
|
||||
"core",
|
||||
"events",
|
||||
"utils",
|
||||
"voice",
|
||||
"ytdlp",
|
||||
]
|
||||
|
||||
YTDL_OPTIONS = {
|
||||
"default_search": "auto",
|
||||
|
33
core.py
33
core.py
@ -9,6 +9,7 @@ import disnake_paginator
|
||||
|
||||
import commands
|
||||
import constants
|
||||
import core
|
||||
import utils
|
||||
|
||||
|
||||
@ -30,6 +31,16 @@ async def on_message(message):
|
||||
C = commands.Command
|
||||
try:
|
||||
match matched[0]:
|
||||
case C.RELOAD if message.author.id in constants.OWNERS:
|
||||
reloaded_modules = set()
|
||||
for module in filter(
|
||||
lambda v: inspect.ismodule(v)
|
||||
and v.__name__ in constants.RELOADABLE_MODULES,
|
||||
globals().values(),
|
||||
):
|
||||
core.rreload(reloaded_modules, module)
|
||||
|
||||
await utils.add_check_reaction(message)
|
||||
case C.EXECUTE if message.author.id in constants.OWNERS:
|
||||
code = message.content[len(tokens[0]) + 1 :].strip().strip("`")
|
||||
for replacement in ["python", "py"]:
|
||||
@ -91,6 +102,8 @@ async def on_message(message):
|
||||
await commands.voice.pause(message)
|
||||
case C.VOLUME:
|
||||
await commands.voice.volume(message)
|
||||
case C.HELP:
|
||||
await commands.bot.help(message)
|
||||
case C.UPTIME:
|
||||
await commands.bot.uptime(message)
|
||||
except Exception as e:
|
||||
@ -101,22 +114,16 @@ async def on_message(message):
|
||||
|
||||
|
||||
def rreload(reloaded_modules, module):
|
||||
reloaded_modules.add(module)
|
||||
reloaded_modules.add(module.__name__)
|
||||
importlib.reload(module)
|
||||
if "__reload_module__" in dir(module):
|
||||
module.__reload_module__()
|
||||
|
||||
with contextlib.suppress(AttributeError):
|
||||
for module in filter(
|
||||
lambda m: m.__spec__.origin != "frozen",
|
||||
filter(
|
||||
lambda v: inspect.ismodule(v)
|
||||
and (
|
||||
v.__name__.split(".")[-1]
|
||||
not in constants.RELOAD_BLACKLISTED_MODULES
|
||||
)
|
||||
and (v not in reloaded_modules),
|
||||
map(lambda attr: getattr(module, attr), dir(module)),
|
||||
),
|
||||
for submodule in filter(
|
||||
lambda v: inspect.ismodule(v)
|
||||
and (v.__name__.split(".")[-1] in constants.RELOADABLE_MODULES)
|
||||
and v.__name__ not in reloaded_modules,
|
||||
map(lambda attr: getattr(module, attr), dir(module)),
|
||||
):
|
||||
rreload(reloaded_modules, module)
|
||||
rreload(reloaded_modules, submodule)
|
||||
|
19
main.py
19
main.py
@ -1,11 +1,8 @@
|
||||
import inspect
|
||||
import time
|
||||
|
||||
import commands
|
||||
import constants
|
||||
import core
|
||||
import events
|
||||
import utils
|
||||
from state import client, start_time
|
||||
|
||||
|
||||
@ -25,25 +22,9 @@ async def on_message_edit(before, after):
|
||||
async def on_message(message):
|
||||
await events.trigger_dynamic_handlers("on_message", message)
|
||||
|
||||
global reloaded_modules
|
||||
|
||||
if not message.content.startswith(constants.PREFIX):
|
||||
return
|
||||
|
||||
if message.author.id in constants.OWNERS and commands.match(message.content) == [
|
||||
commands.Command.RELOAD
|
||||
]:
|
||||
reloaded_modules = set()
|
||||
for module in filter(
|
||||
lambda v: inspect.ismodule(v)
|
||||
and v.__name__ not in constants.RELOAD_BLACKLISTED_MODULES,
|
||||
globals().values(),
|
||||
):
|
||||
core.rreload(reloaded_modules, module)
|
||||
|
||||
await utils.add_check_reaction(message)
|
||||
return
|
||||
|
||||
await core.on_message(message)
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user