如何将拒绝消息添加到Discord py角色受限命令

时间:2019-10-27 10:36:13

标签: python python-3.x discord roles discord.py

我有此代码:

import discord
from discord.ext import commands, tasks
import random
from itertools import cycle
from discord.utils import get
import os

bot = commands.Bot(command_prefix='-')


TOKEN = ''


@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')



@bot.command()
@commands.has_role('Admin')
async def test(ctx):
    await ctx.send(":smiley: :wave: Hello, there! :heart: ")

bot.run(TOKEN)

如何设置拒绝消息? 我的意思是,如果有人使用该命令但没有管理员角色,则机器人会说类似 “您不是管理员好友!”

我已经尝试过了,但是没有用

@bot.command()
@commands.has_role('Admin')
async def test(ctx):
    await ctx.send(":smiley: :wave: Hello, there! :heart: ")
    else:
        await ctx.send("You can't use this!")

2 个答案:

答案 0 :(得分:1)

当用户调用测试命令并且他们没有“管理员”角色时,将引发命令。MissingRole错误。您可以使用error handling来捕捉。

import discord
from discord.ext import commands, tasks
import random
from itertools import cycle
from discord.utils import get
import os

TOKEN = ''

bot = commands.Bot(command_prefix='-')

@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')

@bot.command()
@commands.has_role('Admin')
async def test(ctx):
    await ctx.send(":smiley: :wave: Hello, there! :heart: ")

@test.error
async def test_error(ctx, error):
    if isinstance(error, commands.MissingRole):
        await ctx.send('''You aren't admin buddy!''')

bot.run('TOKEN')

答案 1 :(得分:-1)

如果用户没有角色,这将使您向用户发送消息。您还可以具有多个角色,而不是管理员。

@bot.command()
async def test(ctx):
    if "Admin" in ctx.author.roles:
        await ctx.send(":smiley: :wave: Hello, there! :heart: ")
    else:
        await ctx.send("You are not an admin!")