diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..85b197e --- /dev/null +++ b/bot.py @@ -0,0 +1,19 @@ +import os +import importlib + +import nerimity + +class Bot(nerimity.Client): + """Extended client class for extra functionality.""" + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def load_commands(self, commands_dir: str): + for filename in os.listdir(commands_dir): + if filename.endswith(".py") and not filename.startswith("_"): + module_name = f"{commands_dir}.{filename[:-3]}" + module = importlib.import_module(module_name) + if hasattr(module, "setup"): + module.setup(self) + print(f"Loaded {module_name}") + print("Registered commands:", self.commands) diff --git a/commands/ping.py b/commands/ping.py new file mode 100644 index 0000000..d9b581e --- /dev/null +++ b/commands/ping.py @@ -0,0 +1,7 @@ +import nerimity + +def setup(bot): + @bot.command(name="ping") + @bot.slash_command(name="ping", description="Ping...") + async def ping(ctx: nerimity.Context): + await ctx.send("Pong!") diff --git a/utils.py b/commands/utils.py similarity index 100% rename from utils.py rename to commands/utils.py diff --git a/main.py b/main.py index 56da999..3c20073 100644 --- a/main.py +++ b/main.py @@ -3,7 +3,7 @@ import tomllib import nerimity import pylast -import utils +import bot with open("config.toml", "rb") as f: config = tomllib.load(f) @@ -15,59 +15,15 @@ network = pylast.LastFMNetwork( ) # nerimity client -client = nerimity.Client( +bot = bot.Bot( token=config['token'], prefix='!' ) -# Prefix command -> !ping -@client.command(name="ping") -@client.slash_command(name="ping", description="Ping...") -async def ping(ctx: nerimity.Context): - await ctx.send("Pong!") +bot.load_commands("commands") -# fm command -@client.command(name="fm", aliases=["np"]) -@client.slash_command(name="fm", description="Shows what you're currently playing") -async def fm(ctx: nerimity.Context, params: str = ""): - username = "" - - if params == "": - username = "kaaisudev" - else: - username = params - - try: - track = network.get_user(username).get_now_playing() - if track == None: - now_playing = False - played_track = network.get_user(username).get_recent_tracks(limit=1) - track = played_track[0].track - - else: - now_playing = True - - track_url = track.get_url() - track_name = track.get_name() - track_artist = track.get_artist().get_name() - track_artist_url = track.get_artist().get_url() - - if now_playing == True: - await ctx.send(f"Now playing for **{username}**: [{track_name}]({track_url}) by [{track_artist}]({track_artist_url})") - else: - await ctx.send(f"Last played for **{username}**: [{track_name}]({track_url}) by [{track_artist}]({track_artist_url})") - - except IndexError: - await ctx.send(utils.error_msg("User has no recent tracks or they're set to private.")) - except pylast.WSError: - await ctx.send(utils.error_msg("User not found.")) - except Exception as e: - print(e) - await ctx.send(utils.error_msg("Unknown error.")) - -@client.listen("on_ready") +@bot.listen("on_ready") async def on_ready(params): - print(f"Logged in as {client.account.username}") + print(f"Logged in as {bot.account.username}") - -client.run() +bot.run()