Skip to content

Commit 7bf51fe

Browse files
committed
Use db for extendable polls
1 parent e2f99d1 commit 7bf51fe

File tree

1 file changed

+5
-40
lines changed

1 file changed

+5
-40
lines changed

src/commands/extend.ts

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,49 +9,14 @@ import {
99
import type { MessageCommand } from "@/commands/command.js";
1010
import type { BotContext } from "@/context.js";
1111
import type { ProcessableMessage } from "@/service/command.js";
12+
import type { Poll } from "@/storage/db/model.js";
1213

1314
import * as pollEmbedService from "@/service/pollEmbed.js";
1415
import { parseLegacyMessageParts } from "@/service/command.js";
1516
import * as pollService from "@/service/poll.js";
1617
import { defer } from "@/utils/interactionUtils.js";
1718
import { truncateToLength } from "@/utils/stringUtils.js";
1819

19-
type ResolvedPoll = {
20-
message: Message;
21-
description: string;
22-
};
23-
24-
async function fetchPollsFromChannel(
25-
channel: GuildTextBasedChannel,
26-
context: BotContext,
27-
): Promise<ResolvedPoll[]> {
28-
const messagesFromBot = channel.messages.cache
29-
.values()
30-
.filter(m => m.author.id === context.client.user.id);
31-
const polls = messagesFromBot
32-
.filter(m => m.embeds.length === 1)
33-
.filter(m => m.embeds[0].color === 0x2ecc71) // green color => extendable
34-
.filter(
35-
m =>
36-
m.embeds[0].author?.name.startsWith("Umfrage") ||
37-
m.embeds[0].author?.name.startsWith("Strawpoll"),
38-
);
39-
40-
return Array.from(polls)
41-
.sort((a, b) => b.createdTimestamp - a.createdTimestamp)
42-
.map(m => {
43-
let description = m.embeds[0].description || "";
44-
// Remove leading and trailing double asterisks if present
45-
if (description.startsWith("**") && description.endsWith("**")) {
46-
description = description.slice(2, -2);
47-
}
48-
return {
49-
message: m,
50-
description,
51-
} as ResolvedPoll;
52-
});
53-
}
54-
5520
export default class ExtendCommand implements MessageCommand {
5621
name = "extend";
5722
description = `Nutzbar als Reply auf eine mit --extendable erstellte Umfrage, um eine/mehrere Antwortmöglichkeit/en hinzuzufügen. Die Anzahl der bestehenden und neuen Antwortmöglichkeiten darf ${pollEmbedService.OPTION_LIMIT} nicht übersteigen.\nUsage: $COMMAND_PREFIX$extend [Antwort 1] ; [...]`;
@@ -66,7 +31,7 @@ export default class ExtendCommand implements MessageCommand {
6631

6732
async #offerPollSelection(
6833
extendMessage: Message<true>,
69-
polls: readonly ResolvedPoll[],
34+
polls: readonly Poll[],
7035
): Promise<Message<true> | undefined> {
7136
const pollSelectOption = new StringSelectMenuBuilder()
7237
.setCustomId("extend-poll-select")
@@ -75,8 +40,8 @@ export default class ExtendCommand implements MessageCommand {
7540
polls
7641
.slice(0, 24) // Discord allows max. 25 options
7742
.map(poll => ({
78-
label: truncateToLength(poll.description, 100) || "Kein Titel",
79-
value: poll.message.id,
43+
label: truncateToLength(poll.question, 100) || "Kein Titel",
44+
value: poll.embedMessageId,
8045
})),
8146
);
8247

@@ -116,7 +81,7 @@ export default class ExtendCommand implements MessageCommand {
11681
: undefined;
11782

11883
if (!pollMessage) {
119-
const polls = await fetchPollsFromChannel(message.channel, context);
84+
const polls = await pollService.findExtendablePollsInChannel(message.channel);
12085
if (polls.length === 0) {
12186
return "Bruder ich konnte echt keine Umfrage finden, welche du erweitern könntest. Sieh zu, dass du die Reply-Funktion benutzt oder den richtigen Channel auswählst.";
12287
}

0 commit comments

Comments
 (0)