From 3cc856bc7e9fb1e67a459f8639dcbc7b70b8df18 Mon Sep 17 00:00:00 2001 From: Jackie <108224888+fattyhope@users.noreply.github.com> Date: Fri, 15 Dec 2023 18:55:23 +1000 Subject: [PATCH] Unwhitelist command (#188) * New command which unwhitelists user from a server, kicking them off instantly * Refactored code so it pases code style check * Fixed dodgy check, removed yelling and changd response format * Removed unused query variable and refactored its related code * Refactored code running the command: poetry run black uqcsbot * Removed comment previously used for debugging --- uqcsbot/minecraft.py | 45 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/uqcsbot/minecraft.py b/uqcsbot/minecraft.py index 76c8a64..da5d121 100644 --- a/uqcsbot/minecraft.py +++ b/uqcsbot/minecraft.py @@ -93,6 +93,51 @@ async def mcwhitelist(self, interaction: discord.Interaction, username: str): db_session.close() + @app_commands.command() + @app_commands.describe(username="Minecraft username to unwhitelist.") + 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() + 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: + 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, remove from the database item + if "Removed" in response_remove[0]: + db_session.query(MCWhitelist).filter( + MCWhitelist.mc_username == username + ).delete() + db_session.commit() + + await self.bot.admin_alert( + title="Minecraft Server Unwhitelist", + description=response_remove[0], + footer=f"Action performed by {interaction.user}", + colour=Colour.red(), + ) + + # Display the response to the user in Discord + await interaction.response.send_message(response_remove[0]) + + db_session.close() + mcadmin_group = app_commands.Group( name="mcadmin", description="Commands for managing the UQCS Minecraft server" )