diff options
author | NatrixAeria <upezu@student.kit.edu> | 2020-10-18 21:39:42 +0200 |
---|---|---|
committer | NatrixAeria <upezu@student.kit.edu> | 2020-10-18 21:39:42 +0200 |
commit | 329ef7561eb70e9aa57474e4bf8d1c3bfb3b88b4 (patch) | |
tree | 745e8183a82c91885f05e9e163330c02640e4e2b | |
parent | aa929d2a43a45e6d775af09550c0d9ac1417510b (diff) |
Add a tutor list
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | bot.py | 20 | ||||
-rw-r--r-- | commands.py | 46 | ||||
-rw-r--r-- | config.py | 1 |
4 files changed, 68 insertions, 0 deletions
@@ -1,3 +1,4 @@ .env .idea/ __pycache__ +tutors @@ -19,6 +19,18 @@ class Cupido(commands.Bot): self.task = None self.reaction_map = {} self.vote_map = {} + self.read_file() + + def read_file(self): + try: + with open(config.TUTOR_FILE_PATH, 'r') as f: + self.tutors = {int(line) for line in f.read().split('\n')} + except FileNotFoundError: + self.tutors = set() + + def write_file(self): + with open(config.TUTOR_FILE_PATH, 'w') as f: + f.write('\n'.join(str(t) for t in self.tutors)) async def await_coroutine(self, co): if self.task is not None: @@ -160,6 +172,14 @@ class Cupido(commands.Bot): selected=vote)[0] return await message.edit(embed=embeds[0]) + def add_tutor(self, tutor): + self.tutors.add(tutor) + self.write_file() + + def remove_tutor(self, tutor): + self.tutors.remove(tutor) + self.write_file() + def main(): token = getenv(config.TOKEN_ENV_VAR) diff --git a/commands.py b/commands.py index 8e673c2..0a10b7f 100644 --- a/commands.py +++ b/commands.py @@ -136,5 +136,51 @@ async def loop(ctx: commands.Context, n=config.DEFAULT_LOOP_COUNT, t=config.DEFA if not result: break +@commands.command( + help='add a tutor', + aliases=('tutor', 'hello') +) +async def add(ctx: commands.Context, id=None): + if id is None: + return await answer(ctx, 'error: expecting user id') + try: + id = int(id) + except ValueError: + return await answer(ctx, 'error: invalid user id') + ctx.bot.add_tutor(id) + await answer(ctx, f'added user id {id} to the tutor list') + +@commands.command( + help='remove a tutor', + aliases=('rm', 'del', 'delete', 'byebye', 'bb') +) +async def remove(ctx: commands.Context, id=None): + if id is None: + return await answer(ctx, 'error: expecting user id') + try: + id = int(id) + except ValueError: + return await answer(ctx, 'error: invalid user id') + ctx.bot.remove_tutor(id) + await answer(ctx, f'romved user id {id} from the tutor list') + + +@commands.command( + help='list all tutors', + aliases=('tutors',) +) +async def list(ctx: commands.Context): + if ctx.bot.tutors: + futures = [] + for tutor in ctx.bot.tutors: + try: + user = ctx.bot.get_user(tutor) or await ctx.bot.fetch_user(tutor) + futures.append(answer(ctx, f' • {user} ({tutor})')) + except Exception as e: + futures.append(await answer(ctx, f' • *unknown* ({tutor})')) + await await_n(futures) + else: + await answer(ctx, 'there is no tutor :/') + bot_commands = [cmd for cmd in locals().values() if isinstance(cmd, commands.Command)] @@ -1,5 +1,6 @@ NAME = 'cupido' TOKEN_ENV_VAR = 'DISCORD_TOKEN' +TUTOR_FILE_PATH = 'tutors' COMMAND_PREFIX = '!<3 ' |