This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
executable file
·228 lines (217 loc) · 8.16 KB
/
index.ts
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
import getMemeImage, { getAvatar, getImages } from './http'
import type { IMahiroUse } from 'mahiro'
import ky from 'ky'
import { trimGroupMsg } from './util'
interface memeInfo {
"key": string,
"keywords": string[]
"patterns": string[],
"params": {
"min_images": number,
"max_images": number,
"min_texts": number,
"max_texts": number,
"default_texts": string[],
"args": any[]
}
}
interface pluginConfig {
/**
* api地址,默认为 http://127.0.0.1:2233
*/
apiHost?: string
/**
* 屏蔽的memes,填写meme的key
*/
blockMemes?: string[]
}
const memesFallbackUserNick = ['always_like', 'ask', 'follow', 'my_friend']
const memesMatchKeyStart = ['always_like', 'ascension', 'ask', 'bad_news', 'bronya_holdsign', 'chanshenzi', 'dianzhongdian', 'dont_go_near',
'dont_touch', 'douyin', 'fanatic', 'follow', 'find_chips', 'good_news', 'google', 'hold_grudge', 'imprison', 'keep_away',
'luoyonghao_say', 'luxun_say', 'meteor', 'murmur', 'nokia', 'play_game', 'pornhub', 'psyduck', 'raise_sign', 'repeat', 'run',
'scratchcard', 'scroll', 'together', 'universal', 'weisuoyuwei', 'worship', 'youtube']
export default function Plugin (config?: pluginConfig) {
const {
apiHost = 'http://127.0.0.1:2233',
blockMemes = []
} = config || {}
const use: IMahiroUse = async (mahiro) => {
const logger = mahiro.logger.withTag('Memes') as typeof mahiro.logger
logger.info(`加载插件 Mahiro Memes...`)
logger.info(`被屏蔽的Memes:${blockMemes.join(',')}`)
const memeArr = [] as memeInfo[]
let keywords = [] as string[]
try {
const memes = await ky(`${apiHost}/memes/keys`).json<string[]>()
memes.filter((i) => !blockMemes.includes(i)).map((item) => {
ky(`${apiHost}/memes/${item}/info`).json<memeInfo>().then((res) => {
memeArr.push(res)
keywords = keywords.concat(res.keywords)
}).catch((err) => {
logger.warn(item, '加载失败', err)
})
})
} catch (error) {
logger.error(`插件Mahiro Memes加载失败`, error)
return
}
mahiro.onGroupMessage('Memes', async (data) => {
if (data?.msg?.Content?.toLowerCase() === 'meme帮助') {
mahiro.sendGroupMessage({
groupId: data.groupId,
msg: {
Content: '「meme帮助」获取帮助\n「meme列表」获取可用memes\n「meme名称 帮助」获取具体meme的帮助'
}
})
return
}
if (data?.msg?.Content?.toLowerCase() === 'meme列表') {
ky.post(`${apiHost}/memes/render_list/`, {
timeout: 1000 * 180
}).blob().then(async (res) => {
const imageBuffer = Buffer.from(await res.arrayBuffer())
mahiro.sendGroupMessage({
groupId: data.groupId,
fastImage: imageBuffer.toString('base64')
})
}).catch((err) => {
logger.warn('获取meme失败', err)
})
return
}
if (data?.msg?.Content?.length > 0) {
let curKeyword = ''
const filters = memeArr.filter((i) => i.keywords.some((j) => {
const match = data.msg.Content.toLowerCase().includes(j)
if (match) {
curKeyword = j
if (memesMatchKeyStart.includes(i.key)) {
return data.msg.Content.toLowerCase().startsWith(j)
}
if (i.params.max_texts === 0) {
return trimGroupMsg(keywords, data, false) === j
}
}
return match
}))
if (filters.length == 0) {
return
}
const meme = filters[0]
let content = trimGroupMsg(keywords, data, true)
if (content === '帮助') {
ky.get(`${apiHost}/memes/${meme.key}/preview`, {
timeout: 1000 * 180
}).blob().then(async (res) => {
const imageBuffer = Buffer.from(await res.arrayBuffer())
let str = `关键词:${meme.keywords.join('、')}\n`
if (meme.params.min_texts > 0) {
if (meme.params.min_texts === 1) {
str += `至少需要1段文字\n`
} else {
str += `至少需要${meme.params.min_texts}段文字,以空格分隔\n`
}
}
if (meme.params.min_images > 0) {
str += `至少需要${meme.params.min_images}张图片或@${meme.params.min_images}个人`
}
mahiro.sendGroupMessage({
groupId: data.groupId,
fastImage: imageBuffer.toString('base64'),
msg: {
Content: str
}
})
}).catch((err) => {
logger.error(`获取meme [${meme.key}]失败`, err)
})
return
}
const formData = new FormData()
const args: any = {}
// 需要图片
if (meme.params.min_images > 0) {
// 图片来源为图片消息或头像
if (data?.msg?.Images?.length >= meme.params.min_images || data?.msg?.AtUinLists?.length >= meme.params.min_images) {
let imgDataArr = await getImages(mahiro, data)
if (imgDataArr.length < meme.params.min_images) {
return
}
imgDataArr = imgDataArr.slice(0, meme.params.max_images)
for (const item of imgDataArr) {
formData.append('images', item)
}
} else if (((data?.msg?.Images?.length === 1 || data?.msg?.AtUinLists?.length === 1) && meme.params.min_images === 2)) {
const imgData = await getAvatar(mahiro, data.userId)
formData.append('images', imgData)
const imgDataArr = await getImages(mahiro, data)
if (imgDataArr.length !== 1) {
return
}
formData.append('images', imgDataArr[0])
// 没有就拿头像
} else {
// 防误触
if (meme.params.min_images === 1) {
const imgData = await getAvatar(mahiro, data.userId)
formData.append('images', imgData)
} else {
return
}
}
}
// 需要文字
const defaultTextContainKeyword = meme.params.default_texts.some((i) => i !== curKeyword && i.includes(curKeyword))
const contentContainOtherKeywords = meme.keywords.some((i) => i !== curKeyword && data.msg.Content.includes(i))
if (defaultTextContainKeyword || contentContainOtherKeywords) {
content = trimGroupMsg(keywords, data, false)
if (contentContainOtherKeywords) {
content = content.replace(curKeyword, '').trim()
}
}
if (content === '' && meme.params.min_texts > 0) {
return
}
if (meme.params.min_texts === meme.params.max_texts) {
if (meme.params.min_texts === 1) {
formData.append('texts', content)
} else if (meme.params.min_texts > 1) {
const arr = content.split(' ', meme.params.max_texts)
arr.map((i) => {
formData.append('texts', i)
})
}
} else {
const arr = content.split(' ', meme.params.min_texts)
arr.map((i) => {
formData.append('texts', i)
})
}
if (formData.getAll('texts').length === 0 && memesFallbackUserNick.includes(meme.key)) {
if (data?.msg?.AtUinLists?.length > 0) {
formData.append('texts', data.msg.AtUinLists[0].Nick)
}
}
// 需要参数
if (meme.params.args.length > 0) {
const memeArgs = meme.params.args[0]
if (memeArgs.enum) {
args[memeArgs.name] = memeArgs.enum[Math.floor(Math.random() * memeArgs.enum.length)]
}
}
if (formData.has('texts') || formData.has('images')) {
getMemeImage(apiHost, `/memes/${meme.key}/`, formData, args).then((res) => {
mahiro.sendGroupMessage({
groupId: data.groupId,
fastImage: res
})
}).catch((err) => {
logger.error(`获取meme [${meme.key}]失败`, formData, args, err)
})
}
}
})
logger.success(`插件Mahiro Memes加载成功`)
}
return use
}