-
Notifications
You must be signed in to change notification settings - Fork 0
/
qq.py
255 lines (208 loc) · 8.09 KB
/
qq.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import random
import traceback
from tokenize import group
from urllib.parse import urlencode
import numpy as np # 这行不能删
import requests
from mirai import At, GroupMessage, Image, Shutdown, Startup
from business import *
from config import bot
from schedule import scheduler
from util import *
session = requests.Session()
proxies = {
'http': 'http://localhost:7890/',
'https': 'https://localhost:7890/',
}
def hello(event, msg):
if msg == '您好':
return bot.send(event, 'Hello, World!')
async def pixiv_image(event, msg):
if msg == '来点二次元' or msg == '来张二次元':
# pixiv_url = 'https://www.pixiv.net/ajax/top/illust?mode=all&lang=zh'
# pixiv_response = session.get(pixiv_url, proxies=proxies)
# print(pixiv_response)
pixiv_url = 'https://api.pixiv.cx/rank/?'
pixiv_params = {
'format': 'json',
'mode': 3
}
pixiv_response = session.get(
pixiv_url, params=pixiv_params, verify=False)
if pixiv_response.status_code != 200:
await bot.send(f'出现网络问题:<{pixiv_response.status_code}>\n{pixiv_response.text}\n请访问{pixiv_url}查看')
pixiv_response = pixiv_response.json()
setu_url = pixiv_response['url']
print(setu_url)
img_data = session.get(setu_url, verify=False)
with open('setu.jpg', 'wb') as file:
file.write(img_data.content)
await bot.send(event, Image(path='setu.jpg'))
async def erciyuan(bot, event, msg):
if msg == '来点二次元' or msg == '来张二次元':
url = 'http://iw233.cn/api/Random.php'
response = session.get(url)
with open('setu.jpg', 'wb') as file:
file.write(response.content)
await bot.send(event, Image(path='setu.jpg'))
def web_summary(event, msg: str):
msg = streamline(msg)
if msg.startswith('http'):
return bot.send(event, get_url_brief(msg))
def calc(event, method, payload: str):
if method == 'calc':
group_id = event.sender.group.id
qq_id = event.sender.id
can_read, can_write, who, msg = auth(group_id, qq_id)
if not can_write:
return bot.send(event, msg)
payload = payload.replace('(', '(').replace(')', ')')
result = eval(pre_process(payload))
return bot.send(event, str(result))
def help(event, method, _):
if method != 'help':
return None
group_id = event.sender.group.id
qq_id = event.sender.id
can_read, can_write, who, msg = auth(group_id, qq_id)
help_msg = f'欢迎使用星空凛的机器人!\n您的身份为:{who},可使用如下功能:\n'
for msg in HELP_BASE:
help_msg += msg + '\n'
if can_read:
for msg in HELP_READ:
help_msg += msg + '\n'
if can_write:
for msg in HELP_WRITE:
help_msg += msg + '\n'
return bot.send(event, help_msg)
def daka(event, method, _):
if method == 'daka':
status_code, result = do_daka()
if status_code == 200 and result['e'] == 0:
return bot.send(event, f'打卡成功!\nstatus_code:{status_code}\nresponse:{str(result)}')
else:
return bot.send(event, f'打卡失败!\nstatus_code:{status_code}\nresponse:{str(result)}')
def subscribe(event, method, _):
if method == '订阅':
group_id = event.sender.group.id
if group_id in NAOREN:
return bot.send(event, '下面有请松原花音,当然也有可能她死了')
def command_handler(event, msg):
if msg[0] == '/':
msg_list = msg[1:].split(' ', 1)
method = msg_list[0]
payload = msg_list[1] if len(msg_list) == 2 else None
funcs = [calc, save_fuck_qq, fuck_handler,
fuck_list, help, daka, subscribe, pre_save_img]
for func in funcs:
result = func(event, method, payload)
if result:
return result
return bot.send(event, '指令错误!请发送/help查看可用指令')
def baidu(event, msg: str):
ask_msg = None
end_char = ['是啥', '是谁', '是什么', '啥意思']
start_char = ['啥是', '啥叫', '什么是', '啥事', '为什么']
for char in end_char:
if msg.endswith(char):
ask_msg = msg[:-len(char)]
break
for char in start_char:
if msg.startswith(char):
print('char', char)
ask_msg = msg[len(char):]
print('ask_msg', ask_msg)
break
if ask_msg:
param = {
'wd': ask_msg,
}
search_url = f'可能百度:https://www.baidu.com/s?{urlencode(param)}'
return bot.send(event, search_url)
def go_along(event, msg):
bzd = ['bzd', '不知道'], '那您真bgj'
yg = ['yg', '也管'], '你啥都管'
bgj = ['bgj', '不管经', '不管斤'], 'bgj'
wdl = ['wdl', '无敌了', 'tql', '太强了', '太nm强了', 'lb'], 'qs'
question_mark = ['?', '?'], '?'
kaibai = ['算了', '就这样吧', '开摆', '躺平了', '我不想努力了', '野熊',
'野熊吧', '懒得改了', '可是懒', '懒呗', '懒', '摆烂'], '开摆!'
exclamation_mark = ['!', '!'], msg
go_along_list = [bzd, yg, bgj, wdl, question_mark, kaibai]
if random.random() < 1/4:
for go_alone in go_along_list:
if msg in go_alone[0]:
return bot.send(event, go_alone[1])
if msg[-1] in exclamation_mark[0]:
return bot.send(event, exclamation_mark[1])
def exchange_rate(event, msg):
if msg == '查汇率':
_, rate = get_rate_text()
return bot.send(event, rate)
async def bilibili(bot, event, msg: str):
if msg.startswith('BV') or msg.startswith('bv') or msg.startswith('av'):
url = f'https://www.bilibili.com/video/{msg}'
await bot.send(event, url)
await bot.send(event, get_url_brief(url))
def message_handler(event, msg):
# print('in method: message_handler')
if len(streamline(msg)) < 1:
return
methods = [hello, web_summary, command_handler,
baidu, go_along, exchange_rate, fuck_detect]
for method in methods:
result = method(event, msg)
if result:
return result
async def send_img(bot, event, msg):
if msg.startswith('发图'):
success, save_msg = save_net_img(msg[2:].strip())
if success:
await bot.send(event, Image(path='temp.jpg'))
else:
await bot.send(event, save_msg)
async def asynchronous_handler(event, msg):
# print('in method: asynchronous_handler')
if len(streamline(msg)) < 1:
return
methods = [setu, setu_cache, erciyuan, send_img, setu_send_all, bilibili]
# methods = [setu_cache, erciyuan, send_img, bilibili] # 不可以色色!
for method in methods:
# print('bot in qq', bot)
await method(bot, event, msg)
@bot.on(GroupMessage)
async def on_group_message(event: GroupMessage):
msg_chain_code = event.message_chain.as_mirai_code()
print(msg_chain_code)
msg = str(event.message_chain)
group_id = event.sender.group.id
try:
# print("FUCK_IMG['is_saving']", FUCK_IMG['is_saving'])
if FUCK_IMG['is_saving'] and group_id == FUCK_IMG['group_id']:
print('准备接收图片')
await save_img(event)
return
return message_handler(event, msg)
except Exception as e:
traceback.print_exc()
return bot.send(event, '内部错误:' + repr(e))
@bot.on(GroupMessage)
async def on_asynchronous_message(event: GroupMessage):
# print('on_asynchronous_message')
msg = str(event.message_chain)
try:
await asynchronous_handler(event, msg)
except Exception as e:
traceback.print_exc()
return bot.send(event, '内部错误:' + repr(e))
@bot.on(Startup)
async def startup(_):
await bot.send_group_message(TEST, '哈哈 俺启动辣!')
@bot.on(Startup)
def start_scheduler(_):
scheduler.start() # 启动定时器
@bot.on(Shutdown)
def stop_scheduler(_):
scheduler.shutdown(True) # 结束定时器
if __name__ == '__main__':
bot.run()