如何为命令设置必需的角色?

时间:2018-03-22 02:46:06

标签: python discord discord.py

我正在尝试让我的Discord bot需要排名来执行Say命令,但我找不到让它工作的方法。我想问你们,你们是否可以帮助我设置执行命令所需的角色。

import asyncio
import discord
from discord.ext.commands import Bot

bot = Bot('>') # the <#4> is essential so people cannot share it via DMs.

@bot.command(pass_context = True)
async def Say(ctx, *args):
    mesg = ' '.join(args)
    await bot.delete_message(ctx.message)
    return await bot.say(mesg)

bot.run('Token')

1 个答案:

答案 0 :(得分:1)

Discord.py提供了一个简单的装饰器commands.has_role,用于按名称验证角色。 has_role使用check函数,两个函数都是未记录的命令扩展的一部分。

from discord.ext import commands

bot = commands.Bot('>')

@bot.command(pass_context = True)
@commands.has_role('admin')
async def Say(ctx, *args):
    mesg = ' '.join(args)
    await bot.delete_message(ctx.message)
    return await bot.say(mesg)

bot.run('Token')
相关问题