create Bot class and command modules
This commit is contained in:
parent
45ab8ce67a
commit
1ed84a6058
4 changed files with 32 additions and 50 deletions
19
bot.py
Normal file
19
bot.py
Normal file
|
|
@ -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)
|
||||||
7
commands/ping.py
Normal file
7
commands/ping.py
Normal file
|
|
@ -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!")
|
||||||
56
main.py
56
main.py
|
|
@ -3,7 +3,7 @@ import tomllib
|
||||||
import nerimity
|
import nerimity
|
||||||
import pylast
|
import pylast
|
||||||
|
|
||||||
import utils
|
import bot
|
||||||
|
|
||||||
with open("config.toml", "rb") as f:
|
with open("config.toml", "rb") as f:
|
||||||
config = tomllib.load(f)
|
config = tomllib.load(f)
|
||||||
|
|
@ -15,59 +15,15 @@ network = pylast.LastFMNetwork(
|
||||||
)
|
)
|
||||||
|
|
||||||
# nerimity client
|
# nerimity client
|
||||||
client = nerimity.Client(
|
bot = bot.Bot(
|
||||||
token=config['token'],
|
token=config['token'],
|
||||||
prefix='!'
|
prefix='!'
|
||||||
)
|
)
|
||||||
|
|
||||||
# Prefix command -> !ping
|
bot.load_commands("commands")
|
||||||
@client.command(name="ping")
|
|
||||||
@client.slash_command(name="ping", description="Ping...")
|
|
||||||
async def ping(ctx: nerimity.Context):
|
|
||||||
await ctx.send("Pong!")
|
|
||||||
|
|
||||||
# fm command
|
@bot.listen("on_ready")
|
||||||
@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")
|
|
||||||
async def on_ready(params):
|
async def on_ready(params):
|
||||||
print(f"Logged in as {client.account.username}")
|
print(f"Logged in as {bot.account.username}")
|
||||||
|
|
||||||
|
bot.run()
|
||||||
client.run()
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue