Skip to content

Commit

Permalink
Add support for user installation context in context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
Rian8337 committed Nov 24, 2024
1 parent 72f4cb3 commit a762f2f
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 60 deletions.
137 changes: 78 additions & 59 deletions src/interactions/commands/Bot Creators/deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import {
ApplicationCommandOptionType,
ApplicationCommandType,
ApplicationIntegrationType,
ChatInputApplicationCommandData,
InteractionContextType,
MessageApplicationCommandData,
PermissionResolvable,
UserApplicationCommandData,
} from "discord.js";
import { CommandCategory } from "@enums/core/CommandCategory";
import { SlashCommand } from "structures/core/SlashCommand";
Expand Down Expand Up @@ -36,72 +39,88 @@ export const run: SlashCommand["run"] = async (client, interaction) => {
ApplicationCommandType.ChatInput)
);

if (type === ApplicationCommandType.PrimaryEntryPoint) {
return;
}
switch (type) {
case ApplicationCommandType.PrimaryEntryPoint:
return;

if (type === ApplicationCommandType.ChatInput) {
const command = client.interactions.chatInput.get(commandName);
case ApplicationCommandType.ChatInput: {
const command = client.interactions.chatInput.get(commandName);

if (!command) {
return InteractionHelper.reply(interaction, {
content: MessageCreator.createReject(
localization.getTranslation("commandNotFound"),
),
});
}
if (!command) {
return InteractionHelper.reply(interaction, {
content: MessageCreator.createReject(
localization.getTranslation("commandNotFound"),
),
});
}

let memberPermissions: PermissionResolvable[] | null = null;

if (
command.config.permissions &&
command.config.integrationTypes?.includes(
ApplicationIntegrationType.GuildInstall,
)
) {
if (command.config.permissions.includes("BotOwner")) {
memberPermissions = ["Administrator"];
} else if (!command.config.permissions.includes("Special")) {
memberPermissions = <PermissionResolvable[]>(
command.config.permissions
);
let memberPermissions: PermissionResolvable[] | null = null;

if (
command.config.permissions &&
command.config.integrationTypes?.includes(
ApplicationIntegrationType.GuildInstall,
)
) {
if (command.config.permissions.includes("BotOwner")) {
memberPermissions = ["Administrator"];
} else if (!command.config.permissions.includes("Special")) {
memberPermissions = <PermissionResolvable[]>(
command.config.permissions
);
}
}
}

data = {
name: command.config.name,
description: command.config.description,
options: command.config.options,
defaultMemberPermissions: memberPermissions,
contexts: command.config.contexts ?? [
InteractionContextType.Guild,
InteractionContextType.BotDM,
InteractionContextType.PrivateChannel,
],
integrationTypes: command.config.integrationTypes ?? [
ApplicationIntegrationType.GuildInstall,
ApplicationIntegrationType.UserInstall,
],
};
} else {
const command = (
type === ApplicationCommandType.Message
? client.interactions.contextMenu.message
: client.interactions.contextMenu.user
).get(commandName);

if (!command) {
return InteractionHelper.reply(interaction, {
content: MessageCreator.createReject(
localization.getTranslation("commandNotFound"),
),
});
data = {
name: command.config.name,
description: command.config.description,
options: command.config.options,
defaultMemberPermissions: memberPermissions,
contexts: command.config.contexts ?? [
InteractionContextType.Guild,
InteractionContextType.BotDM,
InteractionContextType.PrivateChannel,
],
integrationTypes: command.config.integrationTypes ?? [
ApplicationIntegrationType.GuildInstall,
ApplicationIntegrationType.UserInstall,
],
} satisfies ChatInputApplicationCommandData;

break;
}

data = {
name: command.config.name,
type: type,
};
default: {
const command = (
type === ApplicationCommandType.Message
? client.interactions.contextMenu.message
: client.interactions.contextMenu.user
).get(commandName);

if (!command) {
return InteractionHelper.reply(interaction, {
content: MessageCreator.createReject(
localization.getTranslation("commandNotFound"),
),
});
}

data = {
name: command.config.name,
type: type,
contexts: command.config.contexts ?? [
InteractionContextType.Guild,
InteractionContextType.BotDM,
InteractionContextType.PrivateChannel,
],
integrationTypes: command.config.integrationTypes ?? [
ApplicationIntegrationType.GuildInstall,
ApplicationIntegrationType.UserInstall,
],
} satisfies
| UserApplicationCommandData
| MessageApplicationCommandData;
}
}

await (
Expand Down
16 changes: 15 additions & 1 deletion src/structures/core/ContextMenuCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Bot } from "@core/Bot";
import { ContextMenuCommandInteraction } from "discord.js";
import {
ApplicationIntegrationType,
ContextMenuCommandInteraction,
InteractionContextType,
} from "discord.js";

/**
* Represents a command that is executed through context menus.
Expand All @@ -25,6 +29,16 @@ export interface ContextMenuCommand {
*/
readonly name: string;

/**
* Interaction contexts where this command can be used, only for globally-scoped commands. Defaults to all interaction context types.
*/
readonly contexts?: InteractionContextType[];

/**
* Installation contexts where the command is available, only for globally-scoped commands. Defaults to all integration types.
*/
readonly integrationTypes?: ApplicationIntegrationType[];

/**
* The cooldown of the command, in seconds.
*/
Expand Down

0 comments on commit a762f2f

Please sign in to comment.