feat(moderation): implemented permissions checking

This commit is contained in:
Seaswimmer 2023-10-22 13:55:54 -04:00
parent 3d2960b37a
commit e61d787edf
No known key found for this signature in database
GPG key ID: 5019678FD9CF50D8

View file

@ -1,6 +1,7 @@
import logging import logging
import time import time
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
from typing import Union
import discord import discord
import humanize import humanize
import mysql.connector import mysql.connector
@ -176,6 +177,17 @@ class Moderation(commands.Cog):
not_found_list.append(item) not_found_list.append(item)
return not_found_list return not_found_list
def check_permissions(self, member: discord.Member, permissions: list, ctx: Union[commands.Context, discord.Interaction] = None):
"""Checks if the bot has a specific permission (or a list of permissions) in a channel."""
if ctx:
resolved_permissions = ctx.channel.permissions_for(member)
else:
resolved_permissions = member.guild_permissions
for permission in permissions:
if not getattr(resolved_permissions, permission, False) and not resolved_permissions.administrator is True:
return permission
return False
async def mysql_log(self, guild_id: str, author_id: str, moderation_type: str, target_id: int, duration, reason: str): async def mysql_log(self, guild_id: str, author_id: str, moderation_type: str, target_id: int, duration, reason: str):
timestamp = int(time.time()) timestamp = int(time.time())
if duration != "NULL": if duration != "NULL":
@ -352,6 +364,10 @@ class Moderation(commands.Cog):
Why are you unbanning this user? Why are you unbanning this user?
silent: bool silent: bool
Should the user be messaged?""" Should the user be messaged?"""
permissions = self.check_permissions(interaction.client.user, ['moderate_members'], interaction)
if permissions:
await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True)
return
if target.is_timed_out() is True: if target.is_timed_out() is True:
await interaction.response.send_message(f"{target.mention} is already muted!", allowed_mentions=discord.AllowedMentions(users=False), ephemeral=True) await interaction.response.send_message(f"{target.mention} is already muted!", allowed_mentions=discord.AllowedMentions(users=False), ephemeral=True)
return return
@ -387,6 +403,10 @@ class Moderation(commands.Cog):
Why are you unmuting this user? Why are you unmuting this user?
silent: bool silent: bool
Should the user be messaged?""" Should the user be messaged?"""
permissions = self.check_permissions(interaction.client.user, ['moderate_members'], interaction)
if permissions:
await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True)
return
if target.is_timed_out() is False: if target.is_timed_out() is False:
await interaction.response.send_message(f"{target.mention} is not muted!", allowed_mentions=discord.AllowedMentions(users=False), ephemeral=True) await interaction.response.send_message(f"{target.mention} is not muted!", allowed_mentions=discord.AllowedMentions(users=False), ephemeral=True)
return return
@ -418,6 +438,10 @@ class Moderation(commands.Cog):
Why are you kicking this user? Why are you kicking this user?
silent: bool silent: bool
Should the user be messaged?""" Should the user be messaged?"""
permissions = self.check_permissions(interaction.client.user, ['kick_members'], interaction)
if permissions:
await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True)
return
await interaction.response.send_message(content=f"{target.mention} has been kicked!\n**Reason** - `{reason}`") await interaction.response.send_message(content=f"{target.mention} has been kicked!\n**Reason** - `{reason}`")
if silent is None: if silent is None:
silent = not await self.config.guild(interaction.guild).dm_users() silent = not await self.config.guild(interaction.guild).dm_users()
@ -454,6 +478,10 @@ class Moderation(commands.Cog):
How many days of messages to delete? How many days of messages to delete?
silent: bool silent: bool
Should the user be messaged?""" Should the user be messaged?"""
permissions = self.check_permissions(interaction.client.user, ['ban_members'], interaction)
if permissions:
await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True)
return
try: try:
await interaction.guild.fetch_ban(target) await interaction.guild.fetch_ban(target)
await interaction.response.send_message(content=f"{target.mention} is already banned!", ephemeral=True) await interaction.response.send_message(content=f"{target.mention} is already banned!", ephemeral=True)
@ -499,6 +527,10 @@ class Moderation(commands.Cog):
Why are you unbanning this user? Why are you unbanning this user?
silent: bool silent: bool
Should the user be messaged?""" Should the user be messaged?"""
permissions = self.check_permissions(interaction.client.user, ['moderate_members'], interaction)
if permissions:
await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True)
return
try: try:
await interaction.guild.fetch_ban(target) await interaction.guild.fetch_ban(target)
except discord.errors.NotFound: except discord.errors.NotFound:
@ -536,6 +568,10 @@ class Moderation(commands.Cog):
Page to select Page to select
epheremal: bool epheremal: bool
Hide the command response""" Hide the command response"""
permissions = self.check_permissions(interaction.client.user, ['embed_links'], interaction)
if permissions:
await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True)
return
database = await self.connect() database = await self.connect()
cursor = database.cursor() cursor = database.cursor()
if target: if target:
@ -598,6 +634,10 @@ class Moderation(commands.Cog):
Case number of the case you're trying to resolve Case number of the case you're trying to resolve
reason: str reason: str
Reason for resolving case""" Reason for resolving case"""
permissions = self.check_permissions(interaction.client.user, ['embed_links'], interaction)
if permissions:
await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True)
return
conf = await self.check_conf(['mysql_database']) conf = await self.check_conf(['mysql_database'])
if conf: if conf:
raise(LookupError) raise(LookupError)
@ -658,6 +698,10 @@ class Moderation(commands.Cog):
What case are you looking up? What case are you looking up?
ephemeral: bool ephemeral: bool
Hide the command response""" Hide the command response"""
permissions = self.check_permissions(interaction.client.user, ['embed_links'], interaction)
if permissions:
await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True)
return
database = await self.connect() database = await self.connect()
cursor = database.cursor() cursor = database.cursor()
query = "SELECT * FROM moderation_%s WHERE moderation_id = %s;" query = "SELECT * FROM moderation_%s WHERE moderation_id = %s;"