【问题标题】:discord.py command not working after I add @commands.cooldown decorator添加 @commands.cooldown 装饰器后,discord.py 命令不起作用
【发布时间】:2021-02-17 14:18:01
【问题描述】:

我正在尝试使用具有 120 秒冷却时间的“查找”命令制作一个不和谐的机器人,但在我添加冷却装饰器后,“查找”命令不再起作用。

这是我的代码:

import discord
from dotenv import load_dotenv
import os
import json
import random
from discord.ext import commands
from discord.ext.commands import cooldown, CommandOnCooldown
from discord.ext.commands.cooldowns import BucketType

load_dotenv()

bot = commands.Bot(command_prefix="nut ")

@bot.event
async def on_ready():
    await bot.change_presence(activity=discord.Game("with nuts ????"))
    print("Logged in as {0.user}".format(bot))

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return

    if message.content.lower() == "nut":
        global r
        r = random.randint(1,3)
        if r == 1:
            await message.add_reaction("\N{CHESTNUT}")
        if r == 2:
            await message.add_reaction("\N{NUT AND BOLT}")
        if r == 3:
            await message.add_reaction("\N{PEANUTS}")

for filename in os.listdir(r"C:\Program Files\Microsoft VS Code\programs\NutBot\cogs"):
     if filename.endswith(".py"):
            bot.load_extension(f'cogs.{filename[:-3]}')

bot.run(os.getenv("TOKEN"))

我唯一的齿轮是:

import discord
from dotenv import load_dotenv
import os
import json
import random
from discord.ext import commands
from discord.ext.commands import cooldown, CommandOnCooldown
from discord.ext.commands.cooldowns import BucketType

class Currency(commands.Cog):
    def __init__(self,bot):
        self.bot = bot
    @commands.command()
    @commands.cooldown(1, 120, commands.BucketType.user)
    async def find(self, ctx):
        global r
        r = int(float(random.randint(1,100))/2)
        if r == 0:
            return await ctx.send(embed=discord.Embed(title="You suck, you didn't find any nuts.", color=0xe0ad53))
        if r == 1:
            return await ctx.send(embed=discord.Embed(title="You found a nut!", color=0xe0ad53))
        if r == 50:
            return await ctx.send(embed=discord.Embed(title="You found 50 nuts! Woah...", color=0xe0ad53))
        else:
            return await ctx.send(embed=discord.Embed(title="You found " + str(r) + " nuts!", color=0xe0ad53))

    @find.error
    async def find_error(self, ctx, error):
        if isinstance(error, commands.CommandOnCooldown):
            await ctx.send(embed=discord.Embed(title="Woah slow down, buddy.", description = "Your puny body is still recovering from the last expedition. You should rest for {int(error.retry_after)} more seconds.", color=0xd60000))

    @commands.command()
    async def search(self, ctx):
        await find.invoke(ctx)

def setup(bot):
    bot.add_cog(Currency(bot))

‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎ ‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎ ‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎ ‎‎‎‎‎‎

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:
    @bot.command()
    @commands.cooldown(1, 120, commands.BucketType.user)
    async def find(ctx):
        global r
        r = int(float(random.randint(1,100))/2)
        if r == 0:
            return await ctx.send(embed=discord.Embed(title="You suck, you didn't find any nuts.", color=0xe0ad53))
        if r == 1:
            return await ctx.send(embed=discord.Embed(title="You found a nut!", color=0xe0ad53))
        if r == 50:
            return await ctx.send(embed=discord.Embed(title="You found 50 nuts! Woah...", color=0xe0ad53))
        else:
            return await ctx.send(embed=discord.Embed(title="You found " + str(r) + " nuts!", color=0xe0ad53))
    
    @find.error
    async def find_error(ctx, error):
        if isinstance(error, commands.CommandOnCooldown):
            await ctx.send(embed=discord.Embed(title="Woah slow down, buddy.", description = "Your puny body is still recovering from the last expedition. You should rest for {int(error.retry_after)} more seconds.", color=0xd60000))
    

    这里有一些需要注意的地方,在所有情况下都添加return await(我在我的机器人中尝试过这个,当我只运行一次时它实际上向我发送了两次消息。你的主命令没有问题。

    您的错误处理程序可能无法按您希望的那样工作。 description = "Your puny body is still recovering from the last expedition. You should rest for {:.2f} more seconds. 这一行没有使用 f'string (这意味着您将看到 {:.2f} 原样}

    即使添加了f'string,您也会收到错误消息。使用这个int(error.retry_after) [这将显示剩余的冷却时间。]

    除此之外,我发现您的代码没有其他错误。

    【讨论】:

    • 我是新手,如果这是一个愚蠢的问题,我很抱歉,但为什么要使用 return await 而不是 await?
    • 按照你说的做之后,“find”仍然不会让机器人说什么,但是如果有帮助的话,取出“@commands.cooldown”可以让它再次工作。
    • 在 discord UnidentifiedComponent#8118 上添加我并向您的测试服务器发送邀请
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    • 2019-07-23
    • 1970-01-01
    • 2021-11-05
    • 2021-08-11
    相关资源
    最近更新 更多