Skip to content

Commit

Permalink
The Update Update (#34)
Browse files Browse the repository at this point in the history
A general batch of QOL updates.
-  The /join command #23 
- Separated staff roles into perms ("staff") and pings ("observers")
- Shared Config object across cogs. (Less reloads needed)
- _Probably_ less reload-needy owner check
- The Parameters described
  • Loading branch information
Tech-TTGames authored Mar 2, 2023
2 parents b84994d + 670321f commit 573e6e9
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 48 deletions.
6 changes: 3 additions & 3 deletions example_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"ticket_users": [508391840525975553],
"info_ticket_users": "User IDs. The bot will create threads under channels created by those users. The currently inputed ID is for the main Tickets bot",
"staff": [111111111111111111, 222222222222222222],
"info_staff": "Role IDs. They will be added to the threads when they are created.",
"staff_ping": true,
"info_staff_ping": "If true, the bot will ping the staff role when a thread is created.",
"info_staff": "Role IDs. They will have permissions to use /respond and /join.",
"observers": [111111111111111111],
"info_observer": "Role IDs. The bot will ping these roles when a thread is created. Does not give them permissions.",
"open_msg": "Staff notes for Ticket $channel.",
"info_open_msg": "Message to be sent when a thread is created. Leave blank for no message insert $channel for the channel mention.",
"staff_team": "Staff Team",
Expand Down
17 changes: 11 additions & 6 deletions extchecks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
CONFG = Config("offline")


async def is_owner(interaction: discord.Interaction):
"""Checks if interaction user is owner."""
if interaction.user.id in CONFG.owner:
return True
await interaction.response.send_message("Error 400: Forbidden", ephemeral=True)
return False
def is_owner_gen(confg: Config = CONFG):
"""A probably bad way to work around the poor design of discord.py's app_command checks."""

async def is_owner(interaction: discord.Interaction):
"""Checks if interaction user is owner."""
if interaction.user.id in confg.owner:
return True
await interaction.response.send_message("Error 400: Forbidden", ephemeral=True)
return False

return is_owner
35 changes: 30 additions & 5 deletions main_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ async def on_channel_create(self, channel):
logging.info(
"Created thread %s for %s", nts_thrd.name, channel.name
)
if self._config.staff_ping:
if self._config.observers:
inv = await nts_thrd.send(
" ".join([role.mention for role in cnfg.staff])
" ".join([role.mention for role in cnfg.observers])
)
await inv.delete()
if self._config.strip_buttons:
Expand Down Expand Up @@ -105,7 +105,7 @@ async def on_message(self, message: discord.Message) -> None:
@app_commands.command(
name="ping", description="The classic ping command. Checks the bot's latency."
)
async def ping(self, ctx):
async def ping(self, ctx: discord.Interaction):
"""This command is used to check if the bot is online."""
await ctx.response.send_message(
"Pong! The bot is online.\nPing: "
Expand All @@ -132,6 +132,7 @@ async def version(self, ctx: discord.Interaction):
@app_commands.command(name="respond", description="Respond to a ticket as the bot.")
@app_commands.guild_only()
@app_commands.checks.has_any_role(*CONFG.staff_ids)
@app_commands.describe(message="The message to send to the ticket.")
async def respond(self, ctx: discord.Interaction, message: str):
"""
EXTENSION 2: Anonymised staff responses.
Expand All @@ -157,9 +158,33 @@ async def respond(self, ctx: discord.Interaction, message: str):
)
await ctx.channel.send(f"**{self._config.staff_team}:** " + message)

@app_commands.command(name="join", description="Join a ticket's staff notes.")
@app_commands.guild_only()
@app_commands.checks.has_any_role(*CONFG.staff_ids)
async def join(self, ctx: discord.Interaction):
"""
EXTENSION 3: Staff notes.
This command is used to join a ticket's staff notes.
"""
if isinstance(ctx.channel, discord.TextChannel):
try:
await ctx.channel.threads[0].add_user(ctx.user)
except IndexError:
await ctx.response.send_message(
"No staff notes thread found.", ephemeral=True
)
return
await ctx.response.send_message(
"Joined the staff notes for this ticket.", ephemeral=True
)
return
await ctx.response.send_message(
"Invalid command execution space.", ephemeral=True
)


async def setup(bot: commands.Bot):
"""Setup function for the cog."""
global CONFG # pylint: disable=global-statement
CONFG = Config(bot)
await bot.add_cog(Utility(bot, Config(bot)))
CONFG = getattr(bot, "config", Config(bot))
await bot.add_cog(Utility(bot, CONFG))
23 changes: 15 additions & 8 deletions override.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
from discord import app_commands
from discord.ext import commands

from extchecks import is_owner
from extchecks import is_owner_gen
from variables import PROG_DIR, Config

IS_OWNER = is_owner_gen()


class Overrides(
commands.GroupCog, name="override", description="Owner override commands."
Expand All @@ -23,7 +25,7 @@ def __init__(self, bot: commands.Bot, config: Config):
logging.info("Loaded %s", self.__class__.__name__)

@app_commands.command(name="reload", description="Reloads the bot's cogs.")
@app_commands.check(is_owner)
@app_commands.check(IS_OWNER)
async def reload(self, ctx: discord.Interaction):
"""Reloads the bot's cogs."""
await ctx.response.send_message("Reloading cogs...")
Expand All @@ -37,7 +39,7 @@ async def reload(self, ctx: discord.Interaction):
logging.info("Finished syncing tree.")

@app_commands.command(name="restart", description="Restarts the bot.")
@app_commands.check(is_owner)
@app_commands.check(IS_OWNER)
async def restart(self, ctx: discord.Interaction):
"""Restarts the bot."""
await ctx.response.send_message("Restarting...")
Expand All @@ -47,7 +49,7 @@ async def restart(self, ctx: discord.Interaction):
@app_commands.command(
name="pull", description="Pulls the latest changes from the git repo."
)
@app_commands.check(is_owner)
@app_commands.check(IS_OWNER)
async def pull(self, ctx: discord.Interaction):
"""Pulls the latest changes from the git repo."""
await ctx.response.send_message("Pulling latest changes...")
Expand All @@ -73,7 +75,8 @@ async def pull(self, ctx: discord.Interaction):
)

@app_commands.command(name="logs", description="Sends the logs.")
@app_commands.check(is_owner)
@app_commands.check(IS_OWNER)
@app_commands.describe(id_no="Log ID (0 for latest log)")
async def logs(self, ctx: discord.Interaction, id_no: int = 0):
"""Sends the logs."""
await ctx.response.defer(thinking=True)
Expand All @@ -90,7 +93,7 @@ async def logs(self, ctx: discord.Interaction, id_no: int = 0):
logging.info("Logs sent.")

@app_commands.command(name="config", description="Sends the config.")
@app_commands.check(is_owner)
@app_commands.check(IS_OWNER)
async def config(self, ctx: discord.Interaction):
"""Sends the config."""
await ctx.response.defer(thinking=True)
Expand All @@ -106,7 +109,8 @@ async def config(self, ctx: discord.Interaction):
logging.info("Config sent.")

@app_commands.command(name="dumpconfig", description="Deletes all data from config")
@app_commands.check(is_owner)
@app_commands.check(IS_OWNER)
@app_commands.describe(confirm="Confirm dump")
async def dump_config(self, ctx: discord.Interaction, confirm: bool):
"""Dump config of bot"""
if confirm:
Expand All @@ -120,4 +124,7 @@ async def dump_config(self, ctx: discord.Interaction, confirm: bool):

async def setup(bot: commands.Bot):
"""Setup function for the cog."""
await bot.add_cog(Overrides(bot, Config(bot)))
global IS_OWNER # pylint: disable=global-statement
cnfg = getattr(bot, "config", Config(bot))
IS_OWNER = is_owner_gen(cnfg)
await bot.add_cog(Overrides(bot, cnfg))
54 changes: 37 additions & 17 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
from discord import app_commands
from discord.ext import commands

from extchecks import is_owner
from extchecks import is_owner_gen
from variables import Config

IS_OWNER = is_owner_gen()


class Settings(commands.GroupCog, name="settings", description="Settings for the bot."):
"""Provides commands to change the bot's settings."""
Expand All @@ -21,6 +23,7 @@ def __init__(self, bot: commands.Bot, config: Config):
@app_commands.command(name="tracked", description="Change the tracked users.")
@app_commands.checks.has_permissions(administrator=True)
@app_commands.guild_only()
@app_commands.describe(user="The user to track/untrack.")
async def change_tracked(self, ctx: discord.Interaction, user: discord.User):
"""
This command is used to change the tracked users.
Expand All @@ -39,9 +42,10 @@ async def change_tracked(self, ctx: discord.Interaction, user: discord.User):
@app_commands.command(name="staff", description="Change the staff roles.")
@app_commands.checks.has_permissions(administrator=True)
@app_commands.guild_only()
@app_commands.describe(role="The role to add/remove from staff roles.")
async def change_staff(self, ctx: discord.Interaction, role: discord.Role):
"""
This command is used to change the staff roles, Staff is added to the notes threads.
This command is used to change the staff roles, Staff are allowed to use staff commands.
If a role is already here, it will be removed.
"""
rspns = ctx.response
Expand All @@ -58,25 +62,33 @@ async def change_staff(self, ctx: discord.Interaction, role: discord.Role):
)
self._config.staff = stff

@app_commands.command(
name="staffping", description="Change the staff ping setting."
)
@app_commands.command(name="observers", description="Change the observers roles.")
@app_commands.checks.has_permissions(administrator=True)
@app_commands.guild_only()
async def change_staffping(self, ctx: discord.Interaction):
@app_commands.describe(role="The role to add/remove from observers roles.")
async def change_observers(self, ctx: discord.Interaction, role: discord.Role):
"""
This command is used to change the staff ping setting.
If it is on, it will be turned off, and vice versa.
This command is used to change the observers roles, which are pinged one new notes threads.
If a role is already here, it will be removed.
"""
stf_ping = self._config.staff_ping
self._config.staff_ping = not stf_ping
await ctx.response.send_message(
f"Staff ping is now {not stf_ping}", ephemeral=True
)
rspns = ctx.response
obsrvrs = self._config.observers
if role in obsrvrs:
obsrvrs.remove(role)
await rspns.send_message(
f"Removed {role.mention} from ping staff roles.", ephemeral=True
)
else:
obsrvrs.append(role)
await rspns.send_message(
f"Added {role.mention} to ping staff roles.", ephemeral=True
)
self._config.observers = obsrvrs

@app_commands.command(name="openmsg", description="Change the open message.")
@app_commands.checks.has_permissions(administrator=True)
@app_commands.guild_only()
@app_commands.describe(message="The new open message.")
async def change_openmsg(self, ctx: discord.Interaction, message: str):
"""This command is used to change the open message."""
self._config.open_msg = message
Expand All @@ -87,6 +99,7 @@ async def change_openmsg(self, ctx: discord.Interaction, message: str):
@app_commands.command(name="staffteam", description="Change the staff team's name.")
@app_commands.checks.has_permissions(administrator=True)
@app_commands.guild_only()
@app_commands.describe(name="The new staff team's name.")
async def change_staffteam(self, ctx: discord.Interaction, name: str):
"""This command is used to change the staff team's name."""
self._config.staff_team = name
Expand Down Expand Up @@ -120,6 +133,7 @@ async def toggle_button_stripping(self, ctx: discord.Interaction):
)
@app_commands.checks.has_permissions(administrator=True)
@app_commands.guild_only()
@app_commands.describe(role="The role to add/remove from community support roles.")
async def change_community_roles(
self, ctx: discord.Interaction, role: discord.Role
):
Expand All @@ -143,7 +157,7 @@ async def change_community_roles(
self._config.community_roles = comsup

@app_commands.command(name="guild", description="Change the guild.")
@app_commands.check(is_owner)
@app_commands.check(IS_OWNER)
@app_commands.guild_only()
async def change_guild(self, ctx: discord.Interaction):
"""This command is used to change the guild. Use in the guild you want to change to."""
Expand All @@ -158,7 +172,10 @@ async def change_guild(self, ctx: discord.Interaction):
)

@app_commands.command(name="owner", description="Change the owners of the bot.")
@app_commands.check(is_owner)
@app_commands.check(IS_OWNER)
@app_commands.describe(
user="The user to add to owners. WARNING: This will not remove them."
)
async def change_owner(self, ctx: discord.Interaction, user: discord.User):
"""
This command is used to change the owner users.
Expand All @@ -175,5 +192,8 @@ async def change_owner(self, ctx: discord.Interaction, user: discord.User):


async def setup(bot: commands.Bot):
"""Adds the cog to the bot."""
await bot.add_cog(Settings(bot, Config(bot)))
"""Setup function for the cog."""
global IS_OWNER # pylint: disable=global-statement
cnfg = getattr(bot, "config", Config(bot))
IS_OWNER = is_owner_gen(cnfg)
await bot.add_cog(Settings(bot, cnfg))
1 change: 1 addition & 0 deletions start_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ async def on_ready():


if __name__ == "__main__":
setattr(bot, "config", cnfg)
bot.run(scrt.token, log_handler=handler, root_logger=True)
23 changes: 14 additions & 9 deletions variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

# v[major].[minor].[release].[build]
# MAJOR and MINOR version changes can be compatibility-breaking
VERSION = "v0.0.2.5"
VERSION = "v0.0.3.0"
PROG_DIR = os.path.dirname(os.path.realpath(__file__))

intents = discord.Intents.default()
Expand Down Expand Up @@ -115,14 +115,19 @@ def staff(self, value: List[discord.Role]) -> None:
self.update()

@property
def staff_ping(self) -> bool:
"""Returns if staff should be pinged on ticket creation"""
return self._config.get("staff_ping", True)

@staff_ping.setter
def staff_ping(self, value: bool) -> None:
"""Sets if staff should be pinged on ticket creation"""
self._config["staff_ping"] = value
def observers(self) -> List[discord.Role]:
"""List of roles who are pinged in staff notes"""
staff = []
for role in self._config.get("observers", []):
stf_role = self.guild.get_role(role)
if isinstance(stf_role, discord.Role):
staff.append(stf_role)
return staff

@observers.setter
def observers(self, value: List[discord.Role]) -> None:
"""Sets the list of users who are pinged in staff notes"""
self._config["observers"] = [role.id for role in value]
self.update()

@property
Expand Down

0 comments on commit 573e6e9

Please sign in to comment.