misc admin commands

This commit is contained in:
yuki 2025-10-07 08:03:03 -03:00
parent d37d773b9b
commit 6bcf41f208
4 changed files with 51 additions and 1 deletions

11
bot.py
View file

@ -7,9 +7,11 @@ import pylast
class Bot(nerimity.Client): class Bot(nerimity.Client):
"""Extended client class for extra functionality.""" """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) super().__init__(*args, **kwargs)
self.owner_id = owner_id
# initialize last.fm client # initialize last.fm client
self.lastfm = pylast.LastFMNetwork( self.lastfm = pylast.LastFMNetwork(
api_key = lastfm_api_key, api_key = lastfm_api_key,
@ -19,6 +21,7 @@ class Bot(nerimity.Client):
self.db = None self.db = None
self.db_file = db_file self.db_file = db_file
def load_commands(self, commands_dir: str): def load_commands(self, commands_dir: str):
for filename in os.listdir(commands_dir): for filename in os.listdir(commands_dir):
if filename.endswith(".py") and not filename.startswith("_"): if filename.endswith(".py") and not filename.startswith("_"):
@ -29,6 +32,12 @@ class Bot(nerimity.Client):
print(f"Loaded {module_name}") print(f"Loaded {module_name}")
print("Registered commands:", self.commands) 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
async def init_db(self): async def init_db(self):
print("Initializing database...") print("Initializing database...")
self.db = await aiosqlite.connect(self.db_file) self.db = await aiosqlite.connect(self.db_file)

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" token = "NerimityTokenHere"
owner_id = 1234567890
api_key = "LastFmApiKeyHere" api_key = "LastFmApiKeyHere"
api_secret = "LastFmApiSecretHere" api_secret = "LastFmApiSecretHere"

View file

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