""" Copyright © Krypton 2019-Present - https://github.com/kkrypt0nn (https://krypton.ninja) Description: 🐍 A simple template to start to code your own and personalized Discord bot in Python Version: 6.4.0 """ import discord from discord import app_commands from discord.ext import commands from discord.ext.commands import Context class Warnings(commands.Cog, name="warnings"): def __init__(self, bot) -> None: self.bot = bot @commands.hybrid_group( name="warning", description="Manage warnings of a user on a server.", ) @commands.has_permissions(manage_messages=True) async def warning(self, context: Context) -> None: """ Manage warnings of a user on a server. :param context: The hybrid command context. """ if context.invoked_subcommand is None: embed = discord.Embed( description="Please specify a subcommand.\n\n**Subcommands:**\n`add` - Add a warning to a user.\n`remove` - Remove a warning from a user.\n`list` - List all warnings of a user.", color=0xE02B2B, ) await context.send(embed=embed) @warning.command( name="add", description="Adds a warning to a user in the server.", ) @commands.has_permissions(manage_messages=True) @app_commands.describe( user="The user that should be warned.", reason="The reason why the user should be warned.", ) async def warning_add( self, context: Context, user: discord.User, *, reason: str = "Not specified" ) -> None: """ Warns a user in his private messages. :param context: The hybrid command context. :param user: The user that should be warned. :param reason: The reason for the warn. Default is "Not specified". """ member = context.guild.get_member(user.id) or await context.guild.fetch_member( user.id ) total = await self.bot.database.add_warn( user.id, context.guild.id, context.author.id, reason ) embed = discord.Embed( description=f"**{member}** was warned by **{context.author}**!\nTotal warns for this user: {total}", color=0xBEBEFE, ) embed.add_field(name="Reason:", value=reason) await context.send(embed=embed) try: await member.send( f"You were warned by **{context.author}** in **{context.guild.name}**!\nReason: {reason}" ) except: await context.send( f"{member.mention}, you were warned by **{context.author}**!\nReason: {reason}" ) @warning.command( name="remove", description="Removes a warning from a user in the server.", ) @commands.has_permissions(manage_messages=True) @app_commands.describe( user="The user that should get their warning removed.", warn_id="The ID of the warning that should be removed.", ) async def warning_remove( self, context: Context, user: discord.User, warn_id: int ) -> None: """ Removes a warning from a user. :param context: The hybrid command context. :param user: The user that should get their warning removed. :param warn_id: The ID of the warning that should be removed. """ member = context.guild.get_member(user.id) or await context.guild.fetch_member( user.id ) total = await self.bot.database.remove_warn(warn_id, user.id, context.guild.id) embed = discord.Embed( description=f"I've removed the warning **#{warn_id}** from **{member}**!\nTotal warns for this user: {total}", color=0xBEBEFE, ) await context.send(embed=embed) @warning.command( name="list", description="Shows the warnings of a user in the server.", ) @commands.has_guild_permissions(manage_messages=True) @app_commands.describe(user="The user you want to get the warnings of.") async def warning_list(self, context: Context, user: discord.User) -> None: """ Shows the warnings of a user in the server. :param context: The hybrid command context. :param user: The user you want to get the warnings of. """ warnings_list = await self.bot.database.get_warnings(user.id, context.guild.id) embed = discord.Embed(title=f"Warnings of {user}", color=0xBEBEFE) description = "" if len(warnings_list) == 0: description = "This user has no warnings." else: for warning in warnings_list: description += f"• Warned by <@{warning[2]}>: **{warning[3]}** () - Warn ID #{warning[5]}\n" embed.description = description await context.send(embed=embed) async def setup(bot) -> None: await bot.add_cog(Warnings(bot))