diff --git a/bot.py b/bot.py index ee70bff..57cf21c 100644 --- a/bot.py +++ b/bot.py @@ -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,12 @@ 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 async def init_db(self): print("Initializing database...") diff --git a/commands/admin.py b/commands/admin.py new file mode 100644 index 0000000..9d163c3 --- /dev/null +++ b/commands/admin.py @@ -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}`")) diff --git a/config-template.toml b/config-template.toml index 83a0744..cdc4798 100644 --- a/config-template.toml +++ b/config-template.toml @@ -1,3 +1,4 @@ token = "NerimityTokenHere" +owner_id = 1234567890 api_key = "LastFmApiKeyHere" api_secret = "LastFmApiSecretHere" diff --git a/main.py b/main.py index 60172fe..1bfd09b 100644 --- a/main.py +++ b/main.py @@ -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'] )