首页 > 解决方案 > bot.event on_ready 使我的命令停止工作 discord.py

问题描述

我编写了一个后台任务,该任务应在机器人运行后立即将消息发送到不和谐频道。但是现在我遇到了一个问题,我的命令不再起作用我试图删除该on_ready功能,然后该命令起作用了。但我需要on_ready而且我不知道

我的代码:

import discord
from random import *
from time import *
from discord.ext import tasks
from discord.ext import commands

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

@bot.command()
async def ping(ctx):
    await ctx.send(f"PONG {bot.latency * 1000} ms")

@bot.event
async def on_ready():
    coins.start()

@tasks.loop(seconds=time())
async def coins():
    print('We have logged in as {0.user}'.format(bot))
    while True:
        time = randrange(120, 300)
        coins = randrange(100, 500)
        em = discord.Embed(title=f"COINSS!!", description=f"{coins} coins have spawned",color=0xf1c40f)
        await bot.get_guild(guild-id).get_channel(channel-id).send(embed=em)
        sleep(time)

你能帮我吗?

标签: pythonfunctioneventscommanddiscord.py

解决方案


您的问题是time.sleep在结尾使用 used coins

这是暂停整个机器人的数量。您应该使用await asyncio.sleep(1)这样只有所需的功能才会休眠。

注意:记得import asyncio


推荐阅读