Skip to content

Commit 8453427

Browse files
Aed-1ImUrXButterscotchV
authored
Added Ephemeral preview feature to reply commands (#26)
* add preview feature * Update bingus-bot/src/contexts/replyWithList.ts Co-authored-by: Uriel <imurx@proton.me> * Update bingus-bot/src/contexts/reply.ts Co-authored-by: Uriel <imurx@proton.me> * Update bingus-bot/src/contexts/reply.ts Co-authored-by: Uriel <imurx@proton.me> * Update bingus-bot/src/util.ts Co-authored-by: Uriel <imurx@proton.me> * Update bingus-bot/src/contexts/replyWithList.ts Co-authored-by: Uriel <imurx@proton.me> * Update bingus-bot/src/util.ts Co-authored-by: Uriel <imurx@proton.me> * Update bingus-bot/src/util.ts Co-authored-by: Uriel <imurx@proton.me> * Added requested changes * cleaned up button in replywithlist * added embed to util.ts * combined the functions in util.ts# * removed any type * made replyWithList send only the one selected * Code reformat & clean-up * Fix closing bracket position --------- Co-authored-by: Uriel <imurx@proton.me> Co-authored-by: Butterscotch! <bscotchvanilla@gmail.com>
1 parent 72b8325 commit 8453427

3 files changed

Lines changed: 95 additions & 53 deletions

File tree

bingus-bot/src/contexts/reply.ts

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import {
2+
ActionRowBuilder,
23
ApplicationCommandType,
4+
ButtonBuilder,
5+
ButtonStyle,
36
ContextMenuCommandBuilder,
4-
EmbedBuilder,
57
MessageFlags,
68
} from "discord.js";
79
import { ContextMenu } from "../index.js";
8-
import { fetchBingus } from "../util.js";
10+
import { EmbedList, fetchBingus, replyEmbed } from "../util.js";
911

1012
export const replyContext: ContextMenu = {
1113
builder: new ContextMenuCommandBuilder()
@@ -21,29 +23,39 @@ export const replyContext: ContextMenu = {
2123

2224
try {
2325
await interaction.deferReply({ flags: MessageFlags.Ephemeral });
24-
const data = await fetchBingus(query);
2526

26-
if (data.length === 0) {
27+
const data = (await fetchBingus(query))[0];
28+
if (!data) {
2729
await interaction.editReply("No results found.");
2830
return;
2931
}
3032

31-
interaction.targetMessage.reply({
33+
const show = new ButtonBuilder()
34+
.setCustomId('show')
35+
.setLabel("Show message")
36+
.setStyle(ButtonStyle.Primary);
37+
38+
const message = await interaction.editReply({
3239
embeds: [
33-
new EmbedBuilder()
34-
.setAuthor({
35-
name: `Triggered by ${interaction.user.displayName}`,
36-
iconURL: interaction.user.avatarURL() ?? undefined,
37-
})
38-
.setTitle(data[0].title)
39-
.setDescription(data[0].text)
40-
.setColor("#65459A")
41-
.setFooter({ text: `${data[0].relevance.toFixed()}% relevant` })
42-
.data,
40+
replyEmbed(interaction, data)
4341
],
42+
components: [new ActionRowBuilder<ButtonBuilder>().addComponents(show)],
4443
});
4544

46-
await interaction.editReply("Replied to the message!");
45+
await message.awaitMessageComponent({
46+
time: EmbedList.MAX_TIME
47+
})
48+
49+
interaction.editReply({
50+
content: "Replied to message!",
51+
embeds: [],
52+
components: []
53+
});
54+
interaction.targetMessage.reply({
55+
embeds: [
56+
replyEmbed(interaction, data)
57+
],
58+
});
4759
} catch (error) {
4860
console.error(error);
4961
interaction.editReply("An error occurred while fetching results.");

bingus-bot/src/contexts/replyWithList.ts

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import {
22
ApplicationCommandType,
3+
ButtonBuilder,
34
ContextMenuCommandBuilder,
4-
EmbedBuilder,
5+
ButtonStyle,
56
MessageFlags,
67
} from "discord.js";
78
import { ContextMenu } from "../index.js";
8-
import { EmbedList, fetchBingus } from "../util.js";
9+
import { EmbedList, fetchBingus, replyEmbed } from "../util.js";
910

1011
export const replyListContext: ContextMenu = {
1112
builder: new ContextMenuCommandBuilder()
@@ -28,37 +29,30 @@ export const replyListContext: ContextMenu = {
2829
}
2930

3031
const data = await fetchBingus(query);
31-
3232
if (data.length === 0) {
3333
await interaction.editReply("No results found.");
3434
return;
3535
}
3636

37-
const embedList = new EmbedList();
38-
embedList.push(
37+
const show = new ButtonBuilder()
38+
.setCustomId('show')
39+
.setLabel("Show message")
40+
.setStyle(ButtonStyle.Primary)
41+
42+
const embedListEph = new EmbedList(show);
43+
embedListEph.push(
3944
...data.slice(0, 5).map(
4045
(res) =>
41-
new EmbedBuilder()
42-
.setAuthor({
43-
name: `Triggered by ${interaction.user.displayName}`,
44-
iconURL: interaction.user.avatarURL() ?? undefined,
45-
})
46-
.setTitle(res.title)
47-
.setDescription(res.text)
48-
.setColor("#65459A")
49-
.setFooter({ text: `(${res.relevance.toFixed()}% relevant)` })
50-
.data,
46+
replyEmbed(interaction, res)
5147
),
5248
);
5349

54-
await embedList.sendChannel(
55-
interaction.targetMessage.channel,
56-
interaction.user.id,
57-
undefined,
58-
{ messageReference: interaction.targetMessage },
59-
);
50+
const selectedIndex = await embedListEph.sendChatInput(interaction)
51+
52+
interaction.targetMessage.reply({
53+
embeds: [replyEmbed(interaction, data[selectedIndex])],
54+
});
6055

61-
await interaction.editReply("Replied to the message!");
6256
} catch (error) {
6357
console.error(error);
6458
interaction.editReply("An error occurred while fetching results.");

bingus-bot/src/util.ts

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
ComponentType,
88
EmbedBuilder,
99
EmojiIdentifierResolvable,
10+
MessageContextMenuCommandInteraction,
1011
ReplyOptions,
1112
SendableChannels,
1213
} from "discord.js";
@@ -33,11 +34,32 @@ export interface BingusFaqResponse {
3334
text: string;
3435
}
3536

37+
export function replyEmbed(interaction: MessageContextMenuCommandInteraction, res: BingusFaqResponse) {
38+
39+
const embedBuilder = new EmbedBuilder()
40+
.setAuthor({
41+
name: `Triggered by ${interaction.user.displayName}`,
42+
iconURL: interaction.user.avatarURL() ?? undefined,
43+
})
44+
.setTitle(res.title)
45+
.setDescription(res.text)
46+
.setColor("#65459A")
47+
.setFooter({ text: `${res.relevance.toFixed()}% relevant` })
48+
.data
49+
50+
return embedBuilder;
51+
}
52+
3653
export class EmbedList {
37-
static MAX_TIME = 300_000;
54+
static readonly MAX_TIME = 300_000;
3855
embeds: APIEmbed[] = [];
56+
_eph?: ButtonBuilder;
3957
index = 0;
4058

59+
constructor(eph?: ButtonBuilder) {
60+
this._eph = eph;
61+
}
62+
4163
push(...embed: APIEmbed[]): number {
4264
return this.embeds.push(...embed);
4365
}
@@ -55,24 +77,23 @@ export class EmbedList {
5577
.setStyle(ButtonStyle.Secondary)
5678
.setDisabled(this.index === 0);
5779

58-
return new ActionRowBuilder<ButtonBuilder>().addComponents(prev, next);
80+
return new ActionRowBuilder<ButtonBuilder>().addComponents(prev, next, ...(this._eph ? [this._eph] : []));
5981
}
6082

6183
get(): EmbedBuilder {
6284
const embed = this.embeds[this.index];
6385
return new EmbedBuilder(embed).setFooter({
64-
text: `${this.index + 1}/${this.embeds.length} ${
65-
embed.footer?.text
66-
}`.trim(),
86+
text: `${this.index + 1}/${this.embeds.length} ${embed.footer?.text
87+
}`.trim(),
6788
});
6889
}
69-
7090
async sendChannel(
7191
channel: SendableChannels,
7292
who: string | null,
7393
content?: string,
7494
reply?: ReplyOptions,
7595
) {
96+
7697
const edit = await channel.send({
7798
content,
7899
embeds: [this.get()],
@@ -101,6 +122,7 @@ export class EmbedList {
101122
return;
102123
}
103124
this.index--;
125+
break;
104126
}
105127

106128
await i.update({
@@ -115,18 +137,24 @@ export class EmbedList {
115137
}
116138

117139
async sendChatInput(
118-
interaction: ChatInputCommandInteraction,
140+
interaction: ChatInputCommandInteraction | MessageContextMenuCommandInteraction,
119141
publicInteraction: boolean | undefined = true,
120-
) {
142+
): Promise<number> {
143+
144+
let resolvePromise: (value: number) => void;
145+
const indexPromise = new Promise<number>((resolve) => {
146+
resolvePromise = resolve;
147+
});
148+
121149
const reply = await (interaction.deferred
122150
? interaction.editReply({
123-
embeds: [this.get()],
124-
components: [this.getActionRow()],
125-
})
151+
embeds: [this.get()],
152+
components: [this.getActionRow()],
153+
})
126154
: interaction.reply({
127-
embeds: [this.get()],
128-
components: [this.getActionRow()],
129-
}));
155+
embeds: [this.get()],
156+
components: [this.getActionRow()],
157+
}));
130158

131159
const collector = reply.createMessageComponentCollector({
132160
componentType: ComponentType.Button,
@@ -151,6 +179,14 @@ export class EmbedList {
151179
return;
152180
}
153181
this.index--;
182+
break;
183+
case "show":
184+
resolvePromise(this.index);
185+
interaction.editReply({
186+
content: "Replied to message!",
187+
embeds: [],
188+
components: []
189+
});
154190
}
155191

156192
await i.update({
@@ -163,7 +199,7 @@ export class EmbedList {
163199
await interaction.editReply({ components: [] });
164200
});
165201

166-
return collector;
202+
return indexPromise;
167203
}
168204
}
169205

0 commit comments

Comments
 (0)