【问题标题】:Discord.py How can message be saved and visible in code?Discord.py 如何在代码中保存和显示消息?
【发布时间】:2021-05-02 00:26:25
【问题描述】:

一段时间以来,我一直在为我和我的朋友制作 Discord 机器人。 我想让他们能够通过在频道上使用命令保存将一些消息添加到表中。 我已经做到了,但是每次机器人重新启动后表格都会变空。 如何在代码中保存和显示消息?

这就是我的代码的一部分

table = [

]

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

【问题讨论】:

    标签: python discord save


    【解决方案1】:

    table 是 RAM 中的变量。重新启动机器人后,它将被清除。要在重新启动机器人后保存消息,您需要将它们保存到硬盘驱动器。 你可以使用

    with open("saved_messages.txt", "a") as myfile:
        myfile.write(a)
    

    这会将每条消息附加到saved_messages.txt。每次启动机器人时,您都需要从硬盘加载此文件

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

    【讨论】:

      猜你喜欢
      • 2021-02-11
      • 2015-09-06
      • 2014-03-02
      • 1970-01-01
      • 1970-01-01
      • 2019-07-07
      • 1970-01-01
      • 2021-02-19
      • 1970-01-01
      相关资源
      最近更新 更多