diff --git a/discord_manage/.env.example b/discord_manage/.env.example new file mode 100644 index 0000000..5d8f5b2 --- /dev/null +++ b/discord_manage/.env.example @@ -0,0 +1,3 @@ +TOKEN=YOUR_BOT_TOKEN_HERE +PREFIX=YOUR_BOT_PREFIX_HERE +INVITE_LINK=YOUR_BOT_INVITE_LINK_HERE \ No newline at end of file diff --git a/discord_manage/bot.py b/discord_manage/bot.py new file mode 100644 index 0000000..b2c8fcf --- /dev/null +++ b/discord_manage/bot.py @@ -0,0 +1,281 @@ +import json +import logging +import os +import platform +import random +import sys + +import aiosqlite +import discord +from discord.ext import commands, tasks +from discord.ext.commands import Context +from dotenv import load_dotenv + +from database import DatabaseManager + +load_dotenv() + +""" +Setup bot intents (events restrictions) +For more information about intents, please go to the following websites: +https://discordpy.readthedocs.io/en/latest/intents.html +https://discordpy.readthedocs.io/en/latest/intents.html#privileged-intents + + +Default Intents: +intents.bans = True +intents.dm_messages = True +intents.dm_reactions = True +intents.dm_typing = True +intents.emojis = True +intents.emojis_and_stickers = True +intents.guild_messages = True +intents.guild_reactions = True +intents.guild_scheduled_events = True +intents.guild_typing = True +intents.guilds = True +intents.integrations = True +intents.invites = True +intents.messages = True # `message_content` is required to get the content of the messages +intents.reactions = True +intents.typing = True +intents.voice_states = True +intents.webhooks = True + +Privileged Intents (Needs to be enabled on developer portal of Discord), please use them only if you need them: +intents.members = True +intents.message_content = True +intents.presences = True +""" + +intents = discord.Intents.default() + +""" +Uncomment this if you want to use prefix (normal) commands. +It is recommended to use slash commands and therefore not use prefix commands. + +If you want to use prefix commands, make sure to also enable the intent below in the Discord developer portal. +""" +# intents.message_content = True + +# Setup both of the loggers + + +class LoggingFormatter(logging.Formatter): + # Colors + black = "\x1b[30m" + red = "\x1b[31m" + green = "\x1b[32m" + yellow = "\x1b[33m" + blue = "\x1b[34m" + gray = "\x1b[38m" + # Styles + reset = "\x1b[0m" + bold = "\x1b[1m" + + COLORS = { + logging.DEBUG: gray + bold, + logging.INFO: blue + bold, + logging.WARNING: yellow + bold, + logging.ERROR: red, + logging.CRITICAL: red + bold, + } + + def format(self, record): + log_color = self.COLORS[record.levelno] + format = "(black){asctime}(reset) (levelcolor){levelname:<8}(reset) (green){name}(reset) {message}" + format = format.replace("(black)", self.black + self.bold) + format = format.replace("(reset)", self.reset) + format = format.replace("(levelcolor)", log_color) + format = format.replace("(green)", self.green + self.bold) + formatter = logging.Formatter(format, "%Y-%m-%d %H:%M:%S", style="{") + return formatter.format(record) + + +logger = logging.getLogger("discord_bot") +logger.setLevel(logging.INFO) + +# Console handler +console_handler = logging.StreamHandler() +console_handler.setFormatter(LoggingFormatter()) +# File handler +file_handler = logging.FileHandler(filename="discord.log", encoding="utf-8", mode="w") +file_handler_formatter = logging.Formatter( + "[{asctime}] [{levelname:<8}] {name}: {message}", "%Y-%m-%d %H:%M:%S", style="{" +) +file_handler.setFormatter(file_handler_formatter) + +# Add the handlers +logger.addHandler(console_handler) +logger.addHandler(file_handler) + + +class DiscordBot(commands.Bot): + def __init__(self) -> None: + super().__init__( + command_prefix=commands.when_mentioned_or(os.getenv("PREFIX")), + intents=intents, + help_command=None, + ) + """ + This creates custom bot variables so that we can access these variables in cogs more easily. + + For example, The logger is available using the following code: + - self.logger # In this class + - bot.logger # In this file + - self.bot.logger # In cogs + """ + self.logger = logger + self.database = None + self.bot_prefix = os.getenv("PREFIX") + self.invite_link = os.getenv("INVITE_LINK") + + async def init_db(self) -> None: + async with aiosqlite.connect( + f"{os.path.realpath(os.path.dirname(__file__))}/database/database.db" + ) as db: + with open( + f"{os.path.realpath(os.path.dirname(__file__))}/database/schema.sql", + encoding = "utf-8" + ) as file: + await db.executescript(file.read()) + await db.commit() + + async def load_cogs(self) -> None: + """ + The code in this function is executed whenever the bot will start. + """ + for file in os.listdir(f"{os.path.realpath(os.path.dirname(__file__))}/cogs"): + if file.endswith(".py"): + extension = file[:-3] + try: + await self.load_extension(f"cogs.{extension}") + self.logger.info(f"Loaded extension '{extension}'") + except Exception as e: + exception = f"{type(e).__name__}: {e}" + self.logger.error( + f"Failed to load extension {extension}\n{exception}" + ) + + @tasks.loop(minutes=1.0) + async def status_task(self) -> None: + """ + Setup the game status task of the bot. + """ + statuses = ["with you!", "with Krypton!", "with humans!"] + await self.change_presence(activity=discord.Game(random.choice(statuses))) + + @status_task.before_loop + async def before_status_task(self) -> None: + """ + Before starting the status changing task, we make sure the bot is ready + """ + await self.wait_until_ready() + + async def setup_hook(self) -> None: + """ + This will just be executed when the bot starts the first time. + """ + self.logger.info(f"Logged in as {self.user.name}") + self.logger.info(f"discord.py API version: {discord.__version__}") + self.logger.info(f"Python version: {platform.python_version()}") + self.logger.info( + f"Running on: {platform.system()} {platform.release()} ({os.name})" + ) + self.logger.info("-------------------") + await self.init_db() + await self.load_cogs() + self.status_task.start() + self.database = DatabaseManager( + connection=await aiosqlite.connect( + f"{os.path.realpath(os.path.dirname(__file__))}/database/database.db" + ) + ) + + async def on_message(self, message: discord.Message) -> None: + """ + The code in this event is executed every time someone sends a message, with or without the prefix + + :param message: The message that was sent. + """ + if message.author == self.user or message.author.bot: + return + await self.process_commands(message) + + async def on_command_completion(self, context: Context) -> None: + """ + The code in this event is executed every time a normal command has been *successfully* executed. + + :param context: The context of the command that has been executed. + """ + full_command_name = context.command.qualified_name + split = full_command_name.split(" ") + executed_command = str(split[0]) + if context.guild is not None: + self.logger.info( + f"Executed {executed_command} command in {context.guild.name} (ID: {context.guild.id}) by {context.author} (ID: {context.author.id})" + ) + else: + self.logger.info( + f"Executed {executed_command} command by {context.author} (ID: {context.author.id}) in DMs" + ) + + async def on_command_error(self, context: Context, error) -> None: + """ + The code in this event is executed every time a normal valid command catches an error. + + :param context: The context of the normal command that failed executing. + :param error: The error that has been faced. + """ + if isinstance(error, commands.CommandOnCooldown): + minutes, seconds = divmod(error.retry_after, 60) + hours, minutes = divmod(minutes, 60) + hours = hours % 24 + embed = discord.Embed( + description=f"**Please slow down** - You can use this command again in {f'{round(hours)} hours' if round(hours) > 0 else ''} {f'{round(minutes)} minutes' if round(minutes) > 0 else ''} {f'{round(seconds)} seconds' if round(seconds) > 0 else ''}.", + color=0xE02B2B, + ) + await context.send(embed=embed) + elif isinstance(error, commands.NotOwner): + embed = discord.Embed( + description="You are not the owner of the bot!", color=0xE02B2B + ) + await context.send(embed=embed) + if context.guild: + self.logger.warning( + f"{context.author} (ID: {context.author.id}) tried to execute an owner only command in the guild {context.guild.name} (ID: {context.guild.id}), but the user is not an owner of the bot." + ) + else: + self.logger.warning( + f"{context.author} (ID: {context.author.id}) tried to execute an owner only command in the bot's DMs, but the user is not an owner of the bot." + ) + elif isinstance(error, commands.MissingPermissions): + embed = discord.Embed( + description="You are missing the permission(s) `" + + ", ".join(error.missing_permissions) + + "` to execute this command!", + color=0xE02B2B, + ) + await context.send(embed=embed) + elif isinstance(error, commands.BotMissingPermissions): + embed = discord.Embed( + description="I am missing the permission(s) `" + + ", ".join(error.missing_permissions) + + "` to fully perform this command!", + color=0xE02B2B, + ) + await context.send(embed=embed) + elif isinstance(error, commands.MissingRequiredArgument): + embed = discord.Embed( + title="Error!", + # We need to capitalize because the command arguments have no capital letter in the code and they are the first word in the error message. + description=str(error).capitalize(), + color=0xE02B2B, + ) + await context.send(embed=embed) + else: + raise error + + +bot = DiscordBot() +bot.run(os.getenv("TOKEN")) \ No newline at end of file diff --git a/discord_manage/cogs/fun.py b/discord_manage/cogs/fun.py new file mode 100644 index 0000000..c8a4bfa --- /dev/null +++ b/discord_manage/cogs/fun.py @@ -0,0 +1,155 @@ +import random + +import aiohttp +import discord +from discord.ext import commands +from discord.ext.commands import Context + + +class Choice(discord.ui.View): + def __init__(self) -> None: + super().__init__() + self.value = None + + @discord.ui.button(label="Heads", style=discord.ButtonStyle.blurple) + async def confirm( + self, interaction: discord.Interaction, button: discord.ui.Button + ) -> None: + self.value = "heads" + self.stop() + + @discord.ui.button(label="Tails", style=discord.ButtonStyle.blurple) + async def cancel( + self, interaction: discord.Interaction, button: discord.ui.Button + ) -> None: + self.value = "tails" + self.stop() + + +class RockPaperScissors(discord.ui.Select): + def __init__(self) -> None: + options = [ + discord.SelectOption( + label="Scissors", description="You choose scissors.", emoji="โœ‚" + ), + discord.SelectOption( + label="Rock", description="You choose rock.", emoji="๐Ÿชจ" + ), + discord.SelectOption( + label="Paper", description="You choose paper.", emoji="๐Ÿงป" + ), + ] + super().__init__( + placeholder="Choose...", + min_values=1, + max_values=1, + options=options, + ) + + async def callback(self, interaction: discord.Interaction) -> None: + choices = { + "rock": 0, + "paper": 1, + "scissors": 2, + } + user_choice = self.values[0].lower() + user_choice_index = choices[user_choice] + + bot_choice = random.choice(list(choices.keys())) + bot_choice_index = choices[bot_choice] + + result_embed = discord.Embed(color=0xBEBEFE) + result_embed.set_author( + name=interaction.user.name, icon_url=interaction.user.display_avatar.url + ) + + winner = (3 + user_choice_index - bot_choice_index) % 3 + if winner == 0: + result_embed.description = f"**That's a draw!**\nYou've chosen {user_choice} and I've chosen {bot_choice}." + result_embed.colour = 0xF59E42 + elif winner == 1: + result_embed.description = f"**You won!**\nYou've chosen {user_choice} and I've chosen {bot_choice}." + result_embed.colour = 0x57F287 + else: + result_embed.description = f"**You lost!**\nYou've chosen {user_choice} and I've chosen {bot_choice}." + result_embed.colour = 0xE02B2B + + await interaction.response.edit_message( + embed=result_embed, content=None, view=None + ) + + +class RockPaperScissorsView(discord.ui.View): + def __init__(self) -> None: + super().__init__() + self.add_item(RockPaperScissors()) + + +class Fun(commands.Cog, name="fun"): + def __init__(self, bot) -> None: + self.bot = bot + + @commands.hybrid_command(name="randomfact", description="Get a random fact.") + async def randomfact(self, context: Context) -> None: + """ + Get a random fact. + + :param context: The hybrid command context. + """ + # This will prevent your bot from stopping everything when doing a web request - see: https://discordpy.readthedocs.io/en/stable/faq.html#how-do-i-make-a-web-request + async with aiohttp.ClientSession() as session: + async with session.get( + "https://uselessfacts.jsph.pl/random.json?language=en" + ) as request: + if request.status == 200: + data = await request.json() + embed = discord.Embed(description=data["text"], color=0xD75BF4) + else: + embed = discord.Embed( + title="Error!", + description="There is something wrong with the API, please try again later", + color=0xE02B2B, + ) + await context.send(embed=embed) + + @commands.hybrid_command( + name="coinflip", description="Make a coin flip, but give your bet before." + ) + async def coinflip(self, context: Context) -> None: + """ + Make a coin flip, but give your bet before. + + :param context: The hybrid command context. + """ + buttons = Choice() + embed = discord.Embed(description="What is your bet?", color=0xBEBEFE) + message = await context.send(embed=embed, view=buttons) + await buttons.wait() # We wait for the user to click a button. + result = random.choice(["heads", "tails"]) + if buttons.value == result: + embed = discord.Embed( + description=f"Correct! You guessed `{buttons.value}` and I flipped the coin to `{result}`.", + color=0xBEBEFE, + ) + else: + embed = discord.Embed( + description=f"Woops! You guessed `{buttons.value}` and I flipped the coin to `{result}`, better luck next time!", + color=0xE02B2B, + ) + await message.edit(embed=embed, view=None, content=None) + + @commands.hybrid_command( + name="rps", description="Play the rock paper scissors game against the bot." + ) + async def rock_paper_scissors(self, context: Context) -> None: + """ + Play the rock paper scissors game against the bot. + + :param context: The hybrid command context. + """ + view = RockPaperScissorsView() + await context.send("Please make your choice", view=view) + + +async def setup(bot) -> None: + await bot.add_cog(Fun(bot)) \ No newline at end of file diff --git a/discord_manage/cogs/general.py b/discord_manage/cogs/general.py new file mode 100644 index 0000000..833628b --- /dev/null +++ b/discord_manage/cogs/general.py @@ -0,0 +1,319 @@ +import platform +import random + +import aiohttp +import discord +from discord import app_commands +from discord.ext import commands +from discord.ext.commands import Context + + +class FeedbackForm(discord.ui.Modal, title="Feeedback"): + feedback = discord.ui.TextInput( + label="What do you think about this bot?", + style=discord.TextStyle.long, + placeholder="Type your answer here...", + required=True, + max_length=256, + ) + + async def on_submit(self, interaction: discord.Interaction): + self.interaction = interaction + self.answer = str(self.feedback) + self.stop() + + +class General(commands.Cog, name="general"): + def __init__(self, bot) -> None: + self.bot = bot + self.context_menu_user = app_commands.ContextMenu( + name="Grab ID", callback=self.grab_id + ) + self.bot.tree.add_command(self.context_menu_user) + self.context_menu_message = app_commands.ContextMenu( + name="Remove spoilers", callback=self.remove_spoilers + ) + self.bot.tree.add_command(self.context_menu_message) + + # Message context menu command + async def remove_spoilers( + self, interaction: discord.Interaction, message: discord.Message + ) -> None: + """ + Removes the spoilers from the message. This command requires the MESSAGE_CONTENT intent to work properly. + + :param interaction: The application command interaction. + :param message: The message that is being interacted with. + """ + spoiler_attachment = None + for attachment in message.attachments: + if attachment.is_spoiler(): + spoiler_attachment = attachment + break + embed = discord.Embed( + title="Message without spoilers", + description=message.content.replace("||", ""), + color=0xBEBEFE, + ) + if spoiler_attachment is not None: + embed.set_image(url=attachment.url) + await interaction.response.send_message(embed=embed, ephemeral=True) + + # User context menu command + async def grab_id( + self, interaction: discord.Interaction, user: discord.User + ) -> None: + """ + Grabs the ID of the user. + + :param interaction: The application command interaction. + :param user: The user that is being interacted with. + """ + embed = discord.Embed( + description=f"The ID of {user.mention} is `{user.id}`.", + color=0xBEBEFE, + ) + await interaction.response.send_message(embed=embed, ephemeral=True) + + @commands.hybrid_command( + name="help", description="List all commands the bot has loaded." + ) + async def help(self, context: Context) -> None: + embed = discord.Embed( + title="Help", description="List of available commands:", color=0xBEBEFE + ) + for i in self.bot.cogs: + if i == "owner" and not (await self.bot.is_owner(context.author)): + continue + cog = self.bot.get_cog(i.lower()) + commands = cog.get_commands() + data = [] + for command in commands: + description = command.description.partition("\n")[0] + data.append(f"{command.name} - {description}") + help_text = "\n".join(data) + embed.add_field( + name=i.capitalize(), value=f"```{help_text}```", inline=False + ) + await context.send(embed=embed) + + @commands.hybrid_command( + name="botinfo", + description="Get some useful (or not) information about the bot.", + ) + async def botinfo(self, context: Context) -> None: + """ + Get some useful (or not) information about the bot. + + :param context: The hybrid command context. + """ + embed = discord.Embed( + description="Used [Krypton's](https://krypton.ninja) template", + color=0xBEBEFE, + ) + embed.set_author(name="Bot Information") + embed.add_field(name="Owner:", value="Krypton#7331", inline=True) + embed.add_field( + name="Python Version:", value=f"{platform.python_version()}", inline=True + ) + embed.add_field( + name="Prefix:", + value=f"/ (Slash Commands) or {self.bot.bot_prefix} for normal commands", + inline=False, + ) + embed.set_footer(text=f"Requested by {context.author}") + await context.send(embed=embed) + + @commands.hybrid_command( + name="serverinfo", + description="Get some useful (or not) information about the server.", + ) + async def serverinfo(self, context: Context) -> None: + """ + Get some useful (or not) information about the server. + + :param context: The hybrid command context. + """ + roles = [role.name for role in context.guild.roles] + num_roles = len(roles) + if num_roles > 50: + roles = roles[:50] + roles.append(f">>>> Displaying [50/{num_roles}] Roles") + roles = ", ".join(roles) + + embed = discord.Embed( + title="**Server Name:**", description=f"{context.guild}", color=0xBEBEFE + ) + if context.guild.icon is not None: + embed.set_thumbnail(url=context.guild.icon.url) + embed.add_field(name="Server ID", value=context.guild.id) + embed.add_field(name="Member Count", value=context.guild.member_count) + embed.add_field( + name="Text/Voice Channels", value=f"{len(context.guild.channels)}" + ) + embed.add_field(name=f"Roles ({len(context.guild.roles)})", value=roles) + embed.set_footer(text=f"Created at: {context.guild.created_at}") + await context.send(embed=embed) + + @commands.hybrid_command( + name="ping", + description="Check if the bot is alive.", + ) + async def ping(self, context: Context) -> None: + """ + Check if the bot is alive. + + :param context: The hybrid command context. + """ + embed = discord.Embed( + title="๐Ÿ“ Pong!", + description=f"The bot latency is {round(self.bot.latency * 1000)}ms.", + color=0xBEBEFE, + ) + await context.send(embed=embed) + + @commands.hybrid_command( + name="invite", + description="Get the invite link of the bot to be able to invite it.", + ) + async def invite(self, context: Context) -> None: + """ + Get the invite link of the bot to be able to invite it. + + :param context: The hybrid command context. + """ + embed = discord.Embed( + description=f"Invite me by clicking [here]({self.bot.invite_link}).", + color=0xD75BF4, + ) + try: + await context.author.send(embed=embed) + await context.send("I sent you a private message!") + except discord.Forbidden: + await context.send(embed=embed) + + @commands.hybrid_command( + name="server", + description="Get the invite link of the discord server of the bot for some support.", + ) + async def server(self, context: Context) -> None: + """ + Get the invite link of the discord server of the bot for some support. + + :param context: The hybrid command context. + """ + embed = discord.Embed( + description=f"Join the support server for the bot by clicking [here](https://discord.gg/mTBrXyWxAF).", + color=0xD75BF4, + ) + try: + await context.author.send(embed=embed) + await context.send("I sent you a private message!") + except discord.Forbidden: + await context.send(embed=embed) + + @commands.hybrid_command( + name="8ball", + description="Ask any question to the bot.", + ) + @app_commands.describe(question="The question you want to ask.") + async def eight_ball(self, context: Context, *, question: str) -> None: + """ + Ask any question to the bot. + + :param context: The hybrid command context. + :param question: The question that should be asked by the user. + """ + answers = [ + "It is certain.", + "It is decidedly so.", + "You may rely on it.", + "Without a doubt.", + "Yes - definitely.", + "As I see, yes.", + "Most likely.", + "Outlook good.", + "Yes.", + "Signs point to yes.", + "Reply hazy, try again.", + "Ask again later.", + "Better not tell you now.", + "Cannot predict now.", + "Concentrate and ask again later.", + "Don't count on it.", + "My reply is no.", + "My sources say no.", + "Outlook not so good.", + "Very doubtful.", + ] + embed = discord.Embed( + title="**My Answer:**", + description=f"{random.choice(answers)}", + color=0xBEBEFE, + ) + embed.set_footer(text=f"The question was: {question}") + await context.send(embed=embed) + + @commands.hybrid_command( + name="bitcoin", + description="Get the current price of bitcoin.", + ) + async def bitcoin(self, context: Context) -> None: + """ + Get the current price of bitcoin. + + :param context: The hybrid command context. + """ + # This will prevent your bot from stopping everything when doing a web request - see: https://discordpy.readthedocs.io/en/stable/faq.html#how-do-i-make-a-web-request + async with aiohttp.ClientSession() as session: + async with session.get( + "https://api.coindesk.com/v1/bpi/currentprice/BTC.json" + ) as request: + if request.status == 200: + data = await request.json() + embed = discord.Embed( + title="Bitcoin price", + description=f"The current price is {data['bpi']['USD']['rate']} :dollar:", + color=0xBEBEFE, + ) + else: + embed = discord.Embed( + title="Error!", + description="There is something wrong with the API, please try again later", + color=0xE02B2B, + ) + await context.send(embed=embed) + + @app_commands.command( + name="feedback", description="Submit a feedback for the owners of the bot" + ) + async def feedback(self, interaction: discord.Interaction) -> None: + """ + Submit a feedback for the owners of the bot. + + :param context: The hybrid command context. + """ + feedback_form = FeedbackForm() + await interaction.response.send_modal(feedback_form) + + await feedback_form.wait() + interaction = feedback_form.interaction + await interaction.response.send_message( + embed=discord.Embed( + description="Thank you for your feedback, the owners have been notified about it.", + color=0xBEBEFE, + ) + ) + + app_owner = (await self.bot.application_info()).owner + await app_owner.send( + embed=discord.Embed( + title="New Feedback", + description=f"{interaction.user} (<@{interaction.user.id}>) has submitted a new feedback:\n```\n{feedback_form.answer}\n```", + color=0xBEBEFE, + ) + ) + + +async def setup(bot) -> None: + await bot.add_cog(General(bot)) \ No newline at end of file diff --git a/discord_manage/cogs/moderation.py b/discord_manage/cogs/moderation.py new file mode 100644 index 0000000..6f053e4 --- /dev/null +++ b/discord_manage/cogs/moderation.py @@ -0,0 +1,367 @@ +import os +from datetime import datetime + +import discord +from discord import app_commands +from discord.ext import commands +from discord.ext.commands import Context + + +class Moderation(commands.Cog, name="moderation"): + def __init__(self, bot) -> None: + self.bot = bot + + @commands.hybrid_command( + name="kick", + description="Kick a user out of the server.", + ) + @commands.has_permissions(kick_members=True) + @commands.bot_has_permissions(kick_members=True) + @app_commands.describe( + user="The user that should be kicked.", + reason="The reason why the user should be kicked.", + ) + async def kick( + self, context: Context, user: discord.User, *, reason: str = "Not specified" + ) -> None: + """ + Kick a user out of the server. + + :param context: The hybrid command context. + :param user: The user that should be kicked from the server. + :param reason: The reason for the kick. Default is "Not specified". + """ + member = context.guild.get_member(user.id) or await context.guild.fetch_member( + user.id + ) + if member.guild_permissions.administrator: + embed = discord.Embed( + description="User has administrator permissions.", color=0xE02B2B + ) + await context.send(embed=embed) + else: + try: + embed = discord.Embed( + description=f"**{member}** was kicked by **{context.author}**!", + color=0xBEBEFE, + ) + embed.add_field(name="Reason:", value=reason) + await context.send(embed=embed) + try: + await member.send( + f"You were kicked by **{context.author}** from **{context.guild.name}**!\nReason: {reason}" + ) + except: + # Couldn't send a message in the private messages of the user + pass + await member.kick(reason=reason) + except: + embed = discord.Embed( + description="An error occurred while trying to kick the user. Make sure my role is above the role of the user you want to kick.", + color=0xE02B2B, + ) + await context.send(embed=embed) + + @commands.hybrid_command( + name="nick", + description="Change the nickname of a user on a server.", + ) + @commands.has_permissions(manage_nicknames=True) + @commands.bot_has_permissions(manage_nicknames=True) + @app_commands.describe( + user="The user that should have a new nickname.", + nickname="The new nickname that should be set.", + ) + async def nick( + self, context: Context, user: discord.User, *, nickname: str = None + ) -> None: + """ + Change the nickname of a user on a server. + + :param context: The hybrid command context. + :param user: The user that should have its nickname changed. + :param nickname: The new nickname of the user. Default is None, which will reset the nickname. + """ + member = context.guild.get_member(user.id) or await context.guild.fetch_member( + user.id + ) + try: + await member.edit(nick=nickname) + embed = discord.Embed( + description=f"**{member}'s** new nickname is **{nickname}**!", + color=0xBEBEFE, + ) + await context.send(embed=embed) + except: + embed = discord.Embed( + description="An error occurred while trying to change the nickname of the user. Make sure my role is above the role of the user you want to change the nickname.", + color=0xE02B2B, + ) + await context.send(embed=embed) + + @commands.hybrid_command( + name="ban", + description="Bans a user from the server.", + ) + @commands.has_permissions(ban_members=True) + @commands.bot_has_permissions(ban_members=True) + @app_commands.describe( + user="The user that should be banned.", + reason="The reason why the user should be banned.", + ) + async def ban( + self, context: Context, user: discord.User, *, reason: str = "Not specified" + ) -> None: + """ + Bans a user from the server. + + :param context: The hybrid command context. + :param user: The user that should be banned from the server. + :param reason: The reason for the ban. Default is "Not specified". + """ + member = context.guild.get_member(user.id) or await context.guild.fetch_member( + user.id + ) + try: + if member.guild_permissions.administrator: + embed = discord.Embed( + description="User has administrator permissions.", color=0xE02B2B + ) + await context.send(embed=embed) + else: + embed = discord.Embed( + description=f"**{member}** was banned by **{context.author}**!", + color=0xBEBEFE, + ) + embed.add_field(name="Reason:", value=reason) + await context.send(embed=embed) + try: + await member.send( + f"You were banned by **{context.author}** from **{context.guild.name}**!\nReason: {reason}" + ) + except: + # Couldn't send a message in the private messages of the user + pass + await member.ban(reason=reason) + except: + embed = discord.Embed( + title="Error!", + description="An error occurred while trying to ban the user. Make sure my role is above the role of the user you want to ban.", + color=0xE02B2B, + ) + await context.send(embed=embed) + + @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: + # Couldn't send a message in the private messages of the user + 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: + """ + Warns a user in his private messages. + + :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) + + @commands.hybrid_command( + name="purge", + description="Delete a number of messages.", + ) + @commands.has_guild_permissions(manage_messages=True) + @commands.bot_has_permissions(manage_messages=True) + @app_commands.describe(amount="The amount of messages that should be deleted.") + async def purge(self, context: Context, amount: int) -> None: + """ + Delete a number of messages. + + :param context: The hybrid command context. + :param amount: The number of messages that should be deleted. + """ + await context.send( + "Deleting messages..." + ) # Bit of a hacky way to make sure the bot responds to the interaction and doens't get a "Unknown Interaction" response + purged_messages = await context.channel.purge(limit=amount + 1) + embed = discord.Embed( + description=f"**{context.author}** cleared **{len(purged_messages)-1}** messages!", + color=0xBEBEFE, + ) + await context.channel.send(embed=embed) + + @commands.hybrid_command( + name="hackban", + description="Bans a user without the user having to be in the server.", + ) + @commands.has_permissions(ban_members=True) + @commands.bot_has_permissions(ban_members=True) + @app_commands.describe( + user_id="The user ID that should be banned.", + reason="The reason why the user should be banned.", + ) + async def hackban( + self, context: Context, user_id: str, *, reason: str = "Not specified" + ) -> None: + """ + Bans a user without the user having to be in the server. + + :param context: The hybrid command context. + :param user_id: The ID of the user that should be banned. + :param reason: The reason for the ban. Default is "Not specified". + """ + try: + await self.bot.http.ban(user_id, context.guild.id, reason=reason) + user = self.bot.get_user(int(user_id)) or await self.bot.fetch_user( + int(user_id) + ) + embed = discord.Embed( + description=f"**{user}** (ID: {user_id}) was banned by **{context.author}**!", + color=0xBEBEFE, + ) + embed.add_field(name="Reason:", value=reason) + await context.send(embed=embed) + except Exception: + embed = discord.Embed( + description="An error occurred while trying to ban the user. Make sure ID is an existing ID that belongs to a user.", + color=0xE02B2B, + ) + await context.send(embed=embed) + + @commands.hybrid_command( + name="archive", + description="Archives in a text file the last messages with a chosen limit of messages.", + ) + @commands.has_permissions(manage_messages=True) + @app_commands.describe( + limit="The limit of messages that should be archived.", + ) + async def archive(self, context: Context, limit: int = 10) -> None: + """ + Archives in a text file the last messages with a chosen limit of messages. This command requires the MESSAGE_CONTENT intent to work properly. + + :param limit: The limit of messages that should be archived. Default is 10. + """ + log_file = f"{context.channel.id}.log" + with open(log_file, "w", encoding="UTF-8") as f: + f.write( + f'Archived messages from: #{context.channel} ({context.channel.id}) in the guild "{context.guild}" ({context.guild.id}) at {datetime.now().strftime("%d.%m.%Y %H:%M:%S")}\n' + ) + async for message in context.channel.history( + limit=limit, before=context.message + ): + attachments = [] + for attachment in message.attachments: + attachments.append(attachment.url) + attachments_text = ( + f"[Attached File{'s' if len(attachments) >= 2 else ''}: {', '.join(attachments)}]" + if len(attachments) >= 1 + else "" + ) + f.write( + f"{message.created_at.strftime('%d.%m.%Y %H:%M:%S')} {message.author} {message.id}: {message.clean_content} {attachments_text}\n" + ) + f = discord.File(log_file) + await context.send(file=f) + os.remove(log_file) + + +async def setup(bot) -> None: + await bot.add_cog(Moderation(bot)) \ No newline at end of file diff --git a/discord_manage/cogs/owner.py b/discord_manage/cogs/owner.py new file mode 100644 index 0000000..11ab565 --- /dev/null +++ b/discord_manage/cogs/owner.py @@ -0,0 +1,212 @@ +import discord +from discord import app_commands +from discord.ext import commands +from discord.ext.commands import Context + + +class Owner(commands.Cog, name="owner"): + def __init__(self, bot) -> None: + self.bot = bot + + @commands.command( + name="sync", + description="Synchonizes the slash commands.", + ) + @app_commands.describe(scope="The scope of the sync. Can be `global` or `guild`") + @commands.is_owner() + async def sync(self, context: Context, scope: str) -> None: + """ + Synchonizes the slash commands. + + :param context: The command context. + :param scope: The scope of the sync. Can be `global` or `guild`. + """ + + if scope == "global": + await context.bot.tree.sync() + embed = discord.Embed( + description="Slash commands have been globally synchronized.", + color=0xBEBEFE, + ) + await context.send(embed=embed) + return + elif scope == "guild": + context.bot.tree.copy_global_to(guild=context.guild) + await context.bot.tree.sync(guild=context.guild) + embed = discord.Embed( + description="Slash commands have been synchronized in this guild.", + color=0xBEBEFE, + ) + await context.send(embed=embed) + return + embed = discord.Embed( + description="The scope must be `global` or `guild`.", color=0xE02B2B + ) + await context.send(embed=embed) + + @commands.command( + name="unsync", + description="Unsynchonizes the slash commands.", + ) + @app_commands.describe( + scope="The scope of the sync. Can be `global`, `current_guild` or `guild`" + ) + @commands.is_owner() + async def unsync(self, context: Context, scope: str) -> None: + """ + Unsynchonizes the slash commands. + + :param context: The command context. + :param scope: The scope of the sync. Can be `global`, `current_guild` or `guild`. + """ + + if scope == "global": + context.bot.tree.clear_commands(guild=None) + await context.bot.tree.sync() + embed = discord.Embed( + description="Slash commands have been globally unsynchronized.", + color=0xBEBEFE, + ) + await context.send(embed=embed) + return + elif scope == "guild": + context.bot.tree.clear_commands(guild=context.guild) + await context.bot.tree.sync(guild=context.guild) + embed = discord.Embed( + description="Slash commands have been unsynchronized in this guild.", + color=0xBEBEFE, + ) + await context.send(embed=embed) + return + embed = discord.Embed( + description="The scope must be `global` or `guild`.", color=0xE02B2B + ) + await context.send(embed=embed) + + @commands.hybrid_command( + name="load", + description="Load a cog", + ) + @app_commands.describe(cog="The name of the cog to load") + @commands.is_owner() + async def load(self, context: Context, cog: str) -> None: + """ + The bot will load the given cog. + + :param context: The hybrid command context. + :param cog: The name of the cog to load. + """ + try: + await self.bot.load_extension(f"cogs.{cog}") + except Exception: + embed = discord.Embed( + description=f"Could not load the `{cog}` cog.", color=0xE02B2B + ) + await context.send(embed=embed) + return + embed = discord.Embed( + description=f"Successfully loaded the `{cog}` cog.", color=0xBEBEFE + ) + await context.send(embed=embed) + + @commands.hybrid_command( + name="unload", + description="Unloads a cog.", + ) + @app_commands.describe(cog="The name of the cog to unload") + @commands.is_owner() + async def unload(self, context: Context, cog: str) -> None: + """ + The bot will unload the given cog. + + :param context: The hybrid command context. + :param cog: The name of the cog to unload. + """ + try: + await self.bot.unload_extension(f"cogs.{cog}") + except Exception: + embed = discord.Embed( + description=f"Could not unload the `{cog}` cog.", color=0xE02B2B + ) + await context.send(embed=embed) + return + embed = discord.Embed( + description=f"Successfully unloaded the `{cog}` cog.", color=0xBEBEFE + ) + await context.send(embed=embed) + + @commands.hybrid_command( + name="reload", + description="Reloads a cog.", + ) + @app_commands.describe(cog="The name of the cog to reload") + @commands.is_owner() + async def reload(self, context: Context, cog: str) -> None: + """ + The bot will reload the given cog. + + :param context: The hybrid command context. + :param cog: The name of the cog to reload. + """ + try: + await self.bot.reload_extension(f"cogs.{cog}") + except Exception: + embed = discord.Embed( + description=f"Could not reload the `{cog}` cog.", color=0xE02B2B + ) + await context.send(embed=embed) + return + embed = discord.Embed( + description=f"Successfully reloaded the `{cog}` cog.", color=0xBEBEFE + ) + await context.send(embed=embed) + + @commands.hybrid_command( + name="shutdown", + description="Make the bot shutdown.", + ) + @commands.is_owner() + async def shutdown(self, context: Context) -> None: + """ + Shuts down the bot. + + :param context: The hybrid command context. + """ + embed = discord.Embed(description="Shutting down. Bye! :wave:", color=0xBEBEFE) + await context.send(embed=embed) + await self.bot.close() + + @commands.hybrid_command( + name="say", + description="The bot will say anything you want.", + ) + @app_commands.describe(message="The message that should be repeated by the bot") + @commands.is_owner() + async def say(self, context: Context, *, message: str) -> None: + """ + The bot will say anything you want. + + :param context: The hybrid command context. + :param message: The message that should be repeated by the bot. + """ + await context.send(message) + + @commands.hybrid_command( + name="embed", + description="The bot will say anything you want, but within embeds.", + ) + @app_commands.describe(message="The message that should be repeated by the bot") + @commands.is_owner() + async def embed(self, context: Context, *, message: str) -> None: + """ + The bot will say anything you want, but using embeds. + + :param context: The hybrid command context. + :param message: The message that should be repeated by the bot. + """ + embed = discord.Embed(description=message, color=0xBEBEFE) + await context.send(embed=embed) + + +async def setup(bot) -> None: + await bot.add_cog(Owner(bot)) \ No newline at end of file diff --git a/discord_manage/cogs/template.py b/discord_manage/cogs/template.py new file mode 100644 index 0000000..9042b88 --- /dev/null +++ b/discord_manage/cogs/template.py @@ -0,0 +1,30 @@ +from discord.ext import commands +from discord.ext.commands import Context + + +# Here we name the cog and create a new class for the cog. +class Template(commands.Cog, name="template"): + def __init__(self, bot) -> None: + self.bot = bot + + # Here you can just add your own commands, you'll always need to provide "self" as first parameter. + + @commands.hybrid_command( + name="testcommand", + description="This is a testing command that does nothing.", + ) + async def testcommand(self, context: Context) -> None: + """ + This is a testing command that does nothing. + + :param context: The application command context. + """ + # Do your stuff here + + # Don't forget to remove "pass", I added this just because there's no content in the method. + pass + + +# And then we finally add the cog to the bot so that it can load, unload, reload and use it's content. +async def setup(bot) -> None: + await bot.add_cog(Template(bot)) \ No newline at end of file diff --git a/discord_manage/database/__init__.py b/discord_manage/database/__init__.py new file mode 100644 index 0000000..3bae6e9 --- /dev/null +++ b/discord_manage/database/__init__.py @@ -0,0 +1,88 @@ +import aiosqlite + + +class DatabaseManager: + def __init__(self, *, connection: aiosqlite.Connection) -> None: + self.connection = connection + + async def add_warn( + self, user_id: int, server_id: int, moderator_id: int, reason: str + ) -> int: + """ + This function will add a warn to the database. + + :param user_id: The ID of the user that should be warned. + :param reason: The reason why the user should be warned. + """ + rows = await self.connection.execute( + "SELECT id FROM warns WHERE user_id=? AND server_id=? ORDER BY id DESC LIMIT 1", + ( + user_id, + server_id, + ), + ) + async with rows as cursor: + result = await cursor.fetchone() + warn_id = result[0] + 1 if result is not None else 1 + await self.connection.execute( + "INSERT INTO warns(id, user_id, server_id, moderator_id, reason) VALUES (?, ?, ?, ?, ?)", + ( + warn_id, + user_id, + server_id, + moderator_id, + reason, + ), + ) + await self.connection.commit() + return warn_id + + async def remove_warn(self, warn_id: int, user_id: int, server_id: int) -> int: + """ + This function will remove a warn from the database. + + :param warn_id: The ID of the warn. + :param user_id: The ID of the user that was warned. + :param server_id: The ID of the server where the user has been warned + """ + await self.connection.execute( + "DELETE FROM warns WHERE id=? AND user_id=? AND server_id=?", + ( + warn_id, + user_id, + server_id, + ), + ) + await self.connection.commit() + rows = await self.connection.execute( + "SELECT COUNT(*) FROM warns WHERE user_id=? AND server_id=?", + ( + user_id, + server_id, + ), + ) + async with rows as cursor: + result = await cursor.fetchone() + return result[0] if result is not None else 0 + + async def get_warnings(self, user_id: int, server_id: int) -> list: + """ + This function will get all the warnings of a user. + + :param user_id: The ID of the user that should be checked. + :param server_id: The ID of the server that should be checked. + :return: A list of all the warnings of the user. + """ + rows = await self.connection.execute( + "SELECT user_id, server_id, moderator_id, reason, strftime('%s', created_at), id FROM warns WHERE user_id=? AND server_id=?", + ( + user_id, + server_id, + ), + ) + async with rows as cursor: + result = await cursor.fetchall() + result_list = [] + for row in result: + result_list.append(row) + return result_list \ No newline at end of file diff --git a/discord_manage/database/schema.sql b/discord_manage/database/schema.sql new file mode 100644 index 0000000..52b109e --- /dev/null +++ b/discord_manage/database/schema.sql @@ -0,0 +1,8 @@ +CREATE TABLE IF NOT EXISTS `warns` ( + `id` int(11) NOT NULL, + `user_id` varchar(20) NOT NULL, + `server_id` varchar(20) NOT NULL, + `moderator_id` varchar(20) NOT NULL, + `reason` varchar(255) NOT NULL, + `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP +); \ No newline at end of file diff --git a/discord_manage/requirements.txt b/discord_manage/requirements.txt new file mode 100644 index 0000000..3e17ae0 --- /dev/null +++ b/discord_manage/requirements.txt @@ -0,0 +1,4 @@ +aiohttp +aiosqlite +discord.py==2.7.1 +python-dotenv \ No newline at end of file