簡體   English   中英

Discord.py 如何使命令適用於某些角色?

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

我有這段代碼,如果有人具有“服務器開發人員”角色,則假設運行該命令,該命令將賦予某人“監獄”角色以用於該命令的阻止量,但它不起作用並且沒有給出錯誤。

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)

您忘記在命令頂部添加@bot.command() @commands.has_role("Server Developer")async def court(ctx, user_mentioned: discord.Member, time: int):

像這樣做:

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)

使用任何檢查裝飾器,如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)

您可以嘗試在命令中添加檢查,如下所示:

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