Skip to content

Commit

Permalink
Merge pull request #79 from HenryXiaoYang/dev
Browse files Browse the repository at this point in the history
v2.1.1
  • Loading branch information
HenryXiaoYang authored Nov 20, 2024
2 parents 751a6ac + face580 commit 89f08c4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
2 changes: 1 addition & 1 deletion plans/daily_greeting.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def get_history_today() -> str:
url = "https://api.03c3.cn/api/history"
response = requests.get(url).json()

if response.code != 200 or response.get("data") != 200:
if response.code != 200 or response.get("code") != 200:
return ""

data = response.get("data")
Expand Down
33 changes: 13 additions & 20 deletions utils/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,34 @@

# sb queue内置库没法获取返回值


@singleton
class BotDatabase:
def __init__(self):
self.database_column = [("WXID", "TEXT PRIMARY KEY"), ("NICKNAME", "TEXT"), ("POINTS", "INT"), ("SIGNINSTAT", "INT"),("WHITELIST", "INT"), ("PRIVATE_GPT_DATA", "TEXT")]

if not os.path.exists("userdata.db"): # 检查数据库是否存在
logger.warning("检测到数据库不存在,正在创建数据库")
conn = sqlite3.connect("userdata.db")
c = conn.cursor()
c.execute(
"""CREATE TABLE USERDATA (WXID TEXT PRIMARY KEY, NICKNAME TEXT, POINTS INT, SIGNINSTAT INT, WHITELIST INT, PRIVATE_GPT_DATA TEXT)"""
)

# compose create table sql
create_table_sql = f"CREATE TABLE USERDATA ("
for column in self.database_column:
create_table_sql += f"{column[0]} {column[1]}, "
create_table_sql = create_table_sql[:-2] + ")"

c.execute(create_table_sql)

conn.commit()
c.close()
conn.close()

logger.warning("已创建数据库")

self.database = sqlite3.connect(
"userdata.db", check_same_thread=False
) # 连接数据库

# 检测数据库是否有正确的列
logger.info("[数据库]检测数据库是否有正确的列")
correct_columns = {'WXID': 'TEXT PRIMARY KEY', 'NICKNAME': 'TEXT', 'POINTS': 'INT', 'SIGNINSTAT': 'INT',
'WHITELIST': 'INT',
'PRIVATE_GPT_DATA': 'TEXT'}
cursor = self.database.cursor()
cursor.execute("PRAGMA table_info(USERDATA)")
columns = cursor.fetchall()
column_names = [column[1] for column in columns]
for c in correct_columns.keys():
if c not in column_names:
sql = f"ALTER TABLE USERDATA ADD COLUMN {c} {correct_columns[c]}"
cursor.execute(sql)
logger.info(f"[数据库]已添加列 {c}")

self.wxid_list = self._get_wxid_list() # 获取已有用户列表

self.executor = ThreadPoolExecutor(
Expand Down Expand Up @@ -185,7 +178,7 @@ def get_highest_points(self, num):
cursor = self.database.cursor()

try:
sql = "select * from USERDATA order by POINTS DESC, SIGNINSTAT LIMIT 0,?"
sql = "SELECT * FROM USERDATA ORDER BY POINTS DESC LIMIT ?;"
arg = (num,)
cursor.execute(sql, arg)
result = cursor.fetchall()
Expand Down

0 comments on commit 89f08c4

Please sign in to comment.