Skip to content
This repository was archived by the owner on Jun 19, 2023. It is now read-only.

Interaction patches #2

Open
wants to merge 51 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
04d9cb3
Fixed Choices.
BenitzCoding Mar 7, 2022
a4f86ce
Fixed Guild Commands Tree Sync.
BenitzCoding Mar 7, 2022
1d9db4b
Updating ENV file.
BenitzCoding Mar 7, 2022
4f28380
Added Checks, and Env functions.
BenitzCoding Mar 13, 2022
f058661
Added `dotenv` to requirements.
BenitzCoding Mar 14, 2022
fe257ad
Updating global functions file.
BenitzCoding Mar 14, 2022
5ed1348
Update core.py
BenitzCoding Apr 1, 2022
251b96b
Renaming `shortcode` to `globals`.
BenitzCoding Apr 1, 2022
7cba56b
Added `setup_hook`.
BenitzCoding Apr 1, 2022
2ceadb1
Added functions.
BenitzCoding Apr 1, 2022
235783e
Hmmmm
JDJGInc Apr 8, 2022
bd80a91
Added Files to `.gitignore`.
BenitzCoding Apr 22, 2022
2ddd157
Fixed imports.
BenitzCoding Apr 22, 2022
9e86d93
Fixed extension autocomplete.
BenitzCoding Apr 22, 2022
7ab62b1
Added load and unload commands.
BenitzCoding Apr 22, 2022
d6edfd6
Fixed unload.
BenitzCoding Apr 22, 2022
f134beb
Fixed Cog setup.
BenitzCoding Apr 22, 2022
4676d6d
Fix guild sync commands.
BenitzCoding Apr 22, 2022
a917ac2
Moved commands to cogs.
BenitzCoding Apr 22, 2022
cb033f0
Fixed register command.
BenitzCoding Apr 22, 2022
bced090
Loading extensions in new `setup_hook` function
BenitzCoding Apr 22, 2022
2ce1240
Deleted Loop.
BenitzCoding Apr 22, 2022
022ee3e
Added `output` function to globals.
BenitzCoding Apr 22, 2022
75e1ce8
Updated Unregister command.
BenitzCoding Apr 22, 2022
46ea5f3
Update .gitignore
BenitzCoding Apr 22, 2022
e17ca8c
Added new ENV Variable.
BenitzCoding Jun 1, 2022
ef8cb05
Moved autocomplete function into class.
BenitzCoding Jun 1, 2022
8b2bc6a
Added function to get users using API.
BenitzCoding Jun 1, 2022
5e35d95
Added Role enum class.
BenitzCoding Jun 1, 2022
5940ef5
Added User Object class.
BenitzCoding Jun 1, 2022
368f6b5
Pep-8 spacing (not indentation yet.)
BenitzCoding Jun 1, 2022
6414b31
Enforcing Pep-8 Indentation.
BenitzCoding Jun 1, 2022
394fd83
Pep-8 changes.
BenitzCoding Jun 6, 2022
29e8769
Importing buttons.
BenitzCoding Jun 6, 2022
e9ee423
Add button class views.
BenitzCoding Jun 6, 2022
56ee5e8
Remade owner checks.
BenitzCoding Jun 6, 2022
5e5fd9f
Rewrite commands to support changes.
BenitzCoding Jun 6, 2022
17217fa
Modified checks on core extension.
BenitzCoding Jun 6, 2022
eecf9a3
Fixed typo.
BenitzCoding Jun 6, 2022
7ced198
Fixed Button Styles.
BenitzCoding Jun 6, 2022
3f0cdc5
Fixed errors with editing messages.
BenitzCoding Jun 6, 2022
2e55680
FIxed kwargs for editing messages.
BenitzCoding Jun 6, 2022
0421836
Fixed attributes.
BenitzCoding Jun 6, 2022
73bb138
Fixed ephemeral error.
BenitzCoding Jun 6, 2022
f28bccf
Update .gitignore
BenitzCoding Jun 29, 2022
dd285c1
Finished Owner Command Management.
BenitzCoding Jul 8, 2022
c441f5f
Simplified code.
BenitzCoding Jul 8, 2022
7febbea
Removed `.env` spam.
BenitzCoding Jul 8, 2022
441f86e
Fixed .env bug.
BenitzCoding Jul 8, 2022
705b7de
Switching out `bot.event` with `bot.listen`.
BenitzCoding Jul 8, 2022
e3ba493
Removing unused args.
BenitzCoding Jul 24, 2022
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
2 changes: 0 additions & 2 deletions .env

This file was deleted.

5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

.env
.env
*.pyc
latest.log
config.json
*.json
176 changes: 137 additions & 39 deletions cogs/core.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,137 @@
# import os
# import sys
# import discord

# from cool_utils import Terminal
# from utils.shortcode import respond, author
# from dotenv import load_dotenv, find_dotenv

# from discord import app_commands
# from discord.ext import commands

# def env(variable: str):
# load_dotenv(find_dotenv())
# env = os.getenv(variable)
# if env == None:
# Terminal.error(f"Environmental variable \"{variable}\" not found.")
# return None
# else:
# return env

# def owner(author):
# if int(author.id) == int(env("OWNER")):
# return True
# else:
# return False

# class Core(commands.Cog):
# def __init__(self, bot):
# self.bot = bot

# @tree.command(guild=CORE_GUILD, description="Shuts down the bot.")
# async def shutdown(self, inter: discord.Interaction):
# if owner(author(inter)) == False:
# return await respond(inter, f":no_entry_sign: You don't have permissions to use this command.", ephemeral=True)
# await respond(inter, f":ballot_box_with_check: Bot Shutting Down...", ephemeral=True)
# sys.exit()

# def setup(bot):
# bot.add_cog(Core(bot))
from distutils import extension
import sys
import discord

from discord import app_commands
from discord.ext import commands

from discord.app_commands import Choice

from utils.globals import (
respond,
author,
owner,
output
)

UNLOADED_EXTENSIONS = []
LOADED_EXTENSIONS = []

class Core(commands.Cog):
def __init__(self, bot):
self.bot = bot
for extension in self.bot.UNLOADED_EXTENSIONS:
UNLOADED_EXTENSIONS.append(extension)

for extension in self.bot.LOADED_EXTENSIONS:
LOADED_EXTENSIONS.append(extension)

async def get_loaded_extensions(self, interaction, current: str):
if LOADED_EXTENSIONS == []:
return [Choice(name="No Extensions", value="No Extensions")]
return [
app_commands.Choice(name=extension, value=extension)
for extensions in LOADED_EXTENSIONS if current.lower() in extensions.lower()
]

async def get_unloaded_extensions(self, interaction, current: str):
if UNLOADED_EXTENSIONS == []:
return [Choice(name="No Extensions", value="No Extensions")]
return [
app_commands.Choice(name=extension, value=extension)
for extensions in UNLOADED_EXTENSIONS if current.lower() in extensions.lower()
]

@app_commands.command(
name = "reload",
description = "Reloads a cog."
)
@app_commands.describe(extension = "Cog extension that needs to be reloaded.")
@app_commands.autocomplete(extension = get_loaded_extensions)
async def reload(self, interaction, extension: str):
respond = interaction.response.send_message
author = interaction.user

await interaction.response.send_message(
"Processing command, please hold...",
ephemeral = True
)
if await self.bot.owner(interaction = interaction) == False:
return
try:
await self.bot.unload_extension(f"cogs.{extension}")
self.bot.LOADED_EXTENSIONS.remove(extension)
self.bot.UNLOADED_EXTENSIONS.append(extension)
await self.bot.load_extension(f"cogs.{extension}")
self.bot.UNLOADED_EXTENSIONS.remove(extension)
self.bot.LOADED_EXTENSIONS.append(extension)
output(f"Reloaded Cog \"{extension}\"")
await interaction.response.edit_message(content = f":ballot_box_with_check: **`cogs.{extension}` reloaded.**")
except Exception as error:
output(f"An error occurred while reloading \"{extension}\" cog.")
await interaction.response.edit_message(content = f":warning: An error occurred while reloading **`cogs.{extension}`**.\n\n```py\n{error}\n```")

@app_commands.command(
name = "load",
description="Loads a cog."
)
@app_commands.describe(extension = "Cog extension that needs to be loaded.")
@app_commands.autocomplete(extension = get_unloaded_extensions)
async def load(self, interaction, extension: str):
respond = interaction.response.send_message
author = interaction.user

await interaction.response.send_message(
"Processing command, please hold...",
ephemeral = True
)
if await self.bot.owner(interaction = interaction) == False:
return
try:
await self.bot.load_extension(f"cogs.{extension}")
self.bot.UNLOADED_EXTENSIONS.remove(extension)
self.bot.LOADED_EXTENSIONS.append(extension)
output(f"Loaded Cog \"{extension}\"")
await interaction.response.edit_message(content = f":ballot_box_with_check: **`cogs.{extension}` loaded.**")
except Exception as error:
output(f"An error occurred while loading \"{extension}\" cog.")
await interaction.response.edit_message(content = f":warning: An error occurred while loading **`cogs.{extension}`**.\n\n```py\n{error}\n```")

@app_commands.command(
name = "unload",
description = "Unloads a cog."
)
@app_commands.describe(extension = "Cog extension that needs to be unloaded.")
@app_commands.autocomplete(extension = get_loaded_extensions)
async def unload(self, interaction, extension: str):
respond = interaction.response.send_message
author = interaction.user

await interaction.response.send_message(
"Processing command, please hold...",
ephemeral = True
)
if await self.bot.owner(interaction = interaction) == False:
return
try:
await self.bot.load_extension(f"cogs.{extension}")
self.bot.UNLOADED_EXTENSIONS.append(extension)
self.bot.LOADED_EXTENSIONS.remove(extension)
output(f"Unoaded Cog \"{extension}\"")
await interaction.response.edit_message(content = f":ballot_box_with_check: **`cogs.{extension}` unloaded.**")
except Exception as error:
output(f"An error occurred while unloading \"{extension}\" cog.")
await interaction.response.edit_message(content = f":warning: An error occurred while unloading **`cogs.{extension}`**.\n\n```py\n{error}\n```")

@app_commands.command(
name = "shutdown",
description = "Shuts down the bot."
)
async def shutdown(self, interaction: discord.Interaction):
if await self.bot.owner(interaction) == False:
return await interaction.response.edit_message(content = f":no_entry_sign: You don't have permissions to use this command.")
await interaction.response.edit_message(content = f":ballot_box_with_check: Bot Shutting Down...")
sys.exit()

async def setup(bot):
await bot.add_cog(Core(bot))
132 changes: 66 additions & 66 deletions cogs/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ class Events(commands.Cog):
def __init__(self, bot):
self.bot = bot

@commands.Cog.listener()
async def on_member_join(self, member):
@commands.Cog.listener("on_member_join")
async def user_authorisation(self, member):
log = int(env("LOG"))
log = await self.bot.fetch_channel(log)
if cool_utils.JSON.get_data(f"{member.id}") == "guests":
try:
role = discord.utils.get(member.guild.roles, id=int(env("GUEST_ROLE")))
await member.add_roles(role, reason="User registered as guest.")
role = discord.utils.get(member.guild.roles, id = int(env("GUEST_ROLE")))
await member.add_roles(role, reason = "User registered as guest.")
action = "Authorised as guest"
code = "01AG"
output(f"Guest \"{member.name}\" joined.")
Expand All @@ -40,7 +40,7 @@ async def on_member_join(self, member):

elif cool_utils.JSON.get_data(f"{member.id}") == "privileged":
try:
role = discord.utils.get(member.guild.roles, id=int(env("PRIVILEGED_ROLE")))
role = discord.utils.get(member.guild.roles, id = int(env("PRIVILEGED_ROLE")))
await member.add_roles(role, reason="User registered as privileged")
action = "Authorised as privileged"
code = "01AP"
Expand All @@ -52,8 +52,8 @@ async def on_member_join(self, member):

elif cool_utils.JSON.get_data(f"{member.id}") == "developer":
try:
role = discord.utils.get(member.guild.roles, id=int(env("DEVELOPER_ROLE")))
await member.add_roles(role, reason="User registered as developer")
role = discord.utils.get(member.guild.roles, id = int(env("DEVELOPER_ROLE")))
await member.add_roles(role, reason = "User registered as developer")
action = "Authorised as developer"
code = "01AD"
output(f"Privileged User \"{member.name}\" joined.")
Expand All @@ -64,31 +64,31 @@ async def on_member_join(self, member):

else:
try:
embed = discord.Embed(description=f"You've joined **{member.guild.name}**, although you are not authorised as a guest or privileged. Please contact a administrator of Senarc if you think that this isn't what was meant to happen.", colour=0xFED42A)
embed.set_author(name="Unauthorised Entry", icon_url="https://i.ibb.co/3YKyhxJ/black-exclamation-mark-on-yellow-260nw-1902354208-modified.png")
embed.set_footer(text="Security Bot", icon_url=member.guild.me.display_avatar)
await member.send(embed=embed)
embed = discord.Embed(description = f"You've joined **{member.guild.name}**, although you are not authorised as a guest or privileged. Please contact a administrator of Senarc if you think that this isn't what was meant to happen.", colour = 0xFED42A)
embed.set_author(name = "Unauthorised Entry", icon_url = "https://i.ibb.co/3YKyhxJ/black-exclamation-mark-on-yellow-260nw-1902354208-modified.png")
embed.set_footer(text = "Security Bot", icon_url = member.guild.me.display_avatar)
await member.send(embed = embed)
code = "01KU"
except Exception:
code = "EMKU"
try:
await member.kick(reason=f"Unauthorised User.")
await member.kick(reason = f"Unauthorised User.")
action = "Kicked for being Unauthorised"
except Exception:
action = "Kicked for being Unauthorised"
code = "EUKU"

_embed = discord.Embed(colour=0x2F3136)
_embed.set_author(name="Security Bot Events")
_embed.add_field(name="Action:", value=f"`ON_MESSAGE_JOIN`", inline=False)
_embed.add_field(name="Action taken:", value=action, inline=False)
_embed.add_field(name="Action Code:", value=f"`{code}`", inline=False)
_embed.add_field(name="User:", value=f"{member.name}#{member.discriminator}(`{member.id}`)", inline=False)
_embed.set_footer(text="Security Bot", icon_url=member.guild.me.display_avatar)
await log.send(embed=_embed)
_embed = discord.Embed(colour = 0x2F3136)
_embed.set_author(name = "Security Bot Events")
_embed.add_field(name = "Action:", value = f"`ON_MESSAGE_JOIN`", inline = False)
_embed.add_field(name = "Action taken:", value = action, inline = False)
_embed.add_field(name = "Action Code:", value = f"`{code}`", inline = False)
_embed.add_field(name = "User:", value = f"{member.name}#{member.discriminator}(`{member.id}`)", inline = False)
_embed.set_footer(text = "Security Bot", icon_url = member.guild.me.display_avatar)
await log.send(embed = _embed)

@commands.Cog.listener()
async def on_member_remove(self, member):
@commands.Cog.listener("on_member_remove")
async def member_leave_log(self, member):
output(f"User \"{member.name}\" left.")
log = int(env("LOG"))
log = await self.bot.fetch_channel(log)
Expand All @@ -98,68 +98,68 @@ async def on_member_remove(self, member):
code = "UB"
else:
code = "UL"
_embed = discord.Embed(colour=0x2F3136)
_embed.set_author(name="Security Bot Events")
_embed.add_field(name="Action:", value=f"`ON_MEMBER_REMOVE`", inline=False)
_embed.add_field(name="Action Code:", value=code, inline=False)
_embed.add_field(name="User:", value=f"{member.name}#{member.discriminator}(`{member.id}`)", inline=False)
_embed.set_footer(text="Security Bot", icon_url=member.guild.me.display_avatar)
await log.send(embed=_embed)
_embed = discord.Embed(colour = 0x2F3136)
_embed.set_author(name = "Security Bot Events")
_embed.add_field(name = "Action:", value = f"`ON_MEMBER_REMOVE`", inline = False)
_embed.add_field(name = "Action Code:", value = code, inline = False)
_embed.add_field(name = "User:", value = f"{member.name}#{member.discriminator}(`{member.id}`)", inline = False)
_embed.set_footer(text = "Security Bot", icon_url = member.guild.me.display_avatar)
await log.send(embed = _embed)

@commands.Cog.listener()
async def on_message_delete(self, message):
@commands.Cog.listener("on_message_delete")
async def message_delete_log(self, message):
if message.content == "":
return
output(f"{message.author.name}'s Message got deleted.")
log = int(env("LOG"))
log = await self.bot.fetch_channel(log)
_embed = discord.Embed(colour=0x2F3136)
_embed.set_author(name="Security Bot Events")
_embed.add_field(name="Action:", value=f"`ON_MESSAGE_DELETE`", inline=False)
_embed.add_field(name="Action Content:", value=f"```\n{message.content.replace('`', '')}\n```", inline=False)
_embed.add_field(name="User:", value=f"{message.author.name}#{message.author.discriminator}(`{message.author.id}`)", inline=False)
_embed.set_footer(text="Security Bot", icon_url=message.guild.me.display_avatar)
await log.send(embed=_embed)
_embed = discord.Embed(colour = 0x2F3136)
_embed.set_author(name = "Security Bot Events")
_embed.add_field(name = "Action:", value = f"`ON_MESSAGE_DELETE`", inline = False)
_embed.add_field(name = "Action Content:", value = f"```\n{message.content.replace('`', '')}\n```", inline = False)
_embed.add_field(name = "User:", value = f"{message.author.name}#{message.author.discriminator}(`{message.author.id}`)", inline = False)
_embed.set_footer(text = "Security Bot", icon_url = message.guild.me.display_avatar)
await log.send(embed = _embed)

@commands.Cog.listener()
async def on_message_edit(self, original_message, edited_message):
@commands.Cog.listener("on_message_edit")
async def message_edit_log(self, original_message, edited_message):
if original_message == "" or edited_message == "" or original_message.author.bot:
return
output(f"{original_message.author.name} Edited their message.")
log = int(env("LOG"))
log = await self.bot.fetch_channel(log)
_embed = discord.Embed(colour=0x2F3136)
_embed.set_author(name="Security Bot Events")
_embed.add_field(name="Action:", value=f"`ON_MESSAGE_EDIT`", inline=False)
_embed.add_field(name="Original Content:", value=f"```\n{original_message.content.replace('`', '')}\n```", inline=False)
_embed.add_field(name="Edited Content:", value=f"```\n{edited_message.content.replace('`', '')}\n```", inline=False)
_embed.add_field(name="User:", value=f"{original_message.author.name}#{original_message.author.discriminator}(`{original_message.author.id}`)", inline=False)
_embed.set_footer(text="Security Bot", icon_url=self.bot.user.display_avatar)
await log.send(embed=_embed)
_embed = discord.Embed(colour = 0x2F3136)
_embed.set_author(name = "Security Bot Events")
_embed.add_field(name = "Action:", value = f"`ON_MESSAGE_EDIT`", inline = False)
_embed.add_field(name = "Original Content:", value = f"```\n{original_message.content.replace('`', '')}\n```", inline = False)
_embed.add_field(name = "Edited Content:", value = f"```\n{edited_message.content.replace('`', '')}\n```", inline = False)
_embed.add_field(name = "User:", value = f"{original_message.author.name}#{original_message.author.discriminator}(`{original_message.author.id}`)", inline = False)
_embed.set_footer(text = "Security Bot", icon_url = self.bot.user.display_avatar)
await log.send(embed = _embed)

@commands.Cog.listener()
async def on_invite_create(self, invite):
@commands.Cog.listener("on_invite_create")
async def invite_creation_log(self, invite):
output(f"Auto-deleted a invite that was created by \"{invite.inviter.name}\".")
log = int(env("LOG"))
if cool_utils.JSON.get_data(f'{invite.inviter.id}') == 'notified':
null = None
else:
embed = discord.Embed(description="Your Invite has been auto-deleted, you are not allowed to create invites since this is a private server.\n\nIf you want to invite someone to the server you should use https://api.senarc.org/authorise/lab this will help us identify if a user is authorised to join the server.\n\nIf you think this is a mistake, please contact a administrator.", colour=0xFED42A)
embed.set_author(name="No Invites Allowed", icon_url="https://i.ibb.co/3YKyhxJ/black-exclamation-mark-on-yellow-260nw-1902354208-modified.png")
embed.set_footer(text="Security Bot", icon_url=self.bot.user.display_avatar)
await invite.inviter.send(embed=embed)
embed = discord.Embed(description = "Your Invite has been auto-deleted, you are not allowed to create invites since this is a private server.\n\nIf you want to invite someone to the server you should use https://api.senarc.org/authorise/lab this will help us identify if a user is authorised to join the server.\n\nIf you think this is a mistake, please contact a administrator.", colour = 0xFED42A)
embed.set_author(name = "No Invites Allowed", icon_url = "https://i.ibb.co/3YKyhxJ/black-exclamation-mark-on-yellow-260nw-1902354208-modified.png")
embed.set_footer(text = "Security Bot", icon_url = self.bot.user.display_avatar)
await invite.inviter.send(embed = embed)
cool_utils.register_value(f'{invite.inviter.id}', 'notified')
log = await self.bot.fetch_channel(log)
_embed = discord.Embed(colour=0x2F3136)
_embed.set_author(name="Security Bot Events")
_embed.add_field(name="Action:", value=f"`ON_INVITE_CREATE`", inline=False)
_embed.add_field(name="Action Taken:", value=f"`INVITE_DELETE`", inline=False)
_embed.add_field(name="Invite Code:", value=f"`{invite.code}`", inline=False)
_embed.add_field(name="Invite Channel:", value=f"<#{invite.channel.id}>", inline=False)
_embed.add_field(name="User:", value=f"{invite.inviter.name}#{invite.inviter.discriminator}(`{invite.inviter.id}`)", inline=False)
_embed.set_footer(text="Security Bot", icon_url=self.bot.user.display_avatar)
await invite.delete(reason="AUTO-DELETE-PROTECTION-RULE")
await log.send(embed=_embed)
_embed = discord.Embed(colour = 0x2F3136)
_embed.set_author(name = "Security Bot Events")
_embed.add_field(name = "Action:", value = f"`ON_INVITE_CREATE`", inline = False)
_embed.add_field(name = "Action Taken:", value = f"`INVITE_DELETE`", inline = False)
_embed.add_field(name = "Invite Code:", value = f"`{invite.code}`", inline = False)
_embed.add_field(name = "Invite Channel:", value = f"<#{invite.channel.id}>", inline = False)
_embed.add_field(name = "User:", value = f"{invite.inviter.name}#{invite.inviter.discriminator}(`{invite.inviter.id}`)", inline = False)
_embed.set_footer(text = "Security Bot", icon_url = self.bot.user.display_avatar)
await invite.delete(reason = "AUTO-DELETE-PROTECTION-RULE")
await log.send(embed = _embed)

def setup(bot):
bot.add_cog(Events(bot))
async def setup(bot):
await bot.add_cog(Events(bot))
Loading