Added decorator for checking if eligable for managing sources

This commit is contained in:
2021-11-22 18:28:48 +01:00
parent b37c3f533a
commit c45bd8ced3
3 changed files with 34 additions and 5 deletions
+6 -5
View File
@@ -6,6 +6,7 @@ from discord.message import Message
from bot import TuneBot from bot import TuneBot
from context import CustomContext from context import CustomContext
from utils.classes import BaseCog from utils.classes import BaseCog
from utils.decorators import source_manager_only
from utils.EmbedGenerator import EmbedGenerator from utils.EmbedGenerator import EmbedGenerator
@@ -47,7 +48,7 @@ class SettingsCog(BaseCog, name="Settings"):
embed.title = f"AutoJoin disabled" embed.title = f"AutoJoin disabled"
await ctx.send(embed=embed) await ctx.send(embed=embed)
@commands.is_owner() @source_manager_only()
@commands.group( @commands.group(
name="source", name="source",
aliases=["src"], aliases=["src"],
@@ -63,7 +64,7 @@ class SettingsCog(BaseCog, name="Settings"):
embed.description = f"```{prefix}source list\n{prefix}source add <url>\n{prefix}source remove <url>\n{prefix}source sync```" embed.description = f"```{prefix}source list\n{prefix}source add <url>\n{prefix}source remove <url>\n{prefix}source sync```"
await ctx.send(embed=embed) await ctx.send(embed=embed)
@commands.is_owner() @source_manager_only()
@source.command(name="remove") @source.command(name="remove")
async def source_remove(self, ctx: CustomContext, source_url: str): async def source_remove(self, ctx: CustomContext, source_url: str):
"""Removes a source from the bot""" """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" embed.title = "Could not remove source, the specified source might not exist"
await ctx.send(embed=embed) await ctx.send(embed=embed)
@commands.is_owner() @source_manager_only()
@source.command(name="add") @source.command(name="add")
async def source_add(self, ctx: CustomContext, source_url: str): async def source_add(self, ctx: CustomContext, source_url: str):
""" """
@@ -117,7 +118,7 @@ class SettingsCog(BaseCog, name="Settings"):
else: else:
await ctx.send(embed=embed) await ctx.send(embed=embed)
@commands.is_owner() @source_manager_only()
@source.command(name="list", aliases=["ls"]) @source.command(name="list", aliases=["ls"])
async def source_list(self, ctx: CustomContext): async def source_list(self, ctx: CustomContext):
"""Display a list of sources""" """Display a list of sources"""
@@ -133,7 +134,7 @@ class SettingsCog(BaseCog, name="Settings"):
embed.description = description embed.description = description
await ctx.send(embed=embed) await ctx.send(embed=embed)
@commands.is_owner() @source_manager_only()
@source.command(name="sync") @source.command(name="sync")
async def source_sync(self, ctx: CustomContext): async def source_sync(self, ctx: CustomContext):
"""Forcefully resyncs all sources""" """Forcefully resyncs all sources"""
+1
View File
@@ -1,6 +1,7 @@
{ {
"token": "", "token": "",
"owner_ids": [194545408960102400, 190875175460405249], "owner_ids": [194545408960102400, 190875175460405249],
"manager_ids": [],
"prefixes": ["ck!"], "prefixes": ["ck!"],
"redis_url": "", "redis_url": "",
"redis_prefix": "", "redis_prefix": "",
+27
View File
@@ -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)