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

Update staging #24

Merged
merged 2 commits into from
Jun 30, 2024
Merged
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
58 changes: 41 additions & 17 deletions cogs/watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import discord
import logging

from typing import Optional
from typing import Optional, Union
from discord.ext import bridge, commands, pages, tasks

from .user_config import UserConfigFile
Expand Down Expand Up @@ -40,9 +40,7 @@
]

DELETE_AFTER = 120
COOLDOWN = 5

ADMIN_ONLY_PERM = discord.Permissions(administrator=True)
COOLDOWN = 15


class CDNCog(commands.Cog):
Expand All @@ -66,6 +64,15 @@ def __init__(self, bot: bridge.Bot):
self.cdn_auto_refresh.start()
self.integrity_check.start()

@staticmethod
def user_is_admin_or_owner(ctx: discord.ApplicationContext):
if ctx.guild.owner_id == ctx.user.id:
return True

return ctx.user.top_role.permissions.administrator

__ADMIN_CHECKS = [user_is_admin_or_owner]

@tasks.loop(hours=24)
async def integrity_check(self):
await self.bot.wait_until_ready()
Expand Down Expand Up @@ -119,8 +126,8 @@ def get_command_link(

async def notify_owner_of_exception(
self,
error,
ctx: discord.ApplicationContext | bridge.BridgeApplicationContext | None = None,
error: Union[discord.ApplicationCommandError, str],
ctx: Optional[discord.ApplicationContext] = None,
):
"""This is supposed to notify the owner of an error, but doesn't always work."""
owner = await self.bot.fetch_user(self.bot.owner_id) # type: ignore
Expand Down Expand Up @@ -471,13 +478,27 @@ async def cdn_auto_refresh(self):

@commands.Cog.listener(name="on_command_error")
@commands.Cog.listener(name="on_application_command_error")
async def handle_command_error(self, ctx: discord.ApplicationContext, exception):
async def handle_command_error(
self,
ctx: discord.ApplicationContext,
exception: Union[
commands.CommandOnCooldown,
commands.NotOwner,
discord.CheckFailure,
discord.ApplicationCommandError,
],
):
delete_after = DELETE_AFTER

if isinstance(exception, commands.CommandOnCooldown):
timestamp = get_discord_timestamp(
round(time.time() + exception.retry_after), relative=True
)
message = f"This command is on cooldown. Try again {timestamp}"
elif isinstance(exception, commands.NotOwner):
delete_after = int(exception.retry_after)
elif isinstance(exception, commands.NotOwner) or isinstance(
exception, discord.CheckFailure
):
message = f"You do not have permission to use this command."
else:
message = "I have encountered an error handling your command. The Titans have been notified."
Expand All @@ -487,9 +508,12 @@ async def handle_command_error(self, ctx: discord.ApplicationContext, exception)
)
await self.bot.notify_owner_of_command_exception(ctx, exception) # type: ignore

await ctx.interaction.response.send_message(
message, ephemeral=True, delete_after=DELETE_AFTER
)
await ctx.respond(message, ephemeral=True, delete_after=delete_after)

@commands.Cog.listener(name="on_unknown_application_command")
async def handle_unk_command(self, ctx: discord.ApplicationContext):
message = "Unknown command. Please try again in a few minutes."
await ctx.respond(message, ephemeral=True, delete_after=DELETE_AFTER)

# DISCORD COMMANDS

Expand Down Expand Up @@ -520,12 +544,12 @@ async def cdn_branches(self, ctx: bridge.BridgeApplicationContext):

@watchlist_commands.command(
name="add",
default_member_permissions=ADMIN_ONLY_PERM,
checks=__ADMIN_CHECKS,
input_type=str,
min_length=3,
max_length=500,
)
@commands.cooldown(1, COOLDOWN, commands.BucketType.user)
@commands.cooldown(1, COOLDOWN, commands.BucketType.guild)
async def cdn_add_to_watchlist(
self, ctx: bridge.BridgeApplicationContext, branch: str
):
Expand Down Expand Up @@ -602,12 +626,12 @@ async def cdn_add_to_watchlist(

@watchlist_commands.command(
name="remove",
default_member_permissions=ADMIN_ONLY_PERM,
checks=__ADMIN_CHECKS,
input_type=str,
min_length=3,
max_length=500,
)
@commands.cooldown(1, COOLDOWN, commands.BucketType.user)
@commands.cooldown(1, COOLDOWN, commands.BucketType.guild)
async def cdn_remove_from_watchlist(
self, ctx: bridge.BridgeApplicationContext, branch: str
):
Expand Down Expand Up @@ -659,9 +683,9 @@ async def cdn_watchlist(self, ctx: bridge.BridgeApplicationContext):

@channel_commands.command(
name="set",
default_member_permissions=ADMIN_ONLY_PERM,
checks=__ADMIN_CHECKS,
)
@commands.cooldown(1, 5, commands.BucketType.user)
@commands.cooldown(1, COOLDOWN, commands.BucketType.guild)
async def cdn_set_channel(
self,
ctx: bridge.BridgeApplicationContext,
Expand Down
Loading