Skip to content

Commit

Permalink
Fix session management for !unrestrict
Browse files Browse the repository at this point in the history
  • Loading branch information
Lekuruu committed Feb 24, 2024
1 parent 77caa02 commit 6508872
Showing 1 changed file with 54 additions and 53 deletions.
107 changes: 54 additions & 53 deletions app/commands/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,64 +74,65 @@ async def unrestrict(context: Context):
)
return

if context.args[0].isnumeric():
# Get internal user id
discord_id = None
user = users.fetch_by_id(int(context.args[0]))

elif context.args[0].startswith("<@"):
# Get discord id
discord_id = int(context.args[0][2:-1])
user = users.fetch_by_discord_id(discord_id)

else:
await context.message.channel.send(
f'Invalid syntax: `!{context.command} <user_id/mention> <reason>`',
reference=context.message,
mention_author=True
)
return

if not user:
await context.message.channel.send(
f'User not found!',
reference=context.message,
mention_author=True
)
return
with app.session.database.managed_session() as session:
if context.args[0].isnumeric():
# Get internal user id
discord_id = None
user = users.fetch_by_id(int(context.args[0]), session=session)

elif context.args[0].startswith("<@"):
# Get discord id
discord_id = int(context.args[0][2:-1])
user = users.fetch_by_discord_id(discord_id, session=session)

else:
await context.message.channel.send(
f'Invalid syntax: `!{context.command} <user_id/mention> <reason>`',
reference=context.message,
mention_author=True
)
return

if not user:
await context.message.channel.send(
f'User not found!',
reference=context.message,
mention_author=True
)
return

if not user.restricted:
await context.message.channel.send(
f'User is already unrestricted!',
reference=context.message,
mention_author=True
)
return

# Restore scores
try:
scores.restore_hidden_scores(user.id, session=session)
stats.restore(user.id, session=session)
except Exception as e:
app.session.logger.error(
f'Failed to restore scores of player "{user.name}": {e}',
exc_info=e
)
await context.message.reply("Failed to restore scores!")

# Unrestrict HWID
clients.update_all(user.id, {'banned': False}, session=session)
users.update(user.id, {'restricted': False}, session=session)

# Add user to players & supporters group
groups.create_entry(user.id, 999, session=session)
groups.create_entry(user.id, 1000, session=session)

if not user.restricted:
await context.message.channel.send(
f'User is already unrestricted!',
f'User unrestricted.',
reference=context.message,
mention_author=True
)
return

# Restore scores
try:
scores.restore_hidden_scores(user.id)
stats.restore(user.id)
except Exception as e:
app.session.logger.error(
f'Failed to restore scores of player "{user.name}": {e}',
exc_info=e
)
await context.message.reply("Failed to restore scores!")

# Unrestrict HWID
clients.update_all(user.id, {'banned': False})
users.update(user.id, {'restricted': False, 'permissions': 5})

# Add user to players & supporters group
groups.create_entry(user.id, 999)
groups.create_entry(user.id, 1000)

await context.message.channel.send(
f'User unrestricted.',
reference=context.message,
mention_author=True
)

@app.session.commands.register(['rename'], roles=['Admin', 'GMT'])
async def rename(context: Context):
Expand Down

0 comments on commit 6508872

Please sign in to comment.