-
Notifications
You must be signed in to change notification settings - Fork 6
/
session.private.ts
103 lines (90 loc) · 3.07 KB
/
session.private.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
import { PrivateChannel } from '../channel';
import {
AppCommand,
FuncResult,
initFuncResult,
MenuCommand,
} from '../command';
import { KBotify } from '../kbotify';
import { kBotifyLogger } from '../logger';
import { ButtonEventMessage, TextMessage } from '../message';
import { SendOptions } from '../msg.types';
import { ResultTypes } from '../types';
import { BaseUser } from '../user';
import { BaseSession } from './session.base';
export class PrivateSession extends BaseSession {
guild: undefined;
constructor(
command: AppCommand | MenuCommand,
args: string[],
msg: ButtonEventMessage | TextMessage,
client?: KBotify
) {
super(command, args, msg, client);
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);
}
this.channel = new PrivateChannel({ id: this.user.id }, this.client);
if (msg.guildId) {
throw new Error(
'Trying to compose private session with guild message'
);
}
// console.debug(this.user);
}
static fromSession = async (
session: BaseSession
): Promise<PrivateSession> => {
if (session.guild?.id) {
throw new Error('Trying to construct PrivateSession with guild id');
}
return new PrivateSession(
session.command,
session.args,
session.msg,
session.client
);
};
_send = async (
content: string | (() => string) | string | (() => Promise<string>),
resultType = ResultTypes.SUCCESS,
sendOptions?: Omit<SendOptions, 'temp'>
): Promise<FuncResult> => {
if (typeof content !== 'string') {
content = await content();
}
// decide if msg should be sent in specific channel.
const replyChannelId = sendOptions?.channel ?? this.channel.id;
// decide if need mention at the start.
const msgType = sendOptions?.msgType ?? 9;
if (!this.client) {
throw new Error('session send used before bot assigned.');
}
if (msgType === 10) {
content = content.replace(/(\r\n|\n|\r)/gm, '');
}
if (this.msg instanceof ButtonEventMessage) {
if (sendOptions?.reply) {
kBotifyLogger.info('回复按钮点击事件时使用了引用!', this);
sendOptions.reply = undefined;
}
}
try {
const msgSent = await this.client.API.directMessage.create(
msgType,
replyChannelId,
undefined,
content,
sendOptions?.reply ? this.msg.msgId : undefined
);
return initFuncResult(this, resultType, msgSent);
} catch (error) {
kBotifyLogger.error(error);
}
return initFuncResult(this, resultType);
};
}