-
Notifications
You must be signed in to change notification settings - Fork 34
/
TextMessagingTunnel.ts
91 lines (83 loc) 路 2.01 KB
/
TextMessagingTunnel.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
import MessagingTunnel, { MessagingAnswer } from '@bot/messaging/MessagingTunnel';
import { GuildMember, Message, TextChannel } from 'discord.js';
/**
* Represents a text messaging channel
* to operate interactions from a text command.
*
* @author Utarwyn
* @since 2.2.0
*/
export default class TextMessagingTunnel extends MessagingTunnel {
/**
* Origin message
* @private
*/
private readonly origin: Message;
/**
* Last reply sent into the tunnnel
* @private
*/
private _reply?: Message;
/**
* Creates the text messaging tunnel.
*
* @param origin origin message
*/
constructor(origin: Message) {
super();
this.origin = origin;
}
/**
* @inheritdoc
*/
public get author(): GuildMember {
return this.origin.member!;
}
/**
* @inheritdoc
*/
public get channel(): TextChannel {
return this.origin.channel as TextChannel;
}
/**
* @inheritdoc
*/
public get reply(): Message | undefined {
return this._reply;
}
/**
* @inheritdoc
*/
public async replyWith(answer: MessagingAnswer, direct?: boolean): Promise<Message> {
if (direct) {
this._reply = await this.origin.reply(answer);
} else {
this._reply = await this.origin.channel.send(answer);
}
return this._reply;
}
/**
* @inheritdoc
*/
public async editReply(answer: MessagingAnswer): Promise<void> {
if (this.reply) {
await this.reply.edit(answer);
}
}
/**
* @inheritdoc
*/
public async end(reason: MessagingAnswer): Promise<void> {
if (this.reply) {
if (this.reply.deletable) {
try {
await this.reply.delete();
} catch {
// ignore api error
}
}
await this.channel.send(reason);
this._reply = undefined;
}
}
}