Make checks in Bank, Economy and Trivia cogs Permissions-friendly (#3672)

* Create converters.py

* Update trivia.py

* Create checks.py

* Update checks.py

* Update checks.py

* Update trivia.py

* Update checks.py

* Update checks.py

* Update trivia.py

* Update bank.py

* Update economy.py

* Update trivia.py

* Update checks.py

* Update checks.py

* Update __init__.py
This commit is contained in:
jack1142
2020-03-28 23:24:12 +01:00
committed by GitHub
parent fce8186759
commit 97d77f5c51
6 changed files with 81 additions and 73 deletions

View File

@@ -1,4 +1,4 @@
from .bank import Bank, check_global_setting_guildowner, check_global_setting_admin
from .bank import Bank, is_owner_if_bank_global
def setup(bot):

View File

@@ -9,10 +9,13 @@ from redbot.core.bot import Red # Only used for type hints
_ = Translator("Bank", __file__)
def check_global_setting_guildowner():
def is_owner_if_bank_global():
"""
Command decorator. If the bank is not global, it checks if the author is
either the guildowner or has the administrator permission.
Command decorator. If the bank is global, it checks if the author is
bot owner, otherwise it does nothing.
When used on the command, this should be combined
with permissions check like `guildowner_or_permissions()`.
"""
async def pred(ctx: commands.Context):
@@ -30,33 +33,6 @@ def check_global_setting_guildowner():
return commands.check(pred)
def check_global_setting_admin():
"""
Command decorator. If the bank is not global, it checks if the author is
either a bot admin or has the manage_guild permission.
"""
async def pred(ctx: commands.Context):
author = ctx.author
if not await bank.is_global():
if not isinstance(ctx.channel, discord.abc.GuildChannel):
return False
if await ctx.bot.is_owner(author):
return True
if author == ctx.guild.owner:
return True
if ctx.channel.permissions_for(author).manage_guild:
return True
admin_role_ids = await ctx.bot.get_admin_role_ids(ctx.guild.id)
for role in author.roles:
if role.id in admin_role_ids:
return True
else:
return await ctx.bot.is_owner(author)
return commands.check(pred)
@cog_i18n(_)
class Bank(commands.Cog):
"""Bank"""
@@ -67,7 +43,7 @@ class Bank(commands.Cog):
# SECTION commands
@check_global_setting_guildowner()
@is_owner_if_bank_global()
@checks.guildowner_or_permissions(administrator=True)
@commands.group(autohelp=True)
async def bankset(self, ctx: commands.Context):
@@ -117,22 +93,25 @@ class Bank(commands.Cog):
await bank.set_global(not cur_setting)
await ctx.send(_("The bank is now {banktype}.").format(banktype=word))
@is_owner_if_bank_global()
@checks.guildowner_or_permissions(administrator=True)
@bankset.command(name="bankname")
@check_global_setting_guildowner()
async def bankset_bankname(self, ctx: commands.Context, *, name: str):
"""Set the bank's name."""
await bank.set_bank_name(name, ctx.guild)
await ctx.send(_("Bank name has been set to: {name}").format(name=name))
@is_owner_if_bank_global()
@checks.guildowner_or_permissions(administrator=True)
@bankset.command(name="creditsname")
@check_global_setting_guildowner()
async def bankset_creditsname(self, ctx: commands.Context, *, name: str):
"""Set the name for the bank's currency."""
await bank.set_currency_name(name, ctx.guild)
await ctx.send(_("Currency name has been set to: {name}").format(name=name))
@is_owner_if_bank_global()
@checks.guildowner_or_permissions(administrator=True)
@bankset.command(name="maxbal")
@check_global_setting_guildowner()
async def bankset_maxbal(self, ctx: commands.Context, *, amount: int):
"""Set the maximum balance a user can get."""
try: