我在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问题,但我不知道
如果任何人有任何可能的帮助,将不胜感激
我建议您使用
Bot
和Cog
类,这样效率会更高,它们在discord.py中提供,具有装饰器来定义循环函数。它们位于模块的discord.ext.commands
部分。你可以做:我刚刚在另一篇文章中回答了一个cog和循环函数的例子,你会发现它here。以下是适用于您的案例的相同结构:
我建议你去看一些教程。为
Bot
使用Client
类不是正确的方法,如果继续使用Client
,您必须对Bot
中已经存在的所有内容进行编程您可以找到Bot、Cog和tasks的API文档here
关于heroku,它将不幸地每24小时重新启动一次你的机器人,除非你选择付费的专业服务,包括24/7运行时间。如果在30分钟内没有收到任何请求,它也会将您的程序置于睡眠模式
相关问题 更多 >
编程相关推荐