diff --git a/cogs/settings.py b/cogs/settings.py index d5f8d70..64cb903 100644 --- a/cogs/settings.py +++ b/cogs/settings.py @@ -6,6 +6,7 @@ from discord.message import Message from bot import TuneBot from context import CustomContext from utils.classes import BaseCog +from utils.decorators import source_manager_only from utils.EmbedGenerator import EmbedGenerator @@ -47,7 +48,7 @@ class SettingsCog(BaseCog, name="Settings"): embed.title = f"AutoJoin disabled" await ctx.send(embed=embed) - @commands.is_owner() + @source_manager_only() @commands.group( name="source", aliases=["src"], @@ -63,7 +64,7 @@ class SettingsCog(BaseCog, name="Settings"): embed.description = f"```{prefix}source list\n{prefix}source add \n{prefix}source remove \n{prefix}source sync```" await ctx.send(embed=embed) - @commands.is_owner() + @source_manager_only() @source.command(name="remove") async def source_remove(self, ctx: CustomContext, source_url: str): """Removes a source from the bot""" @@ -81,7 +82,7 @@ class SettingsCog(BaseCog, name="Settings"): embed.title = "Could not remove source, the specified source might not exist" await ctx.send(embed=embed) - @commands.is_owner() + @source_manager_only() @source.command(name="add") async def source_add(self, ctx: CustomContext, source_url: str): """ @@ -117,7 +118,7 @@ class SettingsCog(BaseCog, name="Settings"): else: await ctx.send(embed=embed) - @commands.is_owner() + @source_manager_only() @source.command(name="list", aliases=["ls"]) async def source_list(self, ctx: CustomContext): """Display a list of sources""" @@ -133,7 +134,7 @@ class SettingsCog(BaseCog, name="Settings"): embed.description = description await ctx.send(embed=embed) - @commands.is_owner() + @source_manager_only() @source.command(name="sync") async def source_sync(self, ctx: CustomContext): """Forcefully resyncs all sources""" diff --git a/config.json.sample b/config.json.sample index 34e2c81..874b4a2 100644 --- a/config.json.sample +++ b/config.json.sample @@ -1,6 +1,7 @@ { "token": "", "owner_ids": [194545408960102400, 190875175460405249], + "manager_ids": [], "prefixes": ["ck!"], "redis_url": "", "redis_prefix": "", diff --git a/utils/decorators.py b/utils/decorators.py new file mode 100644 index 0000000..4851dfd --- /dev/null +++ b/utils/decorators.py @@ -0,0 +1,27 @@ +from typing import Callable +from typing import TYPE_CHECKING +from typing import TypeVar + +from discord.ext.commands import check +from discord.ext.commands.errors import NotOwner + +if TYPE_CHECKING: + from context import CustomContext + +T = TypeVar("T") + + +def source_manager_only() -> Callable[[T], T]: + """ + A :func:`.check` that checks if the person invoking this command is allowed to modify the radio sources. + """ + + async def predicate(ctx: "CustomContext") -> bool: + is_manager = ctx.author.id in ctx.bot.config["manager_ids"] + is_owner = await ctx.bot.is_owner(ctx.author) + if not is_manager and not is_owner: + raise NotOwner("You are not allowed to modify the sources.") + + return True + + return check(predicate)