Q
Q
Quaasaar2020-05-23 17:53:10
Python
Quaasaar, 2020-05-23 17:53:10

Discord.py how to properly use @bot.check_once?

I have code:

import discord
from discord.ext import commands

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


# Для удобства
@bot.event
async def on_ready():
    print('Бот готов - Online...')


# Проверка
@bot.check_once
def whitelist(ctx):
    if 1 > 0:
        print('1>0')


# Тест
@bot.command()
async def test(ctx):
    await ctx.send('Nice job!')


bot.run("TOKEN")
I need to make it so that when I write the !test command, the test should start, and if the test condition is true, then the !test command should continue executing.
I tried to do it in different ways, but everything is even, the code just does not continue to execute the !test...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Nevzorov, 2020-06-16
@fixator10

The function in the Bot.check_once decorator should return a "bool-alike" value (any value that will be interpreted as a bool. Roughly speaking, anything that supports bool(var)).
For example:

WHITELIST = [990298932007060792]

@bot.check_once()
def whitelist(ctx):
    print(f"Check is triggered: {ctx.author} executed {ctx.command}")
    return ctx.author.id in WHITELIST  # Если ID пользователя в указанном выше списке - вернет True
    # В противном случае, результат будет оцениваться как False (функция без указания return возвращает None, а bool(None) == False

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question