Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Discord.py如何在代码中保存和显示消息?_Python_Discord_Save - Fatal编程技术网

Python Discord.py如何在代码中保存和显示消息?

Python Discord.py如何在代码中保存和显示消息?,python,discord,save,Python,Discord,Save,一段时间以来,我一直在为我和我的朋友制造不和。 我想让他们能够使用命令saveonchannels向表中添加一些消息。 我已经成功了,但每次机器人重启后,表都会变空。 如何在代码中保存和显示消息 这就是我要说的代码的一部分 table = [ ] @bot.command() async def save(ctx): a = ctx.message.content.replace('.save','') table.append(a) await ctx.messag

一段时间以来,我一直在为我和我的朋友制造不和。 我想让他们能够使用命令saveonchannels向表中添加一些消息。 我已经成功了,但每次机器人重启后,表都会变空。 如何在代码中保存和显示消息

这就是我要说的代码的一部分

table = [

]

@bot.command()
async def save(ctx):
    a = ctx.message.content.replace('.save','')
    table.append(a)
    await ctx.message.channel.send('saved')

是RAM中的一个变量。一旦重新启动bot,它将被清除。要在重新启动bot之后保存消息,您需要将它们保存到硬盘。 你可以用

with open("saved_messages.txt", "a") as myfile:
    myfile.write(a)
这会将每条消息附加到
保存的\u messages.txt
。每次启动bot时,您都需要从硬盘加载此文件

saved_messages = open('saved_messages.txt', 'r')
Lines = saved_messages.readlines()
 
for line in Lines:
    table.append(line)