【发布时间】:2020-08-07 17:20:48
【问题描述】:
我在 heroku 上托管了一个非常基本的 discord.py 机器人。它的唯一功能是每 24 小时从可能的消息列表中发送一条消息。它总是发送第一条消息然后停止。我在代码中找不到任何错误,当我降低时间并测试它在我的计算机上运行和在 heroku 中运行时,它工作正常。
这一切都在一个 python 文件中,其中包含几个 Heroku 所需的文件和两个用于消息的文本文档。
这里是主要脚本:
import discord
import time
import random as rng
clnt = discord.Client()
ch = 0 #channel id
cnt = 0 #amount of minutes on timer
lp = True #i think this is just a leftover variable from another version, i can't find anywhere i used it
@clnt.event
async def on_ready():
print('ready')
async def ph(): #function to send message
global ch
global lp
qts = open('quotes.txt') #get messages
qtz = qts.read().splitlines() #put messages in a list
qts.close() #close file
#if message file is empty, get the list from a backup file and put them into the first file, reseting the messages
if not qtz:
qts2 = open('quoteslog.txt')
qtz2 = qts2.read().splitlines()
qts2.close()
with open('quotes.txt', 'w') as f:
for i in qtz2:
f.write("%s\n" % i)
f.close()
qts = open('quotes.txt')
qtz = qts.read().splitlines()
qts.close()
#get random message from list
x = rng.randint(1, len(qtz))
x2 = x - 1
y = qtz[x2]
qtz.pop(x2)
open('quotes.txt', 'w').close() #clear the list
#rewrite the same file without the message sent
with open('quotes.txt', 'w') as f:
for i in qtz:
f.write("%s\n" % i)
f.close()
#used for messages with new lines
if y == 'ph1':
await ch.send("this is for one of the messages, it has new lines so it can't be re-inserted into a txt file")
await timer()
elif y == 'ph2':
await ch.send('same here')
await timer()
else:
#send message to channel and restart the timer
await ch.send(y)
await timer()
@clnt.event
async def on_message(m):
if m.author == clnt.user:
return
global ch
if m.content.startswith('send here'):
ch = clnt.get_channel(m.channel.id)
await m.channel.send('ok')
elif m.content.startswith('start'):
await timer()
async def timer(): #loops every 60 seconds, 1440 times, or 24hrs
global lp
while lp:
global cnt
time.sleep(60)
cnt += 1
if cnt == 1440:
cnt = 0 #reset timer and send message
await ph()
clnt.run('the discord bot id')
是的,我知道代码可能是垃圾格式,但据我所知,它应该可以正常工作,但事实并非如此。我什至不确定这是否是代码错误,也可能是 Heroku 问题,但我不知道。
如果有人有任何可能提供帮助的东西,将不胜感激!
【问题讨论】:
-
你的 Heroku 日志是怎么说的?
-
你也在定时器中循环。但随后计时器调用 ph。哪个呼叫计时器。因此,即使这段代码正常工作,它也会非常糟糕。
-
早上会检查日志并更新。至于 ph() 调用 timer(),是的,我很愚蠢,哈哈。我确定我之前实际上并没有那个,然后由于某种原因看到我没有它并认为我需要它,可能忘记了计时器是一个循环什么的。
标签: python heroku discord.py