Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
3.1 kB
2
Indexable
import discord
from discord import app_commands
from discord.ext import commands
from Cogs.Administrative.DataBase.DataBase import Database  # Adjust the import path accordingly

class Warn(commands.Cog):
    def __init__(self, bot, db: Database):
        self.bot = bot
        self.db = db

    @app_commands.command(name="warn", description="Warn a Member")
    @app_commands.default_permissions(kick_members=True)
    @app_commands.describe(member="Mention the member", reason="What's the reason?")
    async def warn(self, interaction: discord.Interaction, member: discord.Member, reason: str):
        guild_id = interaction.guild.id
        channel_id = 1269443871100895373
        channel = interaction.guild.get_channel(channel_id)
        EmbedCogs = self.bot.get_cog("Embeds")
        try:
            # Get a connection from the pool
            async with self.db.get_connection() as connection:
                async with connection.cursor() as cursor:
                    # Fetch current warns
                    fetch_warns_query = f"""
                        SELECT Warns FROM G_{guild_id} WHERE User = %s;
                    """
                    await cursor.execute(fetch_warns_query, (member.id,))
                    result = await cursor.fetchone()

                    if result:
                        warns = result[0]
                    else:
                        warns = 0  # Default to 0 if no record is found

                    print(f"User '{member}' has {warns} warnings.")

                    # Insert new warning count
                    insert_warn = f"""
                        INSERT INTO G_{guild_id} (User, Warns)
                        VALUES (%s, %s)
                        ON DUPLICATE KEY UPDATE Warns = VALUES(Warns);
                    """
                    await cursor.execute(insert_warn, (member.id, warns + 1))
                    await connection.commit()

            # Create and send embed messages
            WarnedEmbed = EmbedCogs.basicEmbed(interaction, member, reason)
            WarnedEmbed.title = "Administrative Department"
            WarnedEmbed.color = 0x57F2
            WarnedEmbed.add_field(name=f"{interaction.user.name} Warned <{member}> Reason: {reason}", value="", inline=False)

            reciveKick_embed = EmbedCogs.basicEmbed(interaction, member, reason)
            reciveKick_embed.add_field(name=f"{interaction.user.name} Warned you - Reason: {reason}. You have {warns + 1} / 3 warnings.", value="", inline=False)

            await member.send(embed=reciveKick_embed)
            await interaction.response.send_message(embed=WarnedEmbed, delete_after=10.0)
            await channel.send(embed=WarnedEmbed)

        except Exception as e:
            print(f"Unexpected error: {e}")

async def setup(bot):
    # Initialize your Database connection here
    db = Database(host='host', user='user', password='pass', database='db')
    await db.connect()  # Ensure to connect the database pool
    await bot.add_cog(Warn(bot, db))
Leave a Comment