简体   繁体   English

Discord.py 如何使命令适用于某些角色?

[英]Discord.py how to make a command work for certain roles?

I have this code which if someone has a role "Server Developer" it is suppose to run the command which is to give someone the role "Jail" for the determent amount n the command but it doesn't work and gives no errors.我有这段代码,如果有人具有“服务器开发人员”角色,则假设运行该命令,该命令将赋予某人“监狱”角色以用于该命令的阻止量,但它不起作用并且没有给出错误。

import discord
from discord.ext import commands
import ctx
import re
import time
from time import sleep
import asyncio

PREFIX = "$"
bot = commands.Bot(command_prefix=PREFIX, description="Hi")

@commands.has_role("Server Developer")
@bot.command()
async def court(ctx, user_mentioned: discord.Member, time: int):
    user_id = message.mentions[0].id
    user = message.mentions[0]
    role = discord.utils.get(message.guild.roles, name="Jail")
    await user_mentioned.add_roles(role)
    await ctx.send(
        f"sending <@{user_id}> to court!"
    )
    await asyncio.sleep(time)
    await user_mentioned.remove_roles(role)

bot.run(TOKNE_GOES_HERE)

You forgot to add @bot.command() on top of your command.您忘记在命令顶部添加@bot.command() Add it between @commands.has_role("Server Developer") and async def court(ctx, user_mentioned: discord.Member, time: int):@commands.has_role("Server Developer")async def court(ctx, user_mentioned: discord.Member, time: int):

Do it like this:像这样做:

import discord
from discord.ext import commands
import ctx
import re
import time
from time import sleep
import asyncio

PREFIX = "$"
bot = commands.Bot(command_prefix=PREFIX, description="Hi")

@commands.has_role("Server Developer")
@bot.command()
async def court(ctx, user_mentioned: discord.Member, time: int):
    user_id = ctx.message.mentions[0].id
    user = ctx.message.mentions[0]
    role = discord.utils.get(ctx.message.guild.roles, name="Jail")
    await user_mentioned.add_roles(role)
    await ctx.send(
        f"sending <@{user_id}> to court!"
    )
    await asyncio.sleep(time)
    await user_mentioned.remove_roles(role)

bot.run(TOKNE_GOES_HERE)

Using any check decorators like has_role or is_owner , doesn't make the function a command, so you would still have to add @commands.command() or @bot.command() :使用任何检查装饰器,如has_roleis_owner ,不会使 function 成为命令,因此您仍然必须添加@commands.command()@bot.command()

@bot.command()
@commands.has_role("Server Developer")
async def court(ctx, user_mentioned: discord.Member, time: int):
    user_id = message.mentions[0].id
    user = message.mentions[0]
    role = discord.utils.get(message.guild.roles, name="Jail")
    await user_mentioned.add_roles(role)
    await ctx.send(
        f"sending <@{user_id}> to court!"
    )
    await asyncio.sleep(time)
    await user_mentioned.remove_roles(role)

bot.run(TOKNE_GOES_HERE)

you could try adding a check inside the command, something like this:您可以尝试在命令中添加检查,如下所示:

if not discord.utils.get(ctx.guild.roles, name="Server Developer") in ctx.user.roles:
    return
else:
    # do something

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM