Skip to content

Commit

Permalink
Merge pull request #51 from BlueAtomic/blacklist-command
Browse files Browse the repository at this point in the history
Blacklist command
  • Loading branch information
JoshuaSlui authored May 31, 2023
2 parents bc87e0d + 6dfadab commit d87093e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
31 changes: 31 additions & 0 deletions cogs/blacklist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import discord
from discord import slash_command, option
from discord.ext import commands
from discord.ext.bridge import Bot
from utils import Utils


class Blacklist(commands.Cog):
def __init__(self, bot: Bot):
self.bot = bot

@slash_command()
@commands.is_owner()
@option("server", discord.Guild, description="The server to blacklist")
@option("reason", str, description="The reason for the blacklist")
async def blacklist(self, ctx, server, reason):
""" Blacklist a server from the bot """
cursor = await Utils.mysql_login()
database = cursor.cursor()
database.execute("INSERT IGNORE INTO blacklist (guild_id, reason) VALUES (%s, %s)", (server.id, reason))
cursor.commit()
database.close()
cursor.close()

guild = self.bot.get_guild(server.id)
await guild.leave()
return await ctx.respond(f'Successfully added guild {server} to the blacklist for:\n**{reason}**')


def setup(bot: Bot):
bot.add_cog(Blacklist(bot))
4 changes: 4 additions & 0 deletions cogs/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ async def on_command_error(self, ctx, error):

@commands.Cog.listener()
async def on_application_command_error(self, ctx, error):
if isinstance(error, commands.NotOwner):
return await ctx.respond("This command is for owners only.")
if isinstance(error, commands.GuildNotFound):
return await ctx.respond("Could not find this guild.")
await Utils.respond(ctx, "An unknown error has occured!\nThis has been logged")
raise error

Expand Down
19 changes: 19 additions & 0 deletions cogs/internallogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@ def __init__(self, bot: Bot):

@commands.Cog.listener()
async def on_guild_join(self, guild: Guild):
cursor = await Utils.mysql_login()
database = cursor.cursor()
database.execute("SELECT * FROM blacklist WHERE guild_id = %s", [guild.id])

try:
result = database.fetchone()[0]
print(f'Guild {guild} attempted to add {self.bot.user.name}, but was blacklisted.')
embed = Embed(title="Joined a guild!", color=Utils.Colors.red)
embed.add_field(name="Name", value=guild.name, inline=True)
embed.add_field(name="ID", value=guild.id, inline=True)
embed.description(f'Guild attempted to add {self.bot.user.name}, but is blacklisted:\n**{result["reason"]}**')
embed.set_thumbnail(url=guild.icon.url)
async with aiohttp.ClientSession() as client_session:
webhook = Webhook.from_url(Utils.get_data()['Logs']["JoinWebhook"], session=client_session)
await webhook.send(embed=embed, username="Pingernos Logs", avatar_url=self.bot.user.avatar.url)
await guild.leave()
except TypeError:
pass

embed = Embed(title="Joined a guild!", color=Utils.Colors.green)
embed.add_field(name="Name", value=guild.name, inline=True)
embed.add_field(name="ID", value=guild.id, inline=True)
Expand Down
1 change: 1 addition & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ async def on_connect():
cursor = await Utils.mysql_login()
database = cursor.cursor()
database.execute("CREATE TABLE IF NOT EXISTS server (guild_id VARCHAR(255) PRIMARY KEY, server_ip TEXT NOT NULL)")
database.execute("CREATE TABLE IF NOT EXISTS blacklist (guild_id VARCHAR(21) PRIMARY KEY, reason TEXT NOT NULL)")
database.close()

@bot.listen()
Expand Down

0 comments on commit d87093e

Please sign in to comment.