Compare commits

..

No commits in common. "025e5fc653c7884d95588c16469b981df372ae60" and "32817f735e994420da7c9eacfbe5a715bed55ce3" have entirely different histories.

5 changed files with 30 additions and 38 deletions

View File

@ -13,6 +13,7 @@ RELOADABLE_MODULES = [
"constants", "constants",
"core", "core",
"events", "events",
"player",
"utils", "utils",
"voice", "voice",
"youtubedl", "youtubedl",

19
core.py
View File

@ -16,9 +16,6 @@ from state import command_locks
async def on_message(message): async def on_message(message):
if not message.content.startswith(constants.PREFIX) or message.author.bot:
return
tokens = commands.tokenize(message.content) tokens = commands.tokenize(message.content)
if not tokens: if not tokens:
return return
@ -125,16 +122,16 @@ async def on_message(message):
def rreload(reloaded_modules, module): def rreload(reloaded_modules, module):
reloaded_modules.add(module.__name__) 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__()
for submodule in filter( with contextlib.suppress(AttributeError):
lambda v: inspect.ismodule(v) for submodule in filter(
and v.__name__ in constants.RELOADABLE_MODULES lambda v: inspect.ismodule(v)
and v.__name__ not in reloaded_modules, and v.__name__ in constants.RELOADABLE_MODULES
vars(module).values(), and v.__name__ not in reloaded_modules,
): map(lambda attr: getattr(module, attr), dir(module)),
rreload(reloaded_modules, submodule) ):
rreload(reloaded_modules, submodule)
importlib.reload(module) importlib.reload(module)

View File

@ -1,30 +1,7 @@
import core
import events
from state import client
dynamic_handlers = {} dynamic_handlers = {}
async def trigger_dynamic_handlers(event_type: str, *data): async def trigger_dynamic_handlers(event_type: str, *data):
if event_type in dynamic_handlers: if event_type in dynamic_handlers:
for dynamic_handler in dynamic_handlers[event_type]: for message_handler in dynamic_handlers[event_type]:
try: await message_handler(*data)
await dynamic_handler(*data)
except Exception as e:
print(
f"error in dynamic event handler {dynamic_handler} for {event_type}: {e}"
)
@client.event
async def on_message_edit(before, after):
await events.trigger_dynamic_handlers("on_message_edit", before, after)
await core.on_message(after)
@client.event
async def on_message(message):
await events.trigger_dynamic_handlers("on_message", message)
await core.on_message(message)

19
main.py
View File

@ -1,14 +1,31 @@
import time import time
import constants import constants
import core
import events import events
from state import client, start_time from state import client, start_time
@client.event @client.event
async def on_ready(): async def on_ready():
await events.trigger_dynamic_handlers("on_ready")
print(f"logged in as {client.user} in {round(time.time() - start_time, 1)}s") print(f"logged in as {client.user} in {round(time.time() - start_time, 1)}s")
@client.event
async def on_message_edit(before, after):
await events.trigger_dynamic_handlers("on_message_edit", before, after)
await on_message(after)
@client.event
async def on_message(message):
await events.trigger_dynamic_handlers("on_message", message)
if not message.content.startswith(constants.PREFIX):
return
await core.on_message(message)
client.run(constants.SECRETS["TOKEN"]) client.run(constants.SECRETS["TOKEN"])

View File

@ -66,7 +66,7 @@ class QueuedSong:
def format(self, with_queuer=False, hide_preview=False) -> str: def format(self, with_queuer=False, hide_preview=False) -> str:
return ( return (
f"[`{self.player.title}`]({'<' if hide_preview else ''}{self.player.original_url}{'>' if hide_preview else ''}) [{self.format_duration(self.player.duration) if self.player.duration else 'live'}]" f"[`{self.player.title}`]({'<' if hide_preview else ''}{self.player.original_url}{'>' if hide_preview else ''}) [{self.format_duration(self.player.duration)}]"
+ (f" (<@{self.queuer}>)" if with_queuer else "") + (f" (<@{self.queuer}>)" if with_queuer else "")
) )