Как добавить сообщение об отклонении в команды с ограниченным доступом Discord py - PullRequest
0 голосов
/ 27 октября 2019

У меня есть этот код:

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 ]

1 голос
/ 27 октября 2019

Когда пользователь вызывает тестовую команду, и у него нет роли «Администратор», выдается ошибка command.MissingRole. Вы можете поймать это с помощью обработки ошибок .

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')
0 голосов
/ 29 октября 2019

Это позволит вам отправить сообщение пользователю, если у него нет роли. Вы также можете иметь несколько ролей вместо администратора.

@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!")
...