我遇到的问题是此代码将工作四到五分钟,如果文本文件更新,则该消息将显示在discord上。但是,无论是内存耗尽还是出现其他问题,机器人都会停止运行,并且不会再收到消息。
文本文件从源获取输入并随机添加。它也不会留下任何错误信息。
import discord
import asyncio
from discord.ext import commands
import time
global count
bot = commands.Bot(command_prefix="!")
def get_msg(): #Retrieves the last message from the text file
global last
global logs
logs = open('msg.txt','r')
msgs = logs.readlines()
logs.close()
last = msgs[len(msgs)-1]
def line_count(): #Retrieves the amount of lines in the text file
global count
r = open("msg.txt", 'r')
j = r.readlines()
r.close()
count = 0
for line in j:
count += 1
r.close()
return count
async def run(): #checks if the amount of lines has changed, if it has; displays message
global count
ct = line_count()
oc = ct
await bot.wait_until_ready()
while True:
if ct != oc:
oc = ct
get_msg()
prep = "Incoming: " + last
await bot.send_message(discord.Object('******'),prep)
oc = ct
else:
ct = line_count()
bot.loop.create_task(run())
bot.run('***********')