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