不和谐的机器人可以在哪里存储信息[discord.py] [英] Where is the place that discord bots can store information [discord.py]

查看:84
本文介绍了不和谐的机器人可以在哪里存储信息[discord.py]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对python不太陌生,但对discord.py不熟悉。我曾尝试过discord.py手册,但没有找到可以在Discord机器人上存储一些临时变量的地方。

I am not too new to python but new to discord.py. I have tried to go through the discord.py manual but did not find where I can store some temporary variable on a discord bot.

discord.py手册: http://discordpy.readthedocs.io/en/latest/api.html

discord.py manual: http://discordpy.readthedocs.io/en/latest/api.html

例如,在PHP SESSION中,我们可以将信息存储在SESSION()上。 discord.py是否具有相同类型的内容?

For example, in PHP SESSION, we can store information on the SESSION(). Did discord.py has the same kind of things?

例如,如果我们有用户 A和用户 B。 A将存储为 A_Object的对象,例如消息等。与 B_Object相似,但与 A_Object不同。在discord.py中,是否存在类似的功能?

For example, if we have user "A" and user "B". "A" will be stored as an object of "A_Object", such as messages etc. Similar for "B_Object" but will be different from "A_Object". In discord.py, is there a function like that?

非常感谢您的帮助!

推荐答案

您可以使用sqlite数据库。在def中输入内容:

You can use sqlite database. Write inside your def:

# define database
import sqlite3
conn = sqlite3.connect("my_database.db")
cursor = conn.cursor()
# get stored object from database
sql = "SELECT * FROM my_table WHERE field_1=?"
cursor.execute(sql, [(value_1)])
data = cursor.fetchall()
# if object does not exist, create it
if len(data) == 0:
    sql = "INSERT INTO my_table VALUES (?, ?)"
    cursor.execute(sql, [(value_1), (value_2)])
# if stored object exist and we need update it
elif ...:
    sql = "UPDATE my_table SET field_2 = ? WHERE field_1 = ?"
    cursor.execute(sql, [(value_2), (value_1)])
else:
    # get data from first object
    value_of_field_1 = data[0][0]
    # get data from third object
    value_of_field_2 = data[2][1]
# close database connection
conn.commit()
conn.close()

my_database.db-是一个sqlite db文件,应与bot的.py文件存储在同一文件夹中。

my_database.db - is a sqlite db file and should be stored in sa same folder with bot's .py file.

这篇关于不和谐的机器人可以在哪里存储信息[discord.py]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆