Skip to content

Commit

Permalink
Add more moderation commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Zingzy committed Apr 12, 2024
1 parent 33616c1 commit b32dffd
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 30 deletions.
210 changes: 182 additions & 28 deletions cogs/moderation_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from utils import *
from constants import *
from api import app

import aiohttp

class moderation(commands.Cog):
def __init__(self, bot):
Expand All @@ -28,6 +28,7 @@ async def ban(self, interaction: discord.Interaction, user: discord.Member, *, r
title="User Banned",
description=f"{user.mention} was banned from the server",
color=discord.Color.brand_red(),
timestamp=datetime.datetime.now(datetime.timezone.utc)
)

embed.add_field(name=f"User Id `{user.id}`", value="** **", inline=False)
Expand All @@ -39,7 +40,7 @@ async def ban(self, interaction: discord.Interaction, user: discord.Member, *, r
except:
embed.set_thumbnail(url=user.default_avatar.url)

embed.set_footer(text=f"{user.name} Banned at `{datetime.datetime.utcnow()}`")
embed.set_footer(text=f"{user.name} Banned")

await interaction.response.send_message(embed=embed)

Expand All @@ -55,6 +56,7 @@ async def unban(self, interaction, user: str):
title="User Unbanned",
description=f"{user.mention} was unbanned from the server",
color=discord.Color.green(),
timestamp=datetime.datetime.now(datetime.timezone.utc)
)

embed.add_field(name=f"User Id `{user.id}`", value="** **", inline=False)
Expand All @@ -65,7 +67,7 @@ async def unban(self, interaction, user: str):
except:
embed.set_thumbnail(url=user.default_avatar.url)

embed.set_footer(text=f"{user.name} Unbanned at `{datetime.datetime.utcnow()}`")
embed.set_footer(text=f"{user.name} Unbanned")

await interaction.response.send_message(embed=embed)

Expand All @@ -81,6 +83,7 @@ async def kick(self, interaction: discord.Interaction, user: discord.Member, *,
title="User Kicked",
description=f"{user.mention} was kicked from the server",
color=discord.Color.orange(),
timestamp=datetime.datetime.now(datetime.timezone.utc)
)

embed.add_field(name=f"User Id `{user.id}`", value="** **", inline=False)
Expand All @@ -92,20 +95,35 @@ async def kick(self, interaction: discord.Interaction, user: discord.Member, *,
except:
embed.set_thumbnail(url=user.default_avatar.url)

embed.set_footer(text=f"{user.name} Kicked at {datetime.datetime.utcnow()}")
embed.set_footer(text=f"{user.name} Kicked")

await interaction.response.send_message(embed=embed)

@app_commands.command(name="purge", description="Purges a certain amount of messages")
@app_commands.guild_only()
@app_commands.default_permissions(administrator=True)
@app_commands.describe(amount = "The amount of messages to purge")
async def purge(self, ctx, amount: int):
if ctx.author.guild_permissions.manage_messages:
await ctx.channel.purge(limit=amount)
await ctx.send(f"Purged {amount} messages")
else:
await ctx.send("You don't have the permissions to do that!")
@app_commands.describe(amount = "The amount of messages to purge", channel="The channel to purge the messages from")
async def purge(self, interaction:discord.Interaction, amount: int, channel: discord.TextChannel = None):

if not interaction.user.guild_permissions.manage_messages:
await interaction.response.send_message(embed=discord.Embed(description="You don't have the permissions to do that!", color=discord.Color.red()), ephemeral=True)
return

await interaction.response.defer()

if channel is None:
channel = interaction.channel

await channel.purge(limit=amount)
embed = discord.Embed(
title="Purge",
description=f"Purged {amount} messages",
color=discord.Color.green(),
timestamp=datetime.datetime.now(datetime.timezone.utc)
)
embed.add_field(name="Channel", value=channel.mention, inline=False)
embed.set_footer(text=f"Purged {amount} messages")
await interaction.followup.send(embed=embed)

@app_commands.command(name="slowmode", description="Sets a slowmode for a channel")
@app_commands.guild_only()
Expand All @@ -129,22 +147,13 @@ async def slowmode(self, interaction, time: int, channel: discord.TextChannel =
title="Slowmode Set",
description=f"Set the slowmode to `{time} seconds`",
color=discord.Color.green(),
timestamp=datetime.datetime.now(datetime.timezone.utc)
)
embed.set_footer(text=f"Slowmode set at {datetime.datetime.utcnow()}")
embed.set_footer(text=f"Slowmode set")
embed.add_field(name="Channel", value=channel.mention, inline=False)

await interaction.followup.send(embed=embed)

# @app_commands.guild_only()
# @app_commands.default_permissions(administrator=True)
# async def nuke(self, ctx):
# if ctx.author.guild_permissions.manage_channels:
# channel = ctx.channel
# new_channel = await channel.clone()
# await channel.delete()
# await new_channel.send("This channel has been nuked!")
# else:
# await ctx.send("You don't have the permissions to do that!")

@app_commands.command(name="add-role", description="Adds a role to a user")
@app_commands.guild_only()
Expand All @@ -156,12 +165,13 @@ async def add_role(self, interaction, user: discord.Member, role: discord.Role):
title="Role Added",
description=f"Added the role {role.mention} to {user.mention}",
color=discord.Color.green(),
timestamp=datetime.datetime.now(datetime.timezone.utc)
)
try:
embed.set_thumbnail(url=user.avatar.url)
except:
embed.set_thumbnail(url=user.default_avatar.url)
embed.set_footer(text=f"Role added at {datetime.datetime.utcnow()}")
embed.set_footer(text=f"Role added")
await interaction.response.send_message(embed=embed)


Expand All @@ -176,12 +186,13 @@ async def removerole(self, interaction, user: discord.Member, role: discord.Role
title="Role Removed",
description=f"Removed the role {role.mention} from {user.mention}",
color=discord.Color.red(),
timestamp=datetime.datetime.now(datetime.timezone.utc)
)
try:
embed.set_thumbnail(url=user.avatar.url)
except:
embed.set_thumbnail(url=user.default_avatar.url)
embed.set_footer(text=f"Role removed at {datetime.datetime.utcnow()}")
embed.set_footer(text=f"Role removed")

await interaction.response.send_message(embed=embed)
else:
Expand All @@ -196,8 +207,9 @@ async def removerole(self, interaction, user: discord.Member, role: discord.Role
@app_commands.command(name="create-channel", description="Creates a channel")
@app_commands.guild_only()
@app_commands.default_permissions(administrator=True)
@app_commands.describe(name="The name of the channel", category="The category to create the channel in", private="Whether the channel should be private or not")
async def createchannel(self, interaction, name: str, category: discord.CategoryChannel = None, private: bool = False):
@app_commands.describe(name="The name of the channel", category="The category to create the channel in", private="Whether the channel should be private or not", emoji="The emoji to use for the channel")
async def createchannel(self, interaction, emoji: str, name: str, category: discord.CategoryChannel = None, private: bool = False):
name = f"{emoji}{name}" if emoji else name
if not private:
await interaction.guild.create_text_channel(name=name, category=category)
else:
Expand All @@ -207,8 +219,9 @@ async def createchannel(self, interaction, name: str, category: discord.Category
title="Channel Created",
description=f"Created the channel `{name}`",
color=discord.Color.green(),
timestamp=datetime.datetime.now(datetime.timezone.utc)
)
embed.set_footer(text=f"Channel created at {datetime.datetime.utcnow()}")
embed.set_footer(text=f"Channel created")

if category:
embed.add_field(name="Category", value=category.mention, inline=False)
Expand All @@ -226,12 +239,153 @@ async def deletechannel(self, interaction, channel: discord.TextChannel):
title="Channel Deleted",
description=f"Deleted the channel `{channel.name}`",
color=discord.Color.red(),
timestamp=datetime.datetime.now(datetime.timezone.utc)
)
embed.set_footer(text=f"Channel deleted")

await interaction.response.send_message(embed=embed)


@app_commands.command(name="create-role", description="Creates a role")
@app_commands.guild_only()
@app_commands.default_permissions(administrator=True)
@app_commands.describe(name="The name of the role", mentionable="Whether the role should be mentionable")
async def createrole(self, interaction, name: str, hoist: bool = False, mentionable: bool = False):
role = await interaction.guild.create_role(name=name, hoist=hoist, mentionable=mentionable)
embed = discord.Embed(
title="Role Created",
description=f"Created the role {role.mention}",
timestamp=datetime.datetime.now(datetime.timezone.utc)
)
embed.set_footer(text=f"Role created")

await interaction.response.send_message(embed=embed)


@app_commands.command(name="delete-role", description="Deletes a role")
@app_commands.guild_only()
@app_commands.default_permissions(administrator=True)
@app_commands.describe(role="The role to delete")
async def deleterole(self, interaction, role: discord.Role):
await role.delete()
embed = discord.Embed(
title="Role Deleted",
description=f"Deleted the role {role.mention}",
color=discord.Color.red(),
timestamp=datetime.datetime.now(datetime.timezone.utc)
)
embed.set_footer(text=f"Role deleted")

await interaction.response.send_message(embed=embed)


@app_commands.command(name="create-category", description="Creates a category")
@app_commands.guild_only()
@app_commands.default_permissions(administrator=True)
@app_commands.describe(name="The name of the category", emoji="The emoji to use for the category")
async def createcategory(self, interaction, emoji:str, name: str):
name = f"{emoji}{name}" if emoji else name
category = await interaction.guild.create_category(name=name)
embed = discord.Embed(
title="Category Created",
description=f"Created the category {category.name}",
color=discord.Color.green(),
timestamp=datetime.datetime.now(datetime.timezone.utc)
)
embed.set_footer(text=f"Channel deleted at {datetime.datetime.utcnow()}")
embed.set_footer(text=f"Category created")

await interaction.response.send_message(embed=embed)


@app_commands.command(name="delete-category", description="Deletes a category")
@app_commands.guild_only()
@app_commands.default_permissions(administrator=True)
@app_commands.describe(category="The category to delete")
async def deletecategory(self, interaction, category: discord.CategoryChannel):
await category.delete()
embed = discord.Embed(
title="Category Deleted",
description=f"Deleted the category {category.name}",
color=discord.Color.red(),
timestamp=datetime.datetime.now(datetime.timezone.utc)
)
embed.set_footer(text=f"Category deleted")

await interaction.response.send_message(embed=embed)


async def get_image(self, url: str) -> discord.Asset:
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.read()

@app_commands.command(name="create-emoji", description="Creates an emoji")
@app_commands.guild_only()
@app_commands.default_permissions(administrator=True)
@app_commands.describe(name="The name of the emoji", url="The url of the emoji")
async def createemoji(self, interaction, name: str, url: str):
image = await self.get_image(url)
emoji = await interaction.guild.create_custom_emoji(name=name, image=image)

embed = discord.Embed(
title="Emoji Created",
description=f"Created the emoji <:{name}:{emoji.id}>",
color=discord.Color.green(),
timestamp=datetime.datetime.now(datetime.timezone.utc)
)

embed.add_field(name="Name", value=name, inline=False)
embed.add_field(name="Url", value=url, inline=False)
embed.set_thumbnail(url=url)

embed.set_footer(text=f"Emoji created")

await interaction.response.send_message(embed=embed)

@app_commands.command(name="edit-channel", description="Edits a channel")
@app_commands.guild_only()
@app_commands.default_permissions(administrator=True)
@app_commands.describe(channel="The channel to edit", name="The new name of the channel", category="The new category of the channel", private="Whether the channel should be private or not", slowmode="The new slowmode of the channel")
async def editchannel(self, interaction, channel: discord.TextChannel, name: str, category: discord.CategoryChannel = None, private: bool = False, slowmode: int = None):
await channel.edit(name=name, category=category, overwrites={interaction.guild.default_role: discord.PermissionOverwrite(read_messages=False)} if private else None, slowmode_delay=slowmode)
embed = discord.Embed(
title="Channel Edited",
description=f"Edited the channel {channel.name}",
color=discord.Color.green(),
timestamp=datetime.datetime.now(datetime.timezone.utc)
)
embed.set_footer(text=f"Channel edited")

await interaction.response.send_message(embed=embed)


@app_commands.command(name="kick", description="Kicks a user from the server")
@app_commands.guild_only()
@app_commands.default_permissions(administrator=True)
@app_commands.describe(user="The user to kick", reason="The reason for kicking the user")
async def kick(self, interaction: discord.Interaction, user: discord.Member, *, reason:str|None = None):
await interaction.guild.kick(user, reason=reason)

embed = discord.Embed(
title="User Kicked",
description=f"{user.mention} was kicked from the server",
color=discord.Color.orange(),
timestamp=datetime.datetime.now(datetime.timezone.utc)
)

embed.add_field(name=f"User Id `{user.id}`", value="** **", inline=False)
embed.add_field(name=f"Kicked By {interaction.user.mention}", value="** **", inline=False)
embed.add_field(name="Reason", value=reason, inline=False)

try:
embed.set_thumbnail(url=user.avatar.url)
except:
embed.set_thumbnail(url=user.default_avatar.url)

embed.set_footer(text=f"{user.name} Kicked")

await interaction.response.send_message(embed=embed)

async def setup(bot):
await bot.add_cog(moderation(bot))
print("Loaded moderation_cog.py")
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async def on_ready(self):
await load()

global start_time
start_time = datetime.datetime.utcnow()
start_time = datetime.datetime.now(datetime.timezone.utc)

await self.wait_until_ready()
await self.change_presence(
Expand Down Expand Up @@ -232,7 +232,7 @@ async def ping(ctx):
)

global start_time
current_time = datetime.datetime.utcnow()
current_time = datetime.datetime.now(datetime.timezone.utc)
delta = current_time - start_time

hours, remainder = divmod(int(delta.total_seconds()), 3600)
Expand Down

0 comments on commit b32dffd

Please sign in to comment.