-
Notifications
You must be signed in to change notification settings - Fork 6
/
session.base.ts
396 lines (362 loc) · 11.2 KB
/
session.base.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import { KBotify } from '../..';
import { mentionById } from '../../utils/mention-by-id';
import { AppCommand, initFuncResult } from '../command/command.app';
import { BaseObject } from '../base/bot.object';
import { Channel, GuildChannel, PrivateChannel } from '../channel';
import { ButtonEventMessage, TextMessage } from '../message';
import { SendOptions } from '../msg.types';
import { ResultTypes } from '../types';
import { BaseUser } from '../user';
import { BaseData, FuncResult } from '../command/types';
import { MenuCommand } from '../command/command.menu';
import { Card, CardObject } from '../card';
import { kBotifyLogger } from '../logger';
import { Guild } from '../guild';
import { SessionSendFunc } from './session.type';
export class BaseSession extends BaseObject implements BaseData {
/**
* 命令字符串
*/
cmdString?: string | undefined;
command: AppCommand | MenuCommand;
args: string[];
msg: ButtonEventMessage | TextMessage;
channel: Channel;
content: string | undefined;
guild?: Guild;
other?: any;
/**
* 会话的用户ID。
* 如果是文字消息,则返回发送者ID,如果是按钮事件,则返回点击者ID。
*
* @type {string}
* @memberof BaseSession
*/
userId: string;
user: BaseUser;
constructor(
command: AppCommand | MenuCommand,
args: string[],
msg: ButtonEventMessage | TextMessage,
client?: KBotify
) {
super(client ?? msg.client);
this.command = command;
this.args = args;
this.msg = msg;
if (msg instanceof TextMessage) {
this.userId = msg.authorId;
this.user = new BaseUser(msg.author, this.client);
} else {
this.userId = msg.userId;
this.user = new BaseUser(msg.user, this.client);
}
if (msg.guildId) {
this.guild = new Guild(msg.guildId, this.client);
this.channel = new GuildChannel({ id: msg.channelId }, this.client);
} else {
this.channel = new PrivateChannel(
{ id: msg.channelId },
this.client
);
}
// console.debug(this.user);
}
get guildId(): string | undefined {
return this.guild?.id;
}
reply: SessionSendFunc = async (
content: string | (() => string) | string | (() => Promise<string>),
resultType = ResultTypes.SUCCESS
) => {
return this._send(content, resultType, {
reply: true,
mention: true,
temp: false,
});
};
replyTemp: SessionSendFunc = async (
content: string | (() => string) | string | (() => Promise<string>),
resultType = ResultTypes.SUCCESS
) => {
return this._send(content, resultType, {
reply: true,
mention: true,
temp: true,
});
};
replyCard = async (
content:
| string
| (() => string)
| (() => Promise<string>)
| CardObject[]
| Card,
options?: Omit<SendOptions, 'reply'>
) => {
return this._sendCard(content, { ...options, reply: true });
};
replyCardTemp = async (
content:
| string
| (() => string)
| (() => Promise<string>)
| CardObject[]
| Card,
options?: Omit<SendOptions, 'temp' | 'reply'>
) => {
return this._sendCard(content, { ...options, temp: true, reply: true });
};
/**
* Send message with quotation (no @ included)
*/
quote: SessionSendFunc = async (
content,
resultType = ResultTypes.SUCCESS
) => {
return this._send(content, resultType, {
reply: true,
mention: false,
temp: false,
});
};
/**
* Send temporary message with quotation (no @ included)
*/
quoteTemp: SessionSendFunc = async (
content,
resultType = ResultTypes.SUCCESS
) => {
return this._send(content, resultType, {
reply: true,
mention: false,
temp: true,
});
};
mention: SessionSendFunc = async (
content: string | (() => string) | string | (() => Promise<string>),
resultType = ResultTypes.SUCCESS
) => {
return this._send(content, resultType, {
reply: false,
mention: true,
temp: false,
});
};
mentionTemp: SessionSendFunc = async (
content: string | (() => string) | string | (() => Promise<string>),
resultType = ResultTypes.SUCCESS
) => {
return this._send(content, resultType, {
reply: false,
mention: true,
temp: true,
});
};
send: SessionSendFunc = async (
content: string | (() => string) | string | (() => Promise<string>),
resultType = ResultTypes.SUCCESS
) => {
return this._send(content, resultType, {
reply: false,
mention: false,
temp: false,
});
};
sendTemp: SessionSendFunc = async (
content: string | (() => string) | string | (() => Promise<string>),
resultType = ResultTypes.SUCCESS
) => {
return this._send(content, resultType, {
reply: false,
mention: false,
temp: true,
});
};
sendCard = async (
content:
| string
| (() => string)
| (() => Promise<string>)
| CardObject[]
| Card,
options?: SendOptions
) => {
return this._sendCard(content, options);
};
sendCardTemp = async (
content:
| string
| (() => string)
| (() => Promise<string>)
| CardObject[]
| Card,
options?: Omit<SendOptions, 'temp'>
) => {
return this._sendCard(content, { ...options, temp: true });
};
/**
*
* @param messageId
* @param content
* @param quote
*/
updateMessage = async (
messageId: string,
content: string | CardObject[],
quote?: string,
tempTargetId?: string
): Promise<FuncResult<boolean>> => {
if (Array.isArray(content)) {
content = JSON.stringify(content);
}
const result = await this.client.API.message.update(
messageId,
content,
quote,
tempTargetId
);
return initFuncResult(
result,
result ? ResultTypes.SUCCESS : ResultTypes.FAIL
);
};
/**
*
* @param messageId
* @param content
* @param quote
*/
updateMessageTemp = async (
messageId: string,
content: string | CardObject[],
quote?: string
) => {
return await this.updateMessage(
messageId,
content,
quote,
this.user.id
);
};
/**
* 设置文字回复触发事件
*
* @param condition 文字满足的要求,包含的文字或正则表达式
* @param timeout 单位:ms 回复触发有效时间,默认为1分钟
* @param callback 触发后的回调函数
* @returns
*/
setTextTrigger = (
condition: string | RegExp,
timeout: number | null = 6e4,
callback: (msg: TextMessage) => void
) => {
const func = (msg: any) => {
msg = msg as TextMessage;
if (msg.authorId != this.userId) {
return;
}
if (condition instanceof RegExp) {
if (!condition.test(msg.content)) {
return;
}
} else if (!msg.content.includes(condition)) {
return;
}
callback(msg);
this.client.message.off('text', func);
};
this.client.message.on('text', func);
if (timeout) {
setTimeout(() => {
this.client.message.off('text', func);
}, timeout);
}
return () => {
this.client.message.off('text', func);
};
};
/**
* 发送消息。
* ### 接收
* 如果接收的消息为文字消息,则可以使用引用和@。
* 如果接收的消息为按钮点击事件消息,则可以使用@。
* ### 发送
* 如果发送的消息为文字或kmarkdown,则可以使用引用和@。
* 如果发送的消息为卡片消息,则可以使用引用。
*
* @param content
* @param [resultType=ResultTypes.SUCCESS]
* @param [sendOptions]
* @memberof BaseSession
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
_send = async (
content: string | (() => string) | string | (() => Promise<string>),
resultType = ResultTypes.SUCCESS,
sendOptions?: SendOptions
): Promise<FuncResult> => {
if (typeof content !== 'string') {
content = await content();
}
// decide if msg should be sent in specific channel.
let replyChannelId = this.msg.channelId;
replyChannelId = sendOptions?.channel ?? replyChannelId;
// decide if need mention at the start.
const msgType = sendOptions?.msgType ?? 9;
let withMention = sendOptions?.mention ?? false;
if (!this.client) {
throw new Error('session send used before bot assigned.');
}
if (msgType == 10) {
if (withMention) {
kBotifyLogger.info('发送卡片消息时使用了mention!', this);
}
withMention = false;
content = content.replace(/(\r\n|\n|\r)/gm, '');
}
content = (withMention ? `${mentionById(this.userId)}` : '') + content;
if (this.msg instanceof ButtonEventMessage) {
if (sendOptions?.reply) {
kBotifyLogger.info('回复按钮点击事件时使用了引用!', this);
sendOptions.reply = undefined;
}
}
try {
const msgSent = await this.client.API.message.create(
msgType,
replyChannelId,
content,
sendOptions?.reply ? this.msg.msgId : undefined,
sendOptions?.temp ? this.userId : undefined
);
return initFuncResult(this, resultType, msgSent);
} catch (error) {
kBotifyLogger.error(error);
}
return initFuncResult(this, resultType);
};
private readonly _sendCard = async (
content:
| string
| (() => string)
| (() => Promise<string>)
| CardObject[]
| Card,
options: SendOptions = { reply: false, temp: false }
) => {
const str =
content instanceof Card
? JSON.stringify([content])
: Array.isArray(content)
? JSON.stringify(content)
: content;
return this._send(str, ResultTypes.SUCCESS, {
msgType: 10,
reply: options.reply,
mention: false,
temp: options.temp,
});
};
}