|
| 1 | +import discord |
| 2 | +from discord import app_commands |
| 3 | +from discord.ext import commands |
| 4 | +from loguru import logger |
| 5 | + |
| 6 | + |
| 7 | +class Jail(commands.Cog): |
| 8 | + def __init__(self, bot: commands.Bot) -> None: |
| 9 | + self.bot = bot |
| 10 | + |
| 11 | + @app_commands.checks.has_any_role("Admin", "Sr. Mod", "Mod", "Jr. Mod") |
| 12 | + @app_commands.command(name="jail", description="Strip all roles except the jail role") |
| 13 | + @app_commands.describe(member="Which member to jail", reason="Reason for jail") |
| 14 | + async def jail( |
| 15 | + self, |
| 16 | + interaction: discord.Interaction, |
| 17 | + member: discord.Member, |
| 18 | + reason: str | None = None, |
| 19 | + ) -> None: |
| 20 | + logger.info(f"{interaction.user} jailed {member.display_name} in {interaction.channel}") |
| 21 | + |
| 22 | + if interaction.guild: |
| 23 | + jail_role = interaction.guild.get_role(1225848510008524851) |
| 24 | + if jail_role: |
| 25 | + response = await self.execute_jail( |
| 26 | + interaction=interaction, |
| 27 | + member=member, |
| 28 | + jail_role=jail_role, |
| 29 | + reason=reason or "None provided", |
| 30 | + ) |
| 31 | + |
| 32 | + await interaction.response.send_message(embed=response) |
| 33 | + |
| 34 | + async def execute_jail( |
| 35 | + self, |
| 36 | + interaction: discord.Interaction, |
| 37 | + member: discord.Member, |
| 38 | + jail_role: discord.Role, |
| 39 | + reason: str | None = None, |
| 40 | + ) -> discord.Embed: |
| 41 | + try: |
| 42 | + await member.edit(roles=[jail_role]) |
| 43 | + embed = discord.Embed( |
| 44 | + title=f"Jailed {member.display_name}!", |
| 45 | + color=discord.Colour.gold(), |
| 46 | + description=f"Reason: `{reason}`", |
| 47 | + ) |
| 48 | + embed.set_footer( |
| 49 | + text=f"Jailed by {interaction.user.display_name}", |
| 50 | + icon_url=interaction.user.display_avatar.url, |
| 51 | + ) |
| 52 | + |
| 53 | + logger.info(f"Successfully Jailed {member.display_name}.") |
| 54 | + |
| 55 | + except discord.errors.Forbidden as error: |
| 56 | + embed = discord.Embed( |
| 57 | + title=f"Failed to jail {member.display_name}", |
| 58 | + color=discord.Colour.red(), |
| 59 | + description=f"Insufficient permissions. Error Info: `{error}`", |
| 60 | + ) |
| 61 | + logger.error(f"Failed to jail {member.display_name}. Error: {error}") |
| 62 | + |
| 63 | + return embed |
| 64 | + |
| 65 | + |
| 66 | +async def setup(bot: commands.Bot) -> None: |
| 67 | + await bot.add_cog(Jail(bot)) |
0 commit comments