This repository has been archived by the owner on May 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
databases.py
159 lines (139 loc) · 7.32 KB
/
databases.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from rich.progress import track
from loguru import logger
import botfunc
import pymysql
import sqlite3
import cache_var
import requests
import aiomysql
# 查看config.yaml中的enable_mysql是否为开启,若开启则使用MySQL,若不是则使用SQLite
try:
if botfunc.get_config('enable_mysql'):
conn = pymysql.connect(host=botfunc.get_cloud_config('MySQL_Host'), port=botfunc.get_cloud_config('MySQL_Port'),
user=botfunc.get_cloud_config('MySQL_User'),
password=botfunc.get_cloud_config('MySQL_Pwd'), charset='utf8mb4',
database=botfunc.get_cloud_config('MySQL_db'))
cursor = conn.cursor()
else:
conn = sqlite3.connect(botfunc.get_config('sqlite_name'))
except pymysql.err.InternalError:
conn = pymysql.connect(host=botfunc.get_cloud_config('MySQL_Host'), port=botfunc.get_cloud_config('MySQL_Port'),
user=botfunc.get_cloud_config('MySQL_User'),
password=botfunc.get_cloud_config('MySQL_Pwd'), charset='utf8mb4')
cursor = conn.cursor()
cursor.execute("""create database if not exists %s""", (botfunc.get_cloud_config('MySQL_db'),))
cursor.execute("""create table if not exists admin
(
uid bigint unsigned default '0' not null
primary key
) ENGINE = innodb DEFAULT CHARACTER SET = "utf8mb4" COLLATE = "utf8mb4_general_ci" """)
cursor.execute("""create table if not exists sblist
(
uid bigint unsigned default '0' not null
primary key
) ENGINE = innodb DEFAULT CHARACTER SET = "utf8mb4" COLLATE = "utf8mb4_general_ci" """)
cursor.execute("""create table if not exists blacklist
(
uid bigint unsigned not null
primary key,
op bigint unsigned not null
) ENGINE = innodb DEFAULT CHARACTER SET = "utf8mb4" COLLATE = "utf8mb4_general_ci" """)
cursor.execute("""create table if not exists bread
(
id int unsigned auto_increment
primary key,
level int unsigned default '0' not null,
time int unsigned default '0' not null,
bread int unsigned default '0' not null,
experience int unsigned default '0' not null
) ENGINE = innodb DEFAULT CHARACTER SET = "utf8mb4" COLLATE = "utf8mb4_general_ci" """)
cursor.execute("""create table if not exists wd
(
wd tinytext null,
count int unsigned null
) ENGINE = innodb DEFAULT CHARACTER SET = "utf8mb4" COLLATE = "utf8mb4_general_ci" """)
cursor.execute("""create table if not exists woodenfish
(
uid bigint unsigned not null comment '赛博(QQ)账号'
primary key,
time bigint unsigned not null comment '上次计算时间',
level int unsigned default '0' not null comment '木鱼等级',
de bigint default 0 not null comment '功德',
e double default 0 not null comment 'log10值',
ee double default 0 not null comment 'log10^10值',
nirvana double default 1 not null comment '涅槃重生次数',
ban int default 0 not null comment '封禁状态',
dt bigint default 0 not null comment '封禁结束时间',
end_time bigint default 0 not null comment '最近一次调用时间',
hit_count int default 0 not null comment '一周期内的调用次数'
) ENGINE = innodb DEFAULT CHARACTER SET = "utf8mb4" COLLATE = "utf8mb4_general_ci" """)
cursor.execute("""CREATE TABLE IF NOT EXISTS `six` (
`uid` bigint UNSIGNED NOT NULL PRIMARY KEY COMMENT 'QQ号' ,
`count` int UNSIGNED NOT NULL DEFAULT 0 COMMENT '6 的次数',
`ti` bigint UNSIGNED NOT NULL DEFAULT 0 COMMENT '最后一次"6"发送时间'
) ENGINE = innodb DEFAULT CHARACTER SET = "utf8mb4" COLLATE = "utf8mb4_general_ci" """)
cursor.execute("""CREATE TABLE IF NOT EXISTS `no_six` (
`gid` bigint UNSIGNED NOT NULL PRIMARY KEY COMMENT '群号'
) ENGINE = innodb DEFAULT CHARACTER SET = "utf8mb4" COLLATE = "utf8mb4_general_ci" """)
cursor.execute("""CREATE TABLE IF NOT EXISTS `inm` (
`gid` bigint UNSIGNED NOT NULL PRIMARY KEY COMMENT '群号'
) ENGINE = innodb DEFAULT CHARACTER SET = "utf8mb4" COLLATE = "utf8mb4_general_ci" """)
conn.commit()
cursor.execute('SELECT wd, count FROM wd')
cache_var.sensitive_words = [x[0] for x in cursor.fetchall()]
if not cache_var.sensitive_words:
print("未找到敏感词库!即将从GitHub仓库拉取……(请保证能正常访问jsDelivr)")
input("> 是否继续?(回车 继续 / ^C 退出)")
# 色情类
d = requests.get(
"https://cdn.jsdelivr.net/gh/fwwdn/sensitive-stop-words@master/%E8%89%B2%E6%83%85%E7%B1%BB.txt").text.split(
',\n')
# 政治类
d.extend(
requests.get(
"https://cdn.jsdelivr.net/gh/fwwdn/sensitive-stop-words@master/%E6%94%BF%E6%B2%BB%E7%B1%BB.txt"
).text.split(',\n')
)
# 违法类
d.extend(
requests.get(
"https://cdn.jsdelivr.net/gh/fwwdn/sensitive-stop-words@master/%E6%B6%89%E6%9E%AA%E6%B6%89%E7%88%86"
"%E8%BF%9D%E6%B3%95%E4%BF%A1%E6%81%AF%E5%85%B3%E9%94%AE%E8%AF%8D.txt"
).text.split(',\n')
)
d.pop(-1) # 上面的这些加载出来在列表末尾会多出一堆乱码,故删除,如果你需要魔改此部分请视情况自行删除
for w in track(d, description="Loading"):
cursor.execute("INSERT INTO wd VALUES (%s, 0)", (w,))
try:
conn.commit()
except pymysql.err.DataError:
conn.rollback()
cursor.execute('SELECT wd, count FROM wd')
cache_var.sensitive_words = [x[0] for x in cursor.fetchall()]
cursor.execute('SELECT gid FROM no_six')
cache_var.no_6 = [x[0] for x in cursor.fetchall()]
cursor.execute('SELECT uid FROM admin')
if not cursor.fetchall():
admin_uid = int(input("未找到任何一个op!请输入你(op)的QQ号:"))
cursor.execute("INSERT INTO admin VALUES (%s)", (admin_uid,))
conn.commit()
cursor.execute('SELECT gid FROM inm')
cache_var.inm = [x[0] for x in cursor.fetchall()]
conn.close()
async def run_sql(sql, arg):
conn = await aiomysql.connect(host=botfunc.get_cloud_config('MySQL_Host'),
port=botfunc.get_cloud_config('MySQL_Port'),
user=botfunc.get_cloud_config('MySQL_User'),
password=botfunc.get_cloud_config('MySQL_Pwd'), charset='utf8mb4',
db=botfunc.get_cloud_config('MySQL_db'), loop=loop)
cur = await conn.cursor()
await cur.execute(sql, arg)
await cur.execute("commit")
await cur.close()
conn.close()
def database_create(name: str):
conn = aiomysql.connect(host=get_cloud_config('MySQL_Host'),
port=get_cloud_config('MySQL_Port'),
user=get_cloud_config('MySQL_User'),
password=get_cloud_config('MySQL_Pwd'), charset='utf8mb4',
db=get_cloud_config('MySQL_db'), loop=loop)