我在heroku上运行的discord.py机器人不断停止

2024-06-01 08:52:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我在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问题,但我不知道

如果任何人有任何可能的帮助,将不胜感激


Tags: thetxtsendmessagecloseifchannelopen
1条回答
网友
1楼 · 发布于 2024-06-01 08:52:44

我建议您使用BotCog类,这样效率会更高,它们在discord.py中提供,具有装饰器来定义循环函数。它们位于模块的discord.ext.commands部分。你可以做:

from discord.ext import commands, tasks

我刚刚在另一篇文章中回答了一个cog和循环函数的例子,你会发现它here。以下是适用于您的案例的相同结构:

# LoopCog.py
from discord.ext import commands, tasks
import random

class LoopCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.test_loop.change_interval(minutes = self.bot.cnt)

    @commands.Cog.listener("on_ready")
    async def on_ready(self):
        self.test_loop.start()

    @tasks.loop(hours=24)
    async def test_loop(self):
        # insert your ph function contents here
        # access the channel id via self.bot.ch

def setup(bot):
    bot.add_cog(LoopCog(bot))
# main.py
from discord.ext import commands

bot = commands.Bot(command_prefix = "!")
bot.ch = 0 
bot.cnt = 0
bot.load_extension("LoopCog")

@bot.event
async def on_ready():
    print("ready")

bot.run(token, reconnect = True)

我建议你去看一些教程。为Bot使用Client类不是正确的方法,如果继续使用Client,您必须对Bot中已经存在的所有内容进行编程

您可以找到BotCogtasks的API文档here

关于heroku,它将不幸地每24小时重新启动一次你的机器人,除非你选择付费的专业服务,包括24/7运行时间。如果在30分钟内没有收到任何请求,它也会将您的程序置于睡眠模式

相关问题 更多 >