Compare commits

..

3 commits

Author SHA1 Message Date
dd98ecdcd1
feat(moderation): added unmute command
Some checks failed
Pylint / Pylint (push) Failing after 1m13s
2023-10-04 16:43:15 -04:00
e66a902442
feat(moderation): added warn command 2023-10-04 16:39:58 -04:00
6fba47ea56
misc(moderation): changed table naming scheme 2023-10-04 16:37:59 -04:00

View file

@ -5,7 +5,7 @@ import discord
import humanize
import mysql.connector
from pytimeparse2 import disable_dateutil, parse
from redbot.core import Config, checks, commands
from redbot.core import Config, checks, commands, tasks
class Moderation(commands.Cog):
@ -115,11 +115,11 @@ class Moderation(commands.Cog):
database = await self.connect()
cursor = database.cursor()
try:
cursor.execute(f"SELECT * FROM `{guild.id}_moderation`")
cursor.execute(f"SELECT * FROM `moderation_{guild.id}`")
logging.info("MySQL Table exists for server %s (%s)", guild.name, guild.id)
except mysql.connector.errors.ProgrammingError:
query = f"""
CREATE TABLE `{guild.id}_moderation` (
CREATE TABLE `moderation_{guild.id}` (
moderation_id INT UNIQUE PRIMARY KEY NOT NULL,
timestamp INT NOT NULL,
moderation_type LONGTEXT NOT NULL,
@ -135,7 +135,7 @@ class Moderation(commands.Cog):
"""
cursor.execute(query)
insert_query = f"""
INSERT INTO `{guild.id}_moderation`
INSERT INTO `moderation_{guild.id}`
(moderation_id, timestamp, moderation_type, target_id, moderator_id, duration, end_timestamp, reason, resolved, resolve_reason, expired)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
@ -143,7 +143,7 @@ class Moderation(commands.Cog):
cursor.execute(insert_query, insert_values)
database.commit()
database.close()
logging.info("MySQL Table created for %s (%s)\n%s_moderation", guild.name, guild.id, guild.id)
logging.info("MySQL Table created for %s (%s)\nmoderation_%s", guild.name, guild.id, guild.id)
else:
database.close()
return
@ -165,16 +165,29 @@ class Moderation(commands.Cog):
end_timestamp = 0
database = await self.connect()
cursor = database.cursor()
cursor.execute(f"SELECT moderation_id FROM `{guild_id}_moderation` ORDER BY moderation_id DESC LIMIT 1")
cursor.execute(f"SELECT moderation_id FROM `moderation_{guild_id}` ORDER BY moderation_id DESC LIMIT 1")
moderation_id = cursor.fetchone()[0] + 1
sql = f"INSERT INTO `{guild_id}_moderation` (moderation_id, timestamp, moderation_type, target_id, moderator_id, duration, end_timestamp, reason, resolved, resolve_reason, expired) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
sql = f"INSERT INTO `moderation_{guild_id}` (moderation_id, timestamp, moderation_type, target_id, moderator_id, duration, end_timestamp, reason, resolved, resolve_reason, expired) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
val = (moderation_id, timestamp, moderation_type, target_id, author_id, duration, end_timestamp, f"{reason}", 0, "NULL", 0)
cursor.execute(sql, val)
database.commit()
database.close()
logging.debug("MySQL row inserted into %s_moderation!\n%s, %s, %s, %s, %s, %s, %s, %s, 0, NULL", guild_id, moderation_id, timestamp, moderation_type, target_id, author_id, duration, end_timestamp, reason)
logging.debug("MySQL row inserted into moderation_%s!\n%s, %s, %s, %s, %s, %s, %s, %s, 0, NULL", guild_id, moderation_id, timestamp, moderation_type, target_id, author_id, duration, end_timestamp, reason)
@commands.hybrid_command(name="mute", aliases=['timeout'])
@commands.hybrid_command(name="warn")
@commands.mod()
async def warn(self, ctx: commands.Context, target: discord.Member, *, reason: str):
"""Warn a user."""
response = await ctx.send(content=f"{target.mention} has been warned!\n**Reason** - `{reason}`")
try:
embed = discord.Embed(title="Warned", description=f"You have been warned in [{ctx.guild.name}]({response.jump_url}).", color=await self.bot.get_embed_color(None))
embed.add_field(name='Reason', value=f"`{reason}`")
await target.send(embed=embed)
except discord.errors.HTTPException:
await response.edit(content=f"{response.content}\n*Failed to send DM, user likely has the bot blocked.*")
await self.mysql_log(ctx.guild.id, ctx.author.id, 'WARN', target.id, 'NULL', reason)
@commands.hybrid_command(name="mute", aliases=['timeout', 'tm'])
@commands.mod()
async def mute(self, ctx: commands.Context, target: discord.Member, duration: str, *, reason: str):
"""Mute a user."""
@ -196,6 +209,25 @@ class Moderation(commands.Cog):
await response.edit(content=f"{response.content}\n*Failed to send DM, user likely has the bot blocked.*")
await self.mysql_log(ctx.guild.id, ctx.author.id, 'MUTE', target.id, parsed_time, reason)
@commands.hybrid_command(name="unmute", aliases=['untimeout', 'utm'])
@commands.mod()
async def unmute(self, ctx: commands.Context, target: discord.Member, *, reason: str = None):
"""Unmute a user."""
if target.is_timed_out() is False:
await ctx.send(f"{target.mention} is not muted!", allowed_mentions=discord.AllowedMentions(users=False))
return
await target.timeout(None)
if reason is None:
reason = "No reason given."
response = await ctx.send(content=f"{target.mention} has been unmuted!\n**Reason** - `{reason}`")
try:
embed = discord.Embed(title="Unmuted", description=f"You have been unmuted in [{ctx.guild.name}]({response.jump_url}).", color=await self.bot.get_embed_color(None))
embed.add_field(name='Reason', value=f"`{reason}`")
await target.send(embed=embed)
except discord.errors.HTTPException:
await response.edit(content=f"{response.content}\n*Failed to send DM, user likely has the bot blocked.*")
await self.mysql_log(ctx.guild.id, ctx.author.id, 'UNMUTE', target.id, 'NULL', reason)
@commands.group(autohelp=True)
@checks.admin()
async def moderationset(self, ctx: commands.Context):