Skip to content

Commit

Permalink
Enable all commands except /locale for user installation in preview (#…
Browse files Browse the repository at this point in the history
…506)

Server-specific emojis do not work in the user-install context

#432
  • Loading branch information
kevinlul authored Sep 3, 2024
1 parent ffa2767 commit 679a19d
Show file tree
Hide file tree
Showing 17 changed files with 81 additions and 58 deletions.
1 change: 1 addition & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,4 @@ jobs:
run: |
docker context use default
docker run -e DISCORD_TOKEN -e BOT_NO_DIRECT_MESSAGE_SEARCH=1 ${{ fromJSON(needs.build.outputs.json).tags[0] }} --deploy-slash 381294999729340417
docker run -e DISCORD_TOKEN -e BOT_NO_DIRECT_MESSAGE_SEARCH=1 ${{ fromJSON(needs.build.outputs.json).tags[0] }} --deploy-slash user-install
4 changes: 2 additions & 2 deletions src/commands/art.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { SlashCommandBuilder } from "@discordjs/builders";
import { RESTPostAPIApplicationCommandsJSONBody } from "discord-api-types/v10";
import { ChatInputCommandInteraction } from "discord.js";
import { Got } from "got";
Expand All @@ -10,6 +9,7 @@ import { getCard, getCardSearchOptions, getRubylessCardName, masterDuelIllustrat
import {
LocaleProvider,
buildLocalisedCommand,
everywhereCommand,
getKonamiIdSubcommand,
getNameSubcommand,
getPasswordSubcommand
Expand All @@ -32,7 +32,7 @@ export class ArtCommand extends Command {

static override get meta(): RESTPostAPIApplicationCommandsJSONBody {
const builder = buildLocalisedCommand(
new SlashCommandBuilder(),
everywhereCommand(),
() => c("command-name").t`art`,
() => c("command-description").t`Display the art for a card!`
);
Expand Down
5 changes: 2 additions & 3 deletions src/commands/deck.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
SlashCommandAttachmentOption,
SlashCommandBooleanOption,
SlashCommandBuilder,
SlashCommandStringOption,
SlashCommandSubcommandBuilder
} from "@discordjs/builders";
Expand Down Expand Up @@ -30,7 +29,7 @@ import { TypedDeck, parseURL, toURL } from "ydke";
import { Command } from "../Command";
import { getRubylessCardName } from "../card";
import { CardSchema } from "../definitions";
import { COMMAND_LOCALIZATIONS, Locale, LocaleProvider } from "../locale";
import { COMMAND_LOCALIZATIONS, Locale, LocaleProvider, everywhereCommand } from "../locale";
import { Logger, getLogger } from "../logger";
import { Metrics } from "../metrics";
import { addNotice, serialiseInteraction, splitText } from "../utils";
Expand Down Expand Up @@ -83,7 +82,7 @@ export class DeckCommand extends Command {
}

static override get meta(): RESTPostAPIApplicationCommandsJSONBody {
const builder = new SlashCommandBuilder()
const builder = everywhereCommand()
.setName("deck")
.setDescription(
"Display a deck list from ydke:// or .ydk format, exported from a number of deck building programs."
Expand Down
11 changes: 7 additions & 4 deletions src/commands/help.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { SlashCommandBuilder, SlashCommandStringOption } from "@discordjs/builders";
import { SlashCommandStringOption } from "@discordjs/builders";
import { RESTPostAPIApplicationCommandsJSONBody } from "discord-api-types/v10";
import { ChatInputCommandInteraction } from "discord.js";
import { inject, injectable } from "tsyringe";
import { c, useLocale } from "ttag";
import { Command } from "../Command";
import { COMMAND_LOCALIZATIONS, LocaleProvider } from "../locale";
import { COMMAND_LOCALIZATIONS, everywhereCommand, LocaleProvider } from "../locale";
import { getLogger, Logger } from "../logger";
import { Metrics } from "../metrics";
import { replyLatency } from "../utils";
Expand All @@ -13,12 +13,15 @@ import { replyLatency } from "../utils";
export class HelpCommand extends Command {
#logger = getLogger("command:help");

constructor(metrics: Metrics, @inject("LocaleProvider") private locales: LocaleProvider) {
constructor(
metrics: Metrics,
@inject("LocaleProvider") private locales: LocaleProvider
) {
super(metrics);
}

static override get meta(): RESTPostAPIApplicationCommandsJSONBody {
const builder = new SlashCommandBuilder().setName("help").setDescription("Get help with a Slash Command.");
const builder = everywhereCommand().setName("help").setDescription("Get help with a Slash Command.");

// update this when documentation is added for new commands!
const documentedCommands = [
Expand Down
10 changes: 7 additions & 3 deletions src/commands/id.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { SlashCommandBuilder, SlashCommandStringOption } from "@discordjs/builders";
import { SlashCommandStringOption } from "@discordjs/builders";
import { RESTPostAPIApplicationCommandsJSONBody } from "discord-api-types/v10";
import { ChatInputCommandInteraction, EmbedBuilder } from "discord.js";
import { Got } from "got";
import { inject, injectable } from "tsyringe";
import { getCard, inferInputType } from "../card";
import { Command } from "../Command";
import { everywhereCommand } from "../locale";
import { getLogger, Logger } from "../logger";
import { Metrics } from "../metrics";
import { addNotice, searchQueryTypeStringOption } from "../utils";
Expand All @@ -13,12 +14,15 @@ import { addNotice, searchQueryTypeStringOption } from "../utils";
export class IdCommand extends Command {
#logger = getLogger("command:id");

constructor(metrics: Metrics, @inject("got") private got: Got) {
constructor(
metrics: Metrics,
@inject("got") private got: Got
) {
super(metrics);
}

static override get meta(): RESTPostAPIApplicationCommandsJSONBody {
return new SlashCommandBuilder()
return everywhereCommand()
.setName("id")
.setDescription("Identify a card by password, Konami ID, or name.")
.addStringOption(
Expand Down
16 changes: 12 additions & 4 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { REST } from "@discordjs/rest";
import { APIUser, Routes } from "discord-api-types/v10";
import { ApplicationIntegrationType } from "discord.js";
import { ArtCommand } from "./art";
import { DeckCommand } from "./deck";
import { HelpCommand } from "./help";
Expand Down Expand Up @@ -61,9 +62,16 @@ export {
// Register Slash Commands on CI
// Specify the guild snowflake to instantly deploy commands on the specified server.
// Otherwise, global commands can take up to an hour to roll out.
export async function registerSlashCommands(guild?: `${bigint}`): Promise<void> {
// Duplicate command metadata if they register any aliases
const commands = classes.map(command => command.meta);
export async function registerSlashCommands(guild?: `${bigint}` | "user-install"): Promise<void> {
const commands =
guild === "user-install"
? classes
.filter(command => command.meta.integration_types)
.map(command => ({
...command.meta,
integration_types: [ApplicationIntegrationType.UserInstall]
}))
: classes.map(command => command.meta);
console.log("Generated command metadata:");
console.log(JSON.stringify(commands, null, 4));

Expand All @@ -75,7 +83,7 @@ export async function registerSlashCommands(guild?: `${bigint}`): Promise<void>
console.log(`${botUser.username}#${botUser.discriminator}`);

const created = await api.put(
guild === undefined
guild === undefined || guild === "user-install"
? Routes.applicationCommands(botUser.id)
: Routes.applicationGuildCommands(botUser.id, guild),
{ body: commands }
Expand Down
33 changes: 16 additions & 17 deletions src/commands/link.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { RESTPostAPIApplicationCommandsJSONBody } from "discord-api-types/v10";
import { ApplicationCommandOptionType, ChatInputCommandInteraction } from "discord.js";
import { ChatInputCommandInteraction } from "discord.js";
import { inject, injectable } from "tsyringe";
import { Command } from "../Command";
import { everywhereCommand } from "../locale";
import { Logger, getLogger } from "../logger";
import { Metrics } from "../metrics";
import { replyLatency } from "../utils";
Expand Down Expand Up @@ -74,24 +75,22 @@ export class LinkCommand extends Command {
};

static override get meta(): RESTPostAPIApplicationCommandsJSONBody {
return {
name: "link",
description: "Display one of several links with useful information.",
options: [
{
type: ApplicationCommandOptionType.String.valueOf(),
name: "key",
description: "The name of the link you want to display.",
required: true,
choices: Object.keys(LinkCommand.links).map(k => {
return {
return everywhereCommand()
.setName("link")
.setDescription("Display one of several links with useful information.")
.addStringOption(option =>
option
.setName("key")
.setDescription("The name of the link you want to display.")
.setRequired(true)
.setChoices(
Object.keys(LinkCommand.links).map(k => ({
name: LinkCommand.links[k].name,
value: k
};
})
}
]
};
}))
)
)
.toJSON();
}

protected override get logger(): Logger {
Expand Down
5 changes: 2 additions & 3 deletions src/commands/metagame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import {
ChatInputCommandInteraction,
EmbedBuilder,
RESTPostAPIApplicationCommandsJSONBody,
SlashCommandBuilder,
SlashCommandStringOption,
SlashCommandSubcommandBuilder
} from "discord.js";
import { Got } from "got";
import { inject, injectable } from "tsyringe";
import { c } from "ttag";
import { Command } from "../Command";
import { buildLocalisedChoice, buildLocalisedCommand } from "../locale";
import { buildLocalisedChoice, buildLocalisedCommand, everywhereCommand } from "../locale";
import { Logger, getLogger } from "../logger";
import { Metrics } from "../metrics";
import { replyLatency } from "../utils";
Expand Down Expand Up @@ -201,7 +200,7 @@ export class MetagameCommand extends Command {

static override get meta(): RESTPostAPIApplicationCommandsJSONBody {
const builder = buildLocalisedCommand(
new SlashCommandBuilder(),
everywhereCommand(),
() => c("command-name").t`metagame`,
() => c("command-description").t`Show statistics on the current competitive state of play.`
);
Expand Down
10 changes: 6 additions & 4 deletions src/commands/ping.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { SlashCommandBuilder } from "@discordjs/builders";
import { RESTPostAPIApplicationCommandsJSONBody } from "discord-api-types/v10";
import { ChatInputCommandInteraction } from "discord.js";
import { inject, injectable } from "tsyringe";
import { c, t, useLocale } from "ttag";
import { Command } from "../Command";
import { buildLocalisedCommand, LocaleProvider } from "../locale";
import { buildLocalisedCommand, everywhereCommand, LocaleProvider } from "../locale";
import { getLogger, Logger } from "../logger";
import { Metrics } from "../metrics";
import { replyLatency } from "../utils";
Expand All @@ -13,13 +12,16 @@ import { replyLatency } from "../utils";
export class PingCommand extends Command {
#logger = getLogger("command:ping");

constructor(metrics: Metrics, @inject("LocaleProvider") private locales: LocaleProvider) {
constructor(
metrics: Metrics,
@inject("LocaleProvider") private locales: LocaleProvider
) {
super(metrics);
}

static override get meta(): RESTPostAPIApplicationCommandsJSONBody {
return buildLocalisedCommand(
new SlashCommandBuilder(),
everywhereCommand(),
() => c("command-name").t`ping`,
() => c("command-description").t`Test latency to the new bot instance.`
).toJSON();
Expand Down
5 changes: 3 additions & 2 deletions src/commands/price.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SlashCommandBuilder, SlashCommandStringOption } from "@discordjs/builders";
import { SlashCommandStringOption } from "@discordjs/builders";
import { Static } from "@sinclair/typebox";
import { RESTPostAPIApplicationCommandsJSONBody } from "discord-api-types/v10";
import { ChatInputCommandInteraction, EmbedBuilder } from "discord.js";
Expand All @@ -13,6 +13,7 @@ import {
LocaleProvider,
buildLocalisedChoice,
buildLocalisedCommand,
everywhereCommand,
getKonamiIdSubcommand,
getNameSubcommand,
getPasswordSubcommand
Expand Down Expand Up @@ -85,7 +86,7 @@ export class PriceCommand extends Command {

static override get meta(): RESTPostAPIApplicationCommandsJSONBody {
const builder = buildLocalisedCommand(
new SlashCommandBuilder(),
everywhereCommand(),
() => c("command-name").t`price`,
() => c("command-description").t`Display the price for a card!`
);
Expand Down
5 changes: 2 additions & 3 deletions src/commands/query.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { SlashCommandBuilder } from "@discordjs/builders";
import { Static } from "@sinclair/typebox";
import { RESTPostAPIApplicationCommandsJSONBody } from "discord-api-types/v10";
import { ChatInputCommandInteraction, SlashCommandStringOption } from "discord.js";
Expand All @@ -7,7 +6,7 @@ import { inject, injectable } from "tsyringe";
import { c } from "ttag";
import { Command } from "../Command";
import { CardSchema } from "../definitions";
import { LocaleProvider, buildLocalisedCommand } from "../locale";
import { LocaleProvider, buildLocalisedCommand, everywhereCommand } from "../locale";
import { Logger, getLogger } from "../logger";
import { Metrics } from "../metrics";

Expand All @@ -26,7 +25,7 @@ export class QueryCommand extends Command {

static override get meta(): RESTPostAPIApplicationCommandsJSONBody {
return buildLocalisedCommand(
new SlashCommandBuilder(),
everywhereCommand(),
() => c("command-name").t`query`,
() => c("command-description").t`Advanced search prototype`
)
Expand Down
5 changes: 2 additions & 3 deletions src/commands/random.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { SlashCommandBuilder } from "@discordjs/builders";
import { Static } from "@sinclair/typebox";
import { RESTPostAPIApplicationCommandsJSONBody } from "discord-api-types/v10";
import { ChatInputCommandInteraction } from "discord.js";
Expand All @@ -9,7 +8,7 @@ import { Command } from "../Command";
import { createCardEmbed } from "../card";
import { CardSchema } from "../definitions";
import { UpdatingLimitRegulationVector } from "../limit-regulation";
import { LocaleProvider, buildLocalisedCommand, getResultLangStringOption } from "../locale";
import { LocaleProvider, buildLocalisedCommand, everywhereCommand, getResultLangStringOption } from "../locale";
import { Logger, getLogger } from "../logger";
import { Metrics } from "../metrics";

Expand Down Expand Up @@ -40,7 +39,7 @@ export class RandomCommand extends Command {

static override get meta(): RESTPostAPIApplicationCommandsJSONBody {
return buildLocalisedCommand(
new SlashCommandBuilder(),
everywhereCommand(),
() => c("command-name").t`random`,
() => c("command-description").t`Get a random Yu-Gi-Oh! card.`
)
Expand Down
4 changes: 2 additions & 2 deletions src/commands/rush-duel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
AutocompleteInteraction,
ChatInputCommandInteraction,
RESTPostAPIApplicationCommandsJSONBody,
SlashCommandBuilder,
SlashCommandIntegerOption,
SlashCommandStringOption,
SlashCommandSubcommandBuilder
Expand All @@ -21,6 +20,7 @@ import {
Locale,
LocaleProvider,
buildLocalisedCommand,
everywhereCommand,
getInputLangStringOption,
getResultLangStringOption
} from "../locale";
Expand Down Expand Up @@ -54,7 +54,7 @@ export class RushDuelCommand extends AutocompletableCommand {

static override get meta(): RESTPostAPIApplicationCommandsJSONBody {
const builder = buildLocalisedCommand(
new SlashCommandBuilder(),
everywhereCommand(),
() => c("command-name").t`rush-duel`,
() => c("command-description").t`Find information on Rush Duel cards.`
);
Expand Down
4 changes: 2 additions & 2 deletions src/commands/search.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { SlashCommandBuilder } from "@discordjs/builders";
import { RESTPostAPIApplicationCommandsJSONBody } from "discord-api-types/v10";
import { ChatInputCommandInteraction } from "discord.js";
import { Got } from "got";
Expand All @@ -10,6 +9,7 @@ import { UpdatingLimitRegulationVector } from "../limit-regulation";
import {
LocaleProvider,
buildLocalisedCommand,
everywhereCommand,
getKonamiIdSubcommand,
getNameSubcommand,
getPasswordSubcommand,
Expand All @@ -34,7 +34,7 @@ export class SearchCommand extends Command {

static override get meta(): RESTPostAPIApplicationCommandsJSONBody {
const builder = buildLocalisedCommand(
new SlashCommandBuilder(),
everywhereCommand(),
() => c("command-name").t`search`,
() => c("command-description").t`Find all information on a card!`
);
Expand Down
6 changes: 3 additions & 3 deletions src/commands/ygoprodeck.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SlashCommandBuilder, SlashCommandStringOption } from "@discordjs/builders";
import { SlashCommandStringOption } from "@discordjs/builders";
import { RESTPostAPIApplicationCommandsJSONBody } from "discord-api-types/v10";
import { AutocompleteInteraction, ChatInputCommandInteraction, escapeMarkdown } from "discord.js";
import { Got } from "got";
Expand All @@ -7,7 +7,7 @@ import { inject, injectable } from "tsyringe";
import { c, t, useLocale } from "ttag";
import { ygoprodeckCard } from "../card";
import { AutocompletableCommand } from "../Command";
import { buildLocalisedCommand, LocaleProvider } from "../locale";
import { buildLocalisedCommand, everywhereCommand, LocaleProvider } from "../locale";
import { getLogger, Logger } from "../logger";
import { Metrics } from "../metrics";
import { replyLatency, serialiseInteraction } from "../utils";
Expand All @@ -30,7 +30,7 @@ export class YGOPRODECKCommand extends AutocompletableCommand {

static override get meta(): RESTPostAPIApplicationCommandsJSONBody {
const builder = buildLocalisedCommand(
new SlashCommandBuilder(),
everywhereCommand(),
() => c("command-name").t`ygoprodeck`,
() => c("command-description").t`Search the YGOPRODECK card database.`
);
Expand Down
Loading

0 comments on commit 679a19d

Please sign in to comment.