-
Notifications
You must be signed in to change notification settings - Fork 34
/
CommandInteractionMessagingTunnel.ts
101 lines (90 loc) 路 2.53 KB
/
CommandInteractionMessagingTunnel.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
import MessagingTunnel, { MessagingAnswer } from '@bot/messaging/MessagingTunnel';
import { CommandInteraction, GuildMember, Message, TextChannel } from 'discord.js';
/**
* Represents an interaction messaging channel
* to operate interactions from an application command.
*
* @author Utarwyn
* @since 2.2.0
*/
export default class CommandInteractionMessagingTunnel extends MessagingTunnel {
/**
* Interaction object retrieved from the Discord API
* @private
*/
private readonly interaction: CommandInteraction;
/**
* Last reply sent into the tunnnel
* @private
*/
private _reply?: Message;
/**
* Constructs a new messaging
* tunnel using Discord interactions.
*
* @param interaction interaction generic object
*/
constructor(interaction: CommandInteraction) {
super();
this.interaction = interaction;
}
/**
* @inheritdoc
*/
public get author(): GuildMember {
return this.interaction.member as GuildMember;
}
/**
* @inheritdoc
*/
public get channel(): TextChannel {
return this.interaction.channel as TextChannel;
}
/**
* @inheritdoc
*/
public get reply(): Message | undefined {
return this._reply;
}
/**
* @inheritdoc
*/
public async replyWith(answer: MessagingAnswer, direct?: boolean): Promise<Message> {
// Fetch current reply if deferred externally and not register in this tunnel
if (!this.reply && this.interaction.deferred) {
this._reply = (await this.interaction.fetchReply()) as Message;
}
// Edit the reply if already exists
if (this.reply) {
await this.editReply(answer);
return this.reply;
}
this._reply = (await this.interaction.reply({
...answer,
fetchReply: true,
ephemeral: direct
})) as Message;
return this._reply;
}
/**
* @inheritdoc
*/
public async editReply(answer: MessagingAnswer): Promise<void> {
if (this.reply) {
await this.interaction.editReply(answer);
}
}
/**
* @inheritdoc
*/
public async end(reason?: MessagingAnswer): Promise<void> {
if (this.reply) {
try {
await this.editReply(reason ?? { content: '.', components: [], embeds: [] });
await this.reply.reactions.removeAll();
} catch {
// ignore api error
}
}
}
}