python - 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)
你能帮我吗?
解决方案
您的问题是time.sleep
在结尾使用 used coins
。
这是暂停整个机器人的数量。您应该使用await asyncio.sleep(1)
这样只有所需的功能才会休眠。
注意:记得import asyncio
推荐阅读
- angularjs - 如何获取迭代元素angularjs的位置?
- selenium-webdriver - 为什么此代码显示错误,而按钮靠近密码字段后?
- php - Detect when a user changes any of their billing details (user meta)
- botframework - Speech functionality not working in IFrame
- laravel - 如何使用 Laravel 在引导模式中获取动态数据?
- macos - Safari 应用程序扩展:contentBlocker(withIdentifier:blockedResourcesWith:on:),它是如何工作的?如何让它工作?
- java - Java - 我尝试打印时出现线程池同步问题
- html - 桌面两列布局,移动一列,儿童不同顺序
- mongodb - BulkWriteError - MongoDB
- java - 使用流在 java 8 中拆分 entrySet 和过滤器