Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blacklist command #51

Merged
merged 6 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Soapy7261 marked this conversation as resolved.
Show resolved Hide resolved
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