aboutsummaryrefslogtreecommitdiff
path: root/code/cogs/warnings.py
blob: f9d84bc47bc5970f41cdbbfc12761e97a31bddb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import discord
from discord.ext import commands
import datetime
from discord import app_commands

from global_variables import CONNECTION, BOT_COLOR


class Warnings(commands.Cog):
    def __init__(self, bot):
        self.bot = bot


    @app_commands.default_permissions(manage_messages=True)
    @app_commands.command()
    @app_commands.checks.has_permissions(manage_messages=True)
    @app_commands.describe(member='The member you would like to warn')
    @app_commands.describe(reason='Reason for warning the member')
    async def warn(
        self,
        interaction: discord.Interaction,
        member: discord.Member,
        reason: str
    ):
        "Warn a member for a given reason"
        guild_id = interaction.user.guild.id
        user_id = member.id
        warn_id = interaction.id
        warn_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        warned_by = interaction.user.id

        cur = CONNECTION.cursor()
        cur.execute("INSERT INTO warnings (warn_id, guild_id, user_id, warning, warn_time, warned_by) VALUES (%s, %s, %s, %s, %s, %s)", (warn_id, guild_id, user_id, reason, warn_time, warned_by))
        CONNECTION.commit()

        embed = discord.Embed(
            title=f"`{member.name}#{member.discriminator}` Has Been Warned in {interaction.guild}",
            description=f"Reason: {reason}",
            color=discord.Color.orange()
        )
        embed.set_footer(text=datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%d %H:%M:%S')+" UTC")
        await interaction.response.send_message(embed=embed)


    @app_commands.default_permissions(manage_messages=True)
    @app_commands.command()
    @app_commands.checks.has_permissions(manage_messages=True)
    @app_commands.describe(member='The member whose warnings you want to see')
    async def warnings(
        self,
        interaction: discord.Interaction,
        member: discord.Member
    ):
        "Show all warnings for a given user"
        guild_id = interaction.user.guild.id
        user_id = member.id

        cur = CONNECTION.cursor()
        cur.execute("SELECT * FROM warnings WHERE guild_id = %s AND user_id = %s", (guild_id, user_id))
        data = cur.fetchall()

        if data:
            embed=discord.Embed(
                title=f"Warnings for `{member.name}#{member.discriminator}` in {interaction.guild}",
                description="",
                color=discord.Color.orange()
            )
            for entry in data:
                embed.description += f"**Reason - `\"{entry[3]}\"` | ID: `{entry[0]}`**\nWarned By: <@{entry[5]}> | Date: {entry[4]}\n\n"

            embed.set_footer(text=datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%d %H:%M:%S')+" UTC")
            await interaction.response.send_message(embed=embed)

        else:
            embed = discord.Embed(
                title="No Warnings",
                description=f"{member.mention} has not been warned in the past, or all of their warnings have been deleted.",
                color=BOT_COLOR
            )
            embed.set_footer(text=datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%d %H:%M:%S')+" UTC")
            await interaction.response.send_message(embed=embed)


    @app_commands.default_permissions(manage_messages=True)
    @app_commands.command()
    @app_commands.checks.has_permissions(manage_messages=True)
    @app_commands.describe(id='ID of the warning you would like to delete')
    async def delwarn(
        self,
        interaction: discord.Interaction,
        id: str
    ):
        "Delete a warning from a user with the warning ID"
        cur = CONNECTION.cursor()
        cur.execute("SELECT warn_id FROM warnings WHERE warn_id = %s", (id,))
        data = cur.fetchone()

        if data:
            cur.execute("DELETE FROM warnings WHERE warn_id = %s", (id,))
            CONNECTION.commit()

            embed = discord.Embed(
                title="Warning Deleted",
                description=f"{interaction.user.mention} has deleted the warning identified by `{id}` .",
                color=discord.Color.orange()
            )
            embed.set_footer(text=datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%d %H:%M:%S')+" UTC")
            await interaction.response.send_message(embed=embed)

        else:
            embed = discord.Embed(
                title="Invalid ID",
                description="That ID is not associated with any warnings in this server.",
                color=BOT_COLOR
            )
            embed.set_footer(text=datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%d %H:%M:%S')+" UTC")
            await interaction.response.send_message(embed=embed, ephemeral=True)


async def setup(bot):
    await bot.add_cog(Warnings(bot))