【发布时间】: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