Context Menu Actions v1, clean and mute support with full options
This commit is contained in:
parent
2281dbcfef
commit
ff774aa5f6
19 changed files with 657 additions and 152 deletions
28
backend/src/plugins/ContextMenus/utils/contextRouter.ts
Normal file
28
backend/src/plugins/ContextMenus/utils/contextRouter.ts
Normal file
|
@ -0,0 +1,28 @@
|
|||
import { ContextMenuInteraction } from "discord.js";
|
||||
import { GuildPluginData } from "../../../../../../Knub/dist";
|
||||
import { availableActions } from "../actions/availableActions";
|
||||
import { ContextMenuPluginType } from "../types";
|
||||
|
||||
export async function routeContextAction(
|
||||
pluginData: GuildPluginData<ContextMenuPluginType>,
|
||||
interaction: ContextMenuInteraction,
|
||||
) {
|
||||
const contextLink = await pluginData.state.contextMenuLinks.get(interaction.commandId);
|
||||
if (!contextLink) return;
|
||||
const contextActions = Object.entries(pluginData.config.get().context_actions);
|
||||
|
||||
const configLink = contextActions.find(x => x[0] === contextLink.action_name);
|
||||
if (!configLink) return;
|
||||
|
||||
for (const [actionName, actionConfig] of Object.entries(configLink[1].action)) {
|
||||
if (actionConfig == null) return;
|
||||
const action = availableActions[actionName];
|
||||
action.apply({
|
||||
actionName,
|
||||
pluginData,
|
||||
actionConfig,
|
||||
interaction,
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
37
backend/src/plugins/ContextMenus/utils/loadAllCommands.ts
Normal file
37
backend/src/plugins/ContextMenus/utils/loadAllCommands.ts
Normal file
|
@ -0,0 +1,37 @@
|
|||
import { ApplicationCommandData } from "discord.js";
|
||||
import { LogType } from "src/data/LogType";
|
||||
import { LogsPlugin } from "src/plugins/Logs/LogsPlugin";
|
||||
import { GuildPluginData } from "../../../../../../Knub/dist";
|
||||
import { ContextMenuPluginType, ContextMenuTypeNameToNumber } from "../types";
|
||||
|
||||
export async function loadAllCommands(pluginData: GuildPluginData<ContextMenuPluginType>) {
|
||||
const comms = await pluginData.client.application!.commands;
|
||||
const actions = pluginData.config.get().context_actions;
|
||||
const newCommands: ApplicationCommandData[] = [];
|
||||
const addedNames: string[] = [];
|
||||
|
||||
for (const [name, configAction] of Object.entries(actions)) {
|
||||
if (!configAction.enabled) continue;
|
||||
|
||||
const data: ApplicationCommandData = {
|
||||
type: ContextMenuTypeNameToNumber[configAction.type],
|
||||
name: configAction.label,
|
||||
};
|
||||
addedNames.push(name);
|
||||
newCommands.push(data);
|
||||
}
|
||||
|
||||
const setCommands = await comms.set(newCommands, pluginData.guild.id).catch(e => {
|
||||
pluginData.getPlugin(LogsPlugin).log(LogType.BOT_ALERT, `Unable to overwrite context menus: ${e}`);
|
||||
return undefined;
|
||||
});
|
||||
if (!setCommands) return;
|
||||
|
||||
const setCommandsArray = [...setCommands.values()];
|
||||
await pluginData.state.contextMenuLinks.deleteAll();
|
||||
|
||||
for (let i = 0; i < setCommandsArray.length; i++) {
|
||||
const command = setCommandsArray[i];
|
||||
pluginData.state.contextMenuLinks.create(command.id, addedNames[i]);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
import { Snowflake, TextChannel } from "discord.js";
|
||||
import { GuildPluginData } from "knub";
|
||||
import { ERRORS, RecoverablePluginError } from "../../../RecoverablePluginError";
|
||||
import { disableUserNotificationStrings, UserNotificationMethod } from "../../../utils";
|
||||
import { ContextMenuPluginType } from "../types";
|
||||
|
||||
export function resolveActionContactMethods(
|
||||
pluginData: GuildPluginData<ContextMenuPluginType>,
|
||||
actionConfig: {
|
||||
notify?: string | null;
|
||||
notifyChannel?: string | null;
|
||||
},
|
||||
): UserNotificationMethod[] {
|
||||
if (actionConfig.notify === "dm") {
|
||||
return [{ type: "dm" }];
|
||||
} else if (actionConfig.notify === "channel") {
|
||||
if (!actionConfig.notifyChannel) {
|
||||
throw new RecoverablePluginError(ERRORS.NO_USER_NOTIFICATION_CHANNEL);
|
||||
}
|
||||
|
||||
const channel = pluginData.guild.channels.cache.get(actionConfig.notifyChannel as Snowflake);
|
||||
if (!(channel instanceof TextChannel)) {
|
||||
throw new RecoverablePluginError(ERRORS.INVALID_USER_NOTIFICATION_CHANNEL);
|
||||
}
|
||||
|
||||
return [{ type: "channel", channel }];
|
||||
} else if (actionConfig.notify && disableUserNotificationStrings.includes(actionConfig.notify)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue