How do we allow access to the team only to people with certain id?
-
I'm writing a discord bot on the python, and I need a function that adds a specified number of xp to the user's field if the id of the caller is on the admins list. That's what I did:
@bot.command() async def add_xp(ctx, member: discord.Member=None, xp=None): """Функция add_xp прибавляет к полю xp пользователя указанное число xp. (только для админов). """ if member is None: emb = discord.Embed(title="Укажите пользователя!", colour=discord.Colour.red()) await ctx.send(embed=emb) else: if xp is None: emb = discord.Embed(title="Укажите количество XP!", colour=discord.Colour.red()) await ctx.send(embed=emb) else: emb = discord.Embed(title=f"Пользователю {member} добавлено {xp}XP", colour=discord.Colour.green()) cursor.execute(f"UPDATE users SET xp = xp + {xp} WHERE id = {member.id}") await ctx.send(embed=emb) await ctx.message.delete()
How do we proceed with the id user test? I wish it wasn't inside the function, but in the decorator, something.
-
You can use special decorators:
- https://discordpy.readthedocs.io/en/stable/ext/commands/api.html#discord.ext.commands.is_owner
- https://discordpy.readthedocs.io/en/stable/ext/commands/api.html#discord.ext.commands.has_role
- https://discordpy.readthedocs.io/en/stable/ext/commands/api.html#discord.ext.commands.has_any_role
- https://discordpy.readthedocs.io/en/stable/ext/commands/api.html#discord.ext.commands.has_permissions
from discord.ext import commands import discord as ds import config
bot = commands.Bot(commands.when_mentioned_or(config.bot_prefix), owner_ids=config.owner_ids)
@commands.is_owner()
async def test1(ctx):
await ctx.reply('You're owner! In owner_ids!')@commands.has_role('Admin')
async def test2(ctx):
await ctx.reply('You have role "Admin"!')@commands.has_role(11432413241)
async def test3(ctx):
await ctx.reply('You have role by id 11432413241!')@commands.has_any_role('Admin', 'Administrator')
async def test4(ctx):
await ctx.reply('You have role "Admin" or "Administrator"!')@commands.has_permissions(administrator=True, ban_members=True)
async def test5(ctx):
await ctx.reply('You can administer the server and ban members!')bot.run(config.ds_token, reconnect=True)
config.py
ds_token = '<token>'
guild_ids = {<guild_id>}
owner_ids = {<your_id>}
bot_prefix = '<command_prefix>'