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

Unwhitelist command #188

Merged
merged 6 commits into from
Dec 15, 2023
Merged
Changes from 2 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
49 changes: 48 additions & 1 deletion uqcsbot/minecraft.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,54 @@ async def mcwhitelist(self, interaction: discord.Interaction, username: str):

db_session.close()

@app_commands.command()
@app_commands.describe(username="Minecraft username to unwhitelist.")
@yelling_exemptor(input_args=["username"])
fattyhope marked this conversation as resolved.
Show resolved Hide resolved
async def mcunwhitelist(self, interaction: discord.Interaction, username: str):
"""Removes a username from the whitelist for the UQCS server."""
db_session = self.bot.create_db_session()
query = db_session.query(MCWhitelist).filter(
MCWhitelist.discord_id == interaction.user.id
)
is_user_admin = (
isinstance(interaction.user, Member)
and interaction.user.guild_permissions.manage_guild
)

# If the user has already whitelisted someone, and they aren't an admin deny it.
if not is_user_admin and query.count() > 0:
fattyhope marked this conversation as resolved.
Show resolved Hide resolved
await interaction.response.send_message(
"You've already whitelisted an account."
)
else:
# Send the RCON command to remove the user from the whitelist
response_remove = await self.send_rcon_command(
f"whitelist remove {username}"
)
logging.info(f"[MINECRAFT] whitelist remove {username}: {response_remove}")

# Send the RCON command to kick the player from the server
response_kick = await self.send_rcon_command(f"kick {username}")
logging.info(f"[MINECRAFT] kick {username}: {response_kick}")

# If the responses indicate successful removal and kick, remove the database item
if "Removed" in response_remove[0] and "Kicked" in response_kick[0]:
query.delete()
db_session.commit()

await self.bot.admin_alert(
title="Minecraft Server Unwhitelist",
description=f"{response_remove[0]}\n{response_kick[0]}",
footer=f"Action performed by {interaction.user}",
colour=Colour.red(), # Use red to indicate removal
)

await interaction.response.send_message(
fattyhope marked this conversation as resolved.
Show resolved Hide resolved
f"{response_remove[0]}\n{response_kick[0]}"
)

db_session.close()

mcadmin_group = app_commands.Group(
name="mcadmin", description="Commands for managing the UQCS Minecraft server"
)
Expand All @@ -119,7 +167,6 @@ async def mcadmin(self, interaction: discord.Interaction, command: str):
for split in split_response[1:]:
await interaction.followup.send(f"```{split}```")

# Just to be safe, send this to the admin log channel as well.
fattyhope marked this conversation as resolved.
Show resolved Hide resolved
await self.bot.admin_alert(
title="Minecraft Server Admin Command",
fields=[
Expand Down