该机器人的工作是检查某人何时进入等级1,然后在不和谐和引燃的人员通道中说一条消息。频道中的人可以使用!comment <#多条消息返回>来对该成员发表评论。由于某种原因,它在服务器上可以正常运行,但是在运行几天后,有时会丢失评论。请回到我身边。 Bot由heroku托管。
import discord, os, time
from discord.ext import tasks, commands
TOKEN = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
client = discord.Client()
msg = None
targetGuild = "xxxxxxxxxxxx"
msgList = []
@client.event
async def on_ready():
global channel
for guild in client.guilds:
print(guild.name)
if guild.name == targetGuild:
print(f"RoleCheck has connected to {guild.name}")
channel = guild.get_channel(xxxxxxxxxxxxxxxxxxxx)
@client.event
async def on_member_update(before, after):
newRole = next(role for role in after.roles if role not in before.roles)
if newRole.name == "1":
global msg
msg = await channel.send(f"IGN:{after.nick} \nDiscord:{after.name}")
print(f"IGN:{after.nick} \nDiscord:{after.name}")
msgList.append(msg)
print(msgList)
@client.event
async def on_message(message):
global msg, msgList
if message.content.startswith("!comment"):
print("TEST")
if message.content[9].isnumeric() and not message.content[10].isnumeric():
print("1")
msg = msgList[int("-"+message.content[9])]
_content = msg.content + '\n' + message.author.name + ': ' + message.content[11:]
await msg.edit(content=_content)
elif message.content[10].isnumeric():
print("2")
await channel.send("Role check only supports single digit numbers")
else:
print("3")
_content = msg.content + '\n' + message.author.name + ': ' + message.content[9:]
await msg.edit(content=_content)
elif message.content.startswith("!rolecheckhelp"):
await channel.send("---RoleCheck--- \n!comment - will add a comment on the last message with whatever you write after\n !help = will show the help screen\n Made with python and the discord.py API by xxxxxxxxxx")
client.run(TOKEN)
答案 0 :(得分:1)
以下代码可以解决您的问题。如Jawad在评论中所述,heroku每24小时重新启动一次。要解决此问题,您需要创建一个保留代码的系统。我在下面使用python _pickle
库完成了此操作,将更新时的列表存储为物理文件。机器人重新启动时,也会on_ready
读取并加载此文件。
为便于控制,我还添加了一个管理命令,该命令仅在使用该命令的人在您的服务器上拥有完全清除msgList
的管理员权限时才会触发。
import _pickle as pickle
import discord, os, time
from discord.ext import tasks, commands
TOKEN = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
client = discord.Client()
msg = None
targetGuild = "xxxxxxxxxxxx"
msgList = []
@client.event
async def on_ready():
global channel, msgList
for guild in client.guilds:
print(guild.name)
if guild.name == targetGuild:
print(f"RoleCheck has connected to {guild.name}")
channel = guild.get_channel(xxxxxxxxxxxxxxxxxxxx)
# Open the messages.p cache and read contents after restart
with open("./messages.p", 'r') as pfile:
msgList = pickle.load(pfile)
@client.event
async def on_member_update(before, after):
newRole = next(role for role in after.roles if role not in before.roles)
if newRole.name == "1":
global msg, msgList
# Open the messages.p cache and read contents prior to updating
with open("./messages.p", 'r') as pfile:
msgList = pickle.load(pfile)
msg = await channel.send(f"IGN:{after.nick} \nDiscord:{after.name}")
print(f"IGN:{after.nick} \nDiscord:{after.name}")
msgList.append(msg)
print(msgList)
# Open messages.p cache and overwrite it with new content after update
with open("./messages.p", 'w') as pfile:
pickle.dump(msgList, pfile)
@client.event
async def on_message(message):
global msg, msgList
if message.content.startswith("!comment"):
print("TEST")
if message.content[9].isnumeric() and not message.content[10].isnumeric():
print("1")
msg = msgList[int("-"+message.content[9])]
_content = msg.content + '\n' + message.author.name + ': ' + message.content[11:]
await msg.edit(content=_content)
elif message.content[10].isnumeric():
print("2")
await channel.send("Role check only supports single digit numbers")
else:
print("3")
_content = msg.content + '\n' + message.author.name + ': ' + message.content[9:]
await msg.edit(content=_content)
elif message.content.startswith("!rolecheckhelp"):
await channel.send("---RoleCheck--- \n!comment - will add a comment on the last message with whatever you write after\n !help = will show the help screen\n Made with python and the discord.py API by xxxxxxxxxx")
elif message.content.lower().startswith("!purge_msg_list"):
if message.author.guild_permissions.administrator:
# Clear msgList and set it to [], then open messages.p cache and overwrite
msgList = []
with open("./messages.p", "w") as pfile:
pfile.dump(msgList, pfile)
await channel.send("Messages in msgList have been purged.")
client.run(TOKEN)