Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add addusers command #27

Merged
merged 5 commits into from
Jul 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions src/bot/root_pythia_cogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ async def status(self, ctx):
rl_idle = check if rate_limiter.is_idle() else cross
# pylint: disable-next=no-member
bot_loop_alive = check if self.check_new_solves.is_running() else cross
await ctx.send(f"RootMe API's Rate Limiter status:\n"
f"- alive: {rl_alive}\n"
f" - paused: {rl_paused}\n"
f" - idle: {rl_idle}\n"
f"Bot's `check_new_solves` loop:\n"
f"- alive: {bot_loop_alive}\n")
await ctx.send(
f"RootMe API's Rate Limiter status:\n"
f"- alive: {rl_alive}\n"
f" - paused: {rl_paused}\n"
f" - idle: {rl_idle}\n"
f"Bot's `check_new_solves` loop:\n"
f"- alive: {bot_loop_alive}\n"
)

@commands.command(name="resume")
async def resume(self, ctx):
Expand All @@ -63,19 +65,45 @@ async def resume(self, ctx):
"Resumed successfully from idle state, requests can be sent again."
)

# TODO: add a add_users command that would accept a list of ids
@commands.before_invoke(log_command_call)
@commands.command(name="adduser")
async def adduser(self, ctx, idx: int):
async def base_add_one_user(self, ctx, idx: int):
# TODO: except 404 error -> if the request in add_user fails
# try:
# user = await self.dbmanager.add_user(idx)
# except 404Error as 404_err: # Error coming from add_user() method
# pass
user = await self.dbmanager.add_user(idx)

if user is None:
await ctx.message.channel.send(f"UserID {idx} already exists in database")
self.logger.warning("UserID '%s' already exists in database", idx)
return

self.logger.info("Add user '%s'", user)
await ctx.message.channel.send(f"{user} added!\nPoints: {user.score}")

@commands.before_invoke(log_command_call)
@commands.command(name="addusers")
async def addusers(self, ctx, *args):
self.logger.info("command `addusers` received '%s' arguments: '%s'", len(args), args)
for user_id in args:
try:
user_id = int(user_id)
except ValueError as value_err: # Error coming from the int() cast
self.logger.error("command `addusers` received: '%s'", value_err)
await ctx.message.channel.send(
f"invalid argument received: `{user_id}`; a UserId is expected, ignoring it..."
)
continue

# TODO: except 404 error -> if the request in add_user fails, we
# should continue here (ex: multiple users but only one can be wrong)
await self.base_add_one_user(ctx, user_id)

@commands.before_invoke(log_command_call)
@commands.command(name="adduser")
async def adduser(self, ctx, idx: int):
await self.base_add_one_user(ctx, idx)

@commands.before_invoke(log_command_call)
@commands.command(name="getuser")
async def getuser(self, ctx, idx: int):
Expand Down