Files
botpy/c/moderation.py
2025-11-26 19:34:08 +05:00

332 lines
14 KiB
Python

import discord
from discord.ext import commands
from discord.ext.commands import has_permissions, Context
import random
import datetime
class Moderation(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.last_member = None
@commands.command()
@has_permissions(kick_members=True)
async def kick(self, ctx, user: discord.Member = None, *, reason: str = "Not specified"):
"""
Kick a user from server
"""
embed = discord.Embed(
title=f"{user} was kicked by {ctx.author}",
color=random.randint(0, 0xFFFFFF),
)
embed.add_field(name="Reason:", value=reason)
try:
await user.kick(reason= reason)
except:
embed = discord.Embed(
title="An error occurred while trying to kick the user. Make sure ID is an existing ID that belongs to a user.",
color=0xE02B2B,
)
await ctx.send(embed=embed)
await user.send(f'you were kicked by {ctx.author} from {ctx.guild.name} for {reason}')
@commands.command()
@has_permissions(kick_members=True)
async def ban(self, ctx, user: discord.Member = None, *, reason: str = "Not specified"):
"""
Ban a user from server
"""
embed = discord.Embed(
title=f"{user} was banned by {ctx.author}",
color=random.randint(0, 0xFFFFFF),
)
embed.add_field(name="Reason:", value=reason)
try:
await user.ban(reason= reason)
except:
embed = discord.Embed(
title="An error occurred while trying to ban the user. Make sure ID is an existing ID that belongs to a user.",
color=0xE02B2B,
)
await ctx.send(embed=embed)
await user.send(f'you were banned by {ctx.author} from {ctx.guild.name} for {reason}')
@commands.command()
@has_permissions(ban_members=True)
async def hban(
self, context: Context, user_id: str, *, reason: str = "Not specified") -> None:
"""
Ban a user from server without user on 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=random.randint(0, 0xFFFFFF),
)
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.command()
@has_permissions(ban_members=True)
async def unban(self, context: Context, user_id: str, *, reason: str = "Not specified"):
"""
Unban a user from server
"""
try:
await self.bot.http.unban(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 unbanned by {context.author}",
color=random.randint(0, 0xFFFFFF),
)
await context.send(embed=embed)
except Exception:
embed = discord.Embed(
description="An error occurred while trying to unban the user. Make sure ID is an existing ID that belongs to a user.",
color=0xE02B2B,
)
await context.send(embed=embed)
@commands.command()
@has_permissions(manage_messages=True)
async def delete(self, context: Context, amount: int) -> None:
"""
Delete a number of messages
"""
await context.send("Deleting messages...")
purged_messages = await context.channel.purge(limit=amount + 2)
embed = discord.Embed(
title=f"**{len(purged_messages)-1}** messages deleted",
description=f"in #{context.channel}",
color=random.randint(0, 0xFFFFFF),
).set_author(name=context.author.name, icon_url = context.author.avatar.url)
res = await self.bot.ch(server_id=context.guild.id)
channel = self.bot.get_channel(int(res))
await channel.send(embed=embed)
@commands.command()
@has_permissions(moderate_members=True)
async def timeout(self, ctx, user: discord.Member,seconds: int = 0, minutes: int = 0, hours: int = 0, days: int = 0, reason: str= "Not specified",) -> None:
"""
Timeout user for a time (s m h d reason)
"""
duration = datetime.timedelta(seconds=seconds, minutes=minutes, hours=hours, days=days)
embed = discord.Embed(
title=f'{user} has been timeout',
color = random.randint(0, 0xFFFFFF)
).set_author(name=ctx.author.name, icon_url = ctx.author.avatar.url)
embed.add_field(name="Reason:", value=reason)
embed.add_field(name="Time:", value=duration)
try:
await user.timeout(duration, reason=reason)
except:
embed = discord.Embed(
title="An error occurred while trying to timeout the user. Make sure ID is an existing ID that belongs to a user.",
color=0xE02B2B
)
await ctx.send(embed=embed)
@commands.command()
@commands.is_owner()
async def setwarnnumb(self, ctx,* , numb):
server_id = ctx.guild.id
row = self.bot.conwarn.execute("SELECT number FROM warnid WHERE server_id=?", (server_id,))
res = row.fetchone()
if res != None:
self.bot.conwarn.execute("DELETE FROM warnid WHERE server_id=?", (server_id,))
self.bot.conwarn.commit()
self.bot.conwarn.execute("INSERT INTO warnid(number, server_id) VALUES (?, ?)", (numb, server_id,))
self.bot.conwarn.commit()
embed = discord.Embed(
color=random.randint(0, 0xFFFFFF),
title='Warn number is set'
)
embed.add_field(name='current',value=numb)
await ctx.send(embed=embed)
@commands.command()
@commands.is_owner()
async def setwarnact(self, ctx,* , numb):
server_id = ctx.guild.id
row = self.bot.conwarn.execute("SELECT number FROM warnact WHERE server_id=?", (server_id,))
res = row.fetchone()
if res != None:
self.bot.conwarn.execute("DELETE FROM warnact WHERE server_id=?", (server_id,))
self.bot.conwarn.commit()
self.bot.conwarn.execute("INSERT INTO warnact(number, server_id) VALUES (?, ?)", (numb, server_id,))
self.bot.conwarn.commit()
embed = discord.Embed(
color=random.randint(0, 0xFFFFFF),
title='Warn action is choosen'
)
if numb == "1": name = "timeout"
elif numb == "2": name = "kick"
elif numb == "3": name = "ban"
else:
await ctx.send('list of actions:' '\n1 - timeout' '\n2 - kick' '\n3 - ban')
pass
embed.add_field(name='current',value=name)
await ctx.send(embed=embed)
@commands.command()
@commands.is_owner()
async def warnsettings(self, ctx):
server_id = ctx.guild.id
row = self.bot.conwarn.execute("SELECT number FROM warnid WHERE server_id=?", (server_id,))
res = row.fetchone()
row1 = self.bot.conwarn.execute("SELECT number FROM warnact WHERE server_id=?", (server_id,))
res1 = row1.fetchone()
if res1 == (1,): name = "timeout"
elif res1 == (2,): name = "kick"
elif res1 == (3,): name = "ban"
embed = discord.Embed(
color=random.randint(0, 0xFFFFFF),
title='Warn settings'
)
limit = str(res).replace("(","").replace(")","").replace(",","")
embed.add_field(name='current warn limit',value=limit)
embed.add_field(name='current warn action',value=name)
await ctx.send(embed=embed)
@commands.command()
@has_permissions(moderate_members=True)
async def warn(self, context = Context, user: discord.Member = None, *, reason: str = "Not specified") -> None:
"""
Warn a user. if it fourth warn, user get timeout
"""
s = 0
m = 0
h = 3
d = 0
duration = datetime.timedelta(seconds=s, minutes=m, hours=h, days=d)
user_id = user.id
server_id = context.guild.id
rows = self.bot.conwarn.execute("SELECT number FROM warnid WHERE server_id=?", (server_id,))
id = rows.fetchone()
try:
rows = self.bot.conwarn.execute("SELECT id FROM warn WHERE user_id=? AND server_id=? ORDER BY id DESC LIMIT 1", (user_id, server_id,))
results = rows.fetchone()
if results == None:
warn_id = 1
self.bot.conwarn.execute("INSERT INTO warn(id, user_id, server_id, reason) VALUES (?, ?, ?, ?)", (warn_id, user_id, server_id, reason,))
self.bot.conwarn.commit()
try:
await user.send(f'You have been warned by {context.author} reason: {reason}')
except:
await context.channel.send(f'<@{user_id}> You have been warned by {context.author} reason: {reason}')
elif results < id:
warn_id = results[0] + 1
self.bot.conwarn.execute("INSERT INTO warn(id, user_id, server_id, reason) VALUES (?, ?, ?, ?)", (warn_id, user_id, server_id, reason,))
self.bot.conwarn.commit()
try:
await user.send(f'You have been warned by {context.author} reason: {reason}')
except:
await context.channel.send(f'<@{user_id}> You have been warned by {context.author} reason: {reason}')
else:
row = self.bot.conwarn.execute("SELECT number FROM warnact WHERE server_id=?", (server_id,))
action = row.fetchone()
if action == (1,):
name = "timeouted"
await user.timeout(duration, reason=reason)
elif action == (2,):
name = "kicked"
await user.kick(reason=reason)
elif action == (3,):
name = "banned"
await user.ban(reason= reason)
await context.send(f"{user} has been {name}")
self.bot.conwarn.execute("DELETE FROM warn WHERE user_id=? AND server_id=?", (user_id, server_id,))
self.bot.conwarn.commit()
try:
if name == "timeouted":
await user.send(f'You get warn by {context.author} reason: {reason} you got {name} amout {duration}')
else:
await user.send(f'You get warn by {context.author} reason: {reason} you got {name}')
except:
if name == "timeouted":
await context.channel.send(f'<@{user.id}> you get warn by {context.author} reason: {reason} you got {name} amout {duration}')
else:
await context.channel.send(f'<@{user.id}> you get warn by {context.author} reason: {reason} you got {name}')
except:
await context.send('ERROR')
@commands.command()
@has_permissions(moderate_members=True)
async def warn_remove(self, context = Context, user: discord.Member = None) -> None:
"""
Remove all warns from user
"""
try:
user_id = user.id
server_id = context.guild.id
self.bot.conwarn.execute("DELETE FROM warn WHERE user_id=? AND server_id=?", (user_id, server_id,))
self.bot.conwarn.commit()
embed = discord.Embed(
title=f"All warns {user} has been deleted",
color=random.randint(0, 0xFFFFFF)
)
except:
embed = discord.Embed(
title="ERROR"
)
await context.send(embed=embed)
@commands.command()
@has_permissions(moderate_members=True)
async def warn_list(self, context = Context, user: discord.Member = None):
"""
List of user warns
"""
server_id = context.guild.id
user_id = user.id
rows = self.bot.conwarn.execute("SELECT user_id, server_id, reason FROM warn WHERE user_id=? AND server_id=?",(user_id, server_id,))
result = rows.fetchall()
result_list = []
for row in result:
result_list.append(row)
embed = discord.Embed(
title=f'Warns {user}',
color=random.randint(0, 0xFFFFFF)
)
description = ""
if len(result_list) == 0:
description = "This user has no warnings."
else:
for warning in result_list:
description += f"{1}. for reason: {warning[2]}\n"
embed.description = description
if user.avatar:
embed.set_thumbnail(url=user.avatar.url)
await context.send(embed=embed)
@commands.Cog.listener()
async def on_ready(self):
print('Moderation cog has been loaded')
async def setup(bot):
await bot.add_cog(Moderation(bot))