【发布时间】:2021-04-30 11:55:04
【问题描述】:
我制作了一个不和谐的机器人,它应该每天 22:00 UTC 时间从文本文件中发送报价。虽然它确实工作了一次,但似乎第一天之后 while 循环就中断了。
import discord
import random
import datetime
client = discord.Client()
text_channel = None
@client.event
async def on_message(message):
global text channel
if message.author == client.user:
return
cntnt = message.content
if cntnt == '!add_channel':
text_channel = message.channel
print("Assigned ", text_channel, " as channel.")
await message.delete()
if cntnt == '!start_quote':
if text_channel != None:
await message.delete()
while True:
current_time = datetime.datetime.now()
print(current_time)
await asyncio.sleep(1)
time1 = current_time.hour
time2 = current_time.minute
if time1 == 22 and time2 == 0:
with open('quotes.txt', 'r', encoding='utf8') as file:
quotes = file.readlines()
index = random.randint(0, len(quotes))
answer = str(quotes[index + 1])
await text_channel.send("Today's quote: " + answer)
await asyncio.sleep(60)
else:
print("No channel assigned.")
await message.delete()
client.run("TOKEN")
【问题讨论】:
-
修复代码的缩进。照原样给出语法错误。
-
我怀疑机器人应该在 on_message() 中这么久。
-
我认为是这样的,因为一切都指向
message.content或一般message。但似乎代码的某些部分丢失了,例如cntnt从未声明过。 @ThomasWeller -
这是我复制粘贴时犯的一个错误,我现在修复了它
标签: python python-3.x discord discord.py