Skip to content

Commit

Permalink
Merge pull request #518 from gowon-bot/user-install
Browse files Browse the repository at this point in the history
Support user installs
  • Loading branch information
abbyfour authored Oct 17, 2024
2 parents 41276fd + 8f087c1 commit 5546a65
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 21 deletions.
5 changes: 4 additions & 1 deletion src/lib/command/CommandRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type CommandFactory = SimpleMap<{
export interface CommandListOptions {
includeSecret?: boolean;
includeArchived?: boolean;
includeDeveloper?: boolean;
includeOnlyDMCommands?: boolean;
}

Expand Down Expand Up @@ -101,14 +102,16 @@ export class CommandRegistry {
list({
includeSecret,
includeArchived,
includeDeveloper,
includeOnlyDMCommands,
}: CommandListOptions = {}): Command[] {
return this.pool.filter(
(c) =>
(c.parentName ? true : c.shouldBeIndexed) &&
(includeSecret || !c.secretCommand) &&
(includeArchived || !c.archived) &&
(includeOnlyDMCommands ? !c.guildRequired : true)
(includeOnlyDMCommands ? !c.guildRequired : true) &&
(includeDeveloper || !c.devCommand)
);
}

Expand Down
25 changes: 25 additions & 0 deletions src/lib/command/interactions/ConvertedSlashCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { RESTPostAPIApplicationCommandsJSONBody } from "discord-api-types/v9";
import { SlashCommandBuilder } from "../../context/arguments/argumentTypes/SlashCommandTypes";

export class ConvertedSlashCommand {
private isUserInstallable = false;

constructor(private builder: SlashCommandBuilder) {}

public toJSON(): RESTPostAPIApplicationCommandsJSONBody {
const json = this.builder.toJSON();

if (this.isUserInstallable) {
// Type is outdated
(json as any).integration_types = [0, 1];
(json as any).contexts = [0, 1, 2];
}

return json;
}

public setUserInstallable(toggle: boolean): this {
this.isUserInstallable = toggle;
return this;
}
}
8 changes: 4 additions & 4 deletions src/lib/command/interactions/InteractionRegister.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { SlashCommandBuilder } from "@discordjs/builders";
import { REST } from "@discordjs/rest";
import { Routes } from "discord-api-types/v9";
import config from "../../../../config.json";
import { Command } from "../Command";
import { ConvertedSlashCommand } from "./ConvertedSlashCommand";
import { SlashCommandConverter } from "./SlashCommandConverter";

export class InteractionRegister {
private discord!: REST;
private converter = new SlashCommandConverter();

init() {
public init() {
this.discord = new REST({ version: "9" }).setToken(config.discordToken);
}

constructor() {}

async register(commands: Command[]) {
const convertedCommands = [] as SlashCommandBuilder[];
const convertedCommands = [] as ConvertedSlashCommand[];

for (const command of commands) {
try {
Expand All @@ -29,7 +29,7 @@ export class InteractionRegister {
await this.registerWithDiscord(convertedCommands);
}

private async registerWithDiscord(commands: SlashCommandBuilder[]) {
private async registerWithDiscord(commands: ConvertedSlashCommand[]) {
try {
if (config.environment === "development") {
await this.clearApplicationCommands();
Expand Down
2 changes: 1 addition & 1 deletion src/lib/command/interactions/InteractionRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class InteractionRegistry {

private getCommands(): Command[] {
return this.commandRegistry
.list({ includeSecret: true })
.list({ includeSecret: true, includeDeveloper: false })
.filter((c) => !!c.slashCommand) as Command[];
}

Expand Down
32 changes: 22 additions & 10 deletions src/lib/command/interactions/SlashCommandConverter.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import { Command, Variation } from "../Command";
import {
SlashCommandBuilder,
SlashCommandSubcommandBuilder,
} from "@discordjs/builders";
import { PermissionFlagsBits } from "discord-api-types/v9";
import { ArgumentsMap } from "../../context/arguments/types";
import { ParentCommand } from "../ParentCommand";
import { ChildCommand } from "../ChildCommand";
import { Command, Variation } from "../Command";
import { ParentCommand } from "../ParentCommand";
import { ConvertedSlashCommand } from "./ConvertedSlashCommand";

export class SlashCommandConverter {
convert(command: Command, asVariation?: Variation): SlashCommandBuilder[] {
convert(command: Command, asVariation?: Variation): ConvertedSlashCommand[] {
if (command instanceof ParentCommand) {
return [this.convertParentCommand(command)];
}

let slashCommand = new SlashCommandBuilder()
.setDefaultPermission(!(command.devCommand || command.adminCommand))
.setDefaultMemberPermissions(
command.adminCommand ? PermissionFlagsBits.Administrator : undefined
)
.setDMPermission(!command.guildRequired)
.setName(
(
asVariation?.name ||
Expand All @@ -33,12 +38,19 @@ export class SlashCommandConverter {

const variations = this.getSeparateVariations(command);

const convertedSlashCommand = new ConvertedSlashCommand(
slashCommand
).setUserInstallable(!command.guildRequired);

// Break the variation recursion
if (variations.length && !asVariation) {
return [slashCommand, ...this.convertVariations(command, variations)];
return [
convertedSlashCommand,
...this.convertVariations(command, variations),
];
}

return [slashCommand];
return [convertedSlashCommand];
}

// The typing here is a disaster
Expand Down Expand Up @@ -91,7 +103,7 @@ export class SlashCommandConverter {
return newSlashCommand;
}

private convertParentCommand(command: ParentCommand): SlashCommandBuilder {
private convertParentCommand(command: ParentCommand): ConvertedSlashCommand {
let parentCommand = new SlashCommandBuilder()
.setDefaultPermission(!(command.devCommand || command.adminCommand))
.setName(command.friendlyName)
Expand All @@ -111,7 +123,7 @@ export class SlashCommandConverter {
}
}

return parentCommand;
return new ConvertedSlashCommand(parentCommand);
}

private getSeparateVariations(command: Command): Variation[] {
Expand All @@ -121,8 +133,8 @@ export class SlashCommandConverter {
private convertVariations(
command: Command,
variations: Variation[]
): SlashCommandBuilder[] {
const slashCommands: SlashCommandBuilder[] = [];
): ConvertedSlashCommand[] {
const slashCommands: ConvertedSlashCommand[] = [];

for (const variation of variations) {
slashCommands.push(...this.convert(command, variation));
Expand Down
8 changes: 3 additions & 5 deletions src/lib/ui/embeds/AquariumEmbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,11 @@ export class AquariumEmbed extends View {
const message = this.getAquariumMessage(fishies);

return this.baseEmbed.setDescription(`
_${message}_
_${message}_
${tank}
${tank}
There be **${displayNumber(
this.aquarium.size
)} total fishy** in your aquarium.
There be **${displayNumber(this.aquarium.size)} total fishy** in your aquarium.
${
this.aquarium.size === 0
? ``
Expand Down

0 comments on commit 5546a65

Please sign in to comment.