【发布时间】:2021-10-15 05:03:04
【问题描述】:
目标:
我只是想从@tasks.loop() 向不和谐频道发送消息,而不需要来自@client.event async def on_message 的不和谐消息变量。 discord bot 使用 uptime 机器人在 repl.it 中持续运行。
方法/背景:
一个简单的while True 循环将不适用于我将应用此原则的较大项目,Karen's answer here 对此进行了详细说明。我现在使用的是 @tasks.loop(),Lovesh 在这里快速详细介绍了它:(see Lovesh's work)。
问题:
使用most common method to send a message in discord using discord.py 时仍然出错。该错误与await channel.send( ) 方法有关。两条消息都不会不和谐地发送。 Here is the error message.
代码:
from discord.ext import tasks, commands
import os
from keep_alive import keep_alive
import time
token = os.environ['goofyToken']
# Set Up Discord Client & Ready Function
client = discord.Client()
channel = client.get_channel(CHANNEL-ID)
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@tasks.loop(count=1)
async def myloop(word):
await channel.send(word)
@client.event
async def on_message(message):
msg = message.content
if msg.startswith('!'):
message_to_send = 'Hello World!'
await channel.send(message_to_send)
myloop.start(message_to_send)
keep_alive()
client.run(token)
尝试的解决方案:
可以使用语法await message.channel.send('Hello World!) 从on_message 事件发送消息。但是,我就是不能用这个。代码由uptimerobot 在线运行,这是一个免费网站,可在 repl.it 上 ping 存储库。当机器人 ping 存储库时,消息变量会丢失,因此循环将停止扫描我正在处理的更大项目中的数据,这给了我这个问题。
【问题讨论】:
标签: python discord discord.py repl.it