From 0a61e8833b79277546ab1263282e8babc1625c2b Mon Sep 17 00:00:00 2001 From: eisenwave Date: Sat, 12 Aug 2023 00:17:46 +0200 Subject: [PATCH 1/3] closes #27; add /bikeshed command component --- src/command.ts | 2 +- src/components/bikeshed.ts | 63 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/components/bikeshed.ts diff --git a/src/command.ts b/src/command.ts index a989622e..6a059d2f 100644 --- a/src/command.ts +++ b/src/command.ts @@ -268,7 +268,7 @@ export class TextBasedCommand extends Command { public member: Discord.GuildMember | Discord.APIInteractionGuildMember | null; public readonly user: Discord.User; - private response: Discord.Message | Discord.InteractionResponse | null = null; + public response: Discord.Message | Discord.InteractionResponse | null = null; public replied = false; private editing = false; diff --git a/src/components/bikeshed.ts b/src/components/bikeshed.ts new file mode 100644 index 00000000..cac6a107 --- /dev/null +++ b/src/components/bikeshed.ts @@ -0,0 +1,63 @@ +import * as Discord from "discord.js"; + +import { strict as assert } from "assert"; + +import { M } from "../utils.js"; +import { colors } from "../common.js"; +import { BotComponent } from "../bot-component.js"; +import { Wheatley } from "../wheatley.js"; +import { TextBasedCommand, TextBasedCommandBuilder } from "../command.js"; + +const U_ARROW_DOUBLE_UP = "\u23eb"; +const U_ARROW_DOUBLE_DOWN = "\u23ec"; +const U_ARROW_UP = "\u2B06\uFE0F"; +const U_ARROW_DOWN = "\u2B07\uFE0F"; +const U_RECORD_BUTTON = "\u23FA\uFE0F"; + +const OPTIONS = [ + { symbol: U_ARROW_DOUBLE_UP, text: "Strongly Favor" }, + { symbol: U_ARROW_UP, text: "Favor" }, + { symbol: U_RECORD_BUTTON, text: "Neutral" }, + { symbol: U_ARROW_DOWN, text: "Against" }, + { symbol: U_ARROW_DOUBLE_DOWN, text: "Strongly Against" }, +]; + +const DESCRIPTION = OPTIONS.map(({ symbol, text }) => `${symbol} - ${text}`).join("\n"); + +/** + * Adds a /bikeshed command for creating polls with responses ranging from + * strongly favor to strongly against. + */ +export default class Bikeshed extends BotComponent { + static override get is_freestanding() { + return true; + } + + constructor(wheatley: Wheatley) { + super(wheatley); + + this.add_command( + new TextBasedCommandBuilder("bikeshed") + .set_description("Create a poll from strongly favor to strongly against") + .add_string_option({ + title: "title", + description: "The title of the poll", + required: true, + }) + .set_handler(Bikeshed.bikeshed), + ); + } + + static async bikeshed(command: TextBasedCommand, arg: string) { + M.log("Creating bikeshed poll for question "); + await command.reply({ + embeds: [new Discord.EmbedBuilder().setColor(colors.color).setTitle(arg).setDescription(DESCRIPTION)], + }); + for (const option of OPTIONS) { + const message = await command.response!.fetch(); + // Await in a loop is intentional here. + // Reactions have to be applied in a consistent order. + await message.react(option.symbol); + } + } +} From 47f92187c89392a7efd1c982ef2e571bcfa5781b Mon Sep 17 00:00:00 2001 From: eisenwave Date: Sat, 12 Aug 2023 01:15:48 +0200 Subject: [PATCH 2/3] replace unicode escapes with emoji characters --- src/components/bikeshed.ts | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/components/bikeshed.ts b/src/components/bikeshed.ts index cac6a107..0b5857a4 100644 --- a/src/components/bikeshed.ts +++ b/src/components/bikeshed.ts @@ -8,18 +8,12 @@ import { BotComponent } from "../bot-component.js"; import { Wheatley } from "../wheatley.js"; import { TextBasedCommand, TextBasedCommandBuilder } from "../command.js"; -const U_ARROW_DOUBLE_UP = "\u23eb"; -const U_ARROW_DOUBLE_DOWN = "\u23ec"; -const U_ARROW_UP = "\u2B06\uFE0F"; -const U_ARROW_DOWN = "\u2B07\uFE0F"; -const U_RECORD_BUTTON = "\u23FA\uFE0F"; - const OPTIONS = [ - { symbol: U_ARROW_DOUBLE_UP, text: "Strongly Favor" }, - { symbol: U_ARROW_UP, text: "Favor" }, - { symbol: U_RECORD_BUTTON, text: "Neutral" }, - { symbol: U_ARROW_DOWN, text: "Against" }, - { symbol: U_ARROW_DOUBLE_DOWN, text: "Strongly Against" }, + { symbol: "⏫", text: "Strongly Favor" }, + { symbol: "⬆️", text: "Favor" }, + { symbol: "⏺️", text: "Neutral" }, + { symbol: "⬇️", text: "Against" }, + { symbol: "⏬", text: "Strongly Against" }, ]; const DESCRIPTION = OPTIONS.map(({ symbol, text }) => `${symbol} - ${text}`).join("\n"); From eb8cdeafc71b297205f70c80f73bb5b158a38d5a Mon Sep 17 00:00:00 2001 From: eisenwave Date: Sat, 12 Aug 2023 01:17:16 +0200 Subject: [PATCH 3/3] make bikeshed function non-static for consistency --- src/components/bikeshed.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/bikeshed.ts b/src/components/bikeshed.ts index 0b5857a4..4231f8c8 100644 --- a/src/components/bikeshed.ts +++ b/src/components/bikeshed.ts @@ -38,11 +38,11 @@ export default class Bikeshed extends BotComponent { description: "The title of the poll", required: true, }) - .set_handler(Bikeshed.bikeshed), + .set_handler(this.bikeshed.bind(this)), ); } - static async bikeshed(command: TextBasedCommand, arg: string) { + async bikeshed(command: TextBasedCommand, arg: string) { M.log("Creating bikeshed poll for question "); await command.reply({ embeds: [new Discord.EmbedBuilder().setColor(colors.color).setTitle(arg).setDescription(DESCRIPTION)],