首页 > 解决方案 > discord.py 使用字典

问题描述

我正在尝试制作一个不和谐的机器人,其中一个功能是它会编写您必须执行的任务/作业。我这样做是为了让人们可以为其他人编写任务。我怎样才能做到这一点?我所拥有的只是一种编写任务的方法,但没有办法打印它。有人可以帮助我用一种简单的方法为 discord.py 制作这本字典吗?这是我拥有的未完成代码的示例:

elif str(message.content).lower() == ">homeworkadd":
     await message.channel.send("Enter the homework subject you want to add. Start your text with '>hs *subject and what to do*'")

elif ">hs" in message.content:
    whattodo = str(message.content[4:])
    homework.append(whattodo)
    await message.channel.send("Your subject and your definition ('{}') have been installed successfully!".format(whattodo))
elif str(message.content).lower() == ">homework":
    print(homework)
    await message.content.send(homework)```

标签: pythondictionarydiscord.py

解决方案


不是一个完整的答案,但我建议您使用至少将作业保存到文件的数据库。如果您的机器人崩溃或您需要重新启动它,那么您将丢失存储在数据库中的所有作业。

我建议您使用数据库,它可以非常简单地设置和编写(查看https://docs.python.org/3/library/sqlite3.html)。它还允许您更好地扩展/添加更多功能。基线是,避免将其保存到字典/列表/等。


推荐阅读