Compare commits

...

4 commits

Author SHA1 Message Date
36485fbda8 minor formatting 2025-10-07 08:06:50 -03:00
75dd4f796e do not process commands if author is self 2025-10-07 08:06:35 -03:00
6bcf41f208 misc admin commands 2025-10-07 08:03:03 -03:00
d37d773b9b modify todo list 2025-10-07 06:24:28 -03:00
5 changed files with 60 additions and 1 deletions

View file

@ -13,3 +13,4 @@ dead simple last.fm bot
- add collage generation
- fix attachment generation and upload
- temporary fix: upload and link to catbox urls
- permanent fix: fork and update nerimity library

19
bot.py
View file

@ -7,9 +7,11 @@ import pylast
class Bot(nerimity.Client):
"""Extended client class for extra functionality."""
def __init__(self, lastfm_api_key: str, lastfm_api_secret: str, db_file: str = "database.db", *args, **kwargs):
def __init__(self, lastfm_api_key: str, lastfm_api_secret: str, owner_id: int = None, db_file: str = "database.db", *args, **kwargs):
super().__init__(*args, **kwargs)
self.owner_id = owner_id
# initialize last.fm client
self.lastfm = pylast.LastFMNetwork(
api_key = lastfm_api_key,
@ -19,6 +21,7 @@ class Bot(nerimity.Client):
self.db = None
self.db_file = db_file
def load_commands(self, commands_dir: str):
for filename in os.listdir(commands_dir):
if filename.endswith(".py") and not filename.startswith("_"):
@ -28,6 +31,20 @@ class Bot(nerimity.Client):
module.setup(self)
print(f"Loaded {module_name}")
print("Registered commands:", self.commands)
def is_owner(self, user: nerimity.Member = None):
if not user: return None
else:
if user.id == self.owner_id: return True
else: return False
def _process_slash_commands(self, message: nerimity.Message):
if message.author.id == self.account.id: return
super()._process_slash_commands(message)
async def _process_commands(self, message: nerimity.Message):
if message.author.id == self.account.id: return
await super()._process_commands(message)
async def init_db(self):
print("Initializing database...")

39
commands/admin.py Normal file
View file

@ -0,0 +1,39 @@
import nerimity
import bot
import utils as u
def setup(bot: bot.Bot):
@bot.command(name="echo")
async def echo(ctx: nerimity.Context, *text: str):
print(bot.is_owner(ctx.author))
if not bot.is_owner(ctx.author):
print("returned")
return
if not text:
await ctx.send(u.error_msg("Contents are empty."))
return
try: await ctx.send(" ".join(text))
except Exception as e:
print(e)
await ctx.send(u.error_msg(f"Unknown error:\n`{e}`"))
@bot.command(name="post")
async def post(ctx: nerimity.Context, *text: str):
print(bot.is_owner(ctx.author))
if not bot.is_owner(ctx.author):
print("returned")
return
if not text:
await ctx.send(u.error_msg("Contents are empty."))
return
try:
await ctx.send(f"Creating post:\n```markdown\n{" ".join(text)}\n```")
# post = nerimity.Post.create_post(content=" ".join(text))
# print(post)
await ctx.send(u.good_msg(f"Post created!!"))
except Exception as e:
print(e)
await ctx.send(u.error_msg(f"Unknown error:\n`{e}`"))

View file

@ -1,3 +1,4 @@
token = "NerimityTokenHere"
owner_id = 1234567890
api_key = "LastFmApiKeyHere"
api_secret = "LastFmApiSecretHere"

View file

@ -9,6 +9,7 @@ with open("config.toml", "rb") as f:
bot = bot.Bot(
prefix = '!',
token = config['token'],
owner_id = config['owner_id'],
lastfm_api_key = config['api_key'],
lastfm_api_secret = config['api_secret']
)