3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-14 13:55:03 +00:00

Change ContextMenu to hardcoded default actions

Sadge

(More complex self-defined stuff coming at some point tho)
This commit is contained in:
Dark 2021-08-15 01:09:04 +02:00
parent 3ddfb3b65a
commit 7cf75f3255
No known key found for this signature in database
GPG key ID: 384C4B4F5B1E25A8
12 changed files with 183 additions and 272 deletions

View file

@ -1,64 +1,52 @@
import { TextChannel } from "discord.js";
import * as t from "io-ts";
import { canActOn } from "src/pluginUtils";
import { ContextMenuInteraction, TextChannel } from "discord.js";
import { GuildPluginData } from "knub";
import { LogType } from "../../../data/LogType";
import { UtilityPlugin } from "../../../plugins/Utility/UtilityPlugin";
import { ERRORS, RecoverablePluginError } from "../../../RecoverablePluginError";
import { tNullable } from "../../../utils";
import { LogsPlugin } from "../../Logs/LogsPlugin";
import { contextMenuAction } from "../helpers";
import { ContextMenuPluginType } from "../types";
export const CleanAction = contextMenuAction({
configType: t.type({
amount: tNullable(t.number),
targetUserOnly: tNullable(t.boolean),
"delete-pins": tNullable(t.boolean),
}),
export async function cleanAction(
pluginData: GuildPluginData<ContextMenuPluginType>,
amount: number,
interaction: ContextMenuInteraction,
) {
interaction.deferReply({ ephemeral: true });
const executingMember = await pluginData.guild.members.fetch(interaction.user.id);
const userCfg = await pluginData.config.getMatchingConfig({
channelId: interaction.channelId,
member: executingMember,
});
defaultConfig: {
amount: 10,
targetUserOnly: false,
"delete-pins": false,
},
// TODO: Add perm check for can_clean in util
if (!userCfg.can_use) {
await interaction.followUp({ content: "Cannot clean: insufficient permissions" });
return;
}
async apply({ pluginData, actionConfig, actionName, interaction }) {
interaction.deferReply({ ephemeral: true });
const targetMessage = interaction.channel
? await interaction.channel.messages.fetch(interaction.targetId)
: await (pluginData.guild.channels.resolve(interaction.channelId) as TextChannel).messages.fetch(
interaction.targetId,
);
const amount = actionConfig.amount ?? 10;
const targetUserOnly = actionConfig.targetUserOnly ?? false;
const deletePins = actionConfig["delete-pins"] ?? false;
const user = targetUserOnly ? targetMessage.author.id : undefined;
const targetMember = await pluginData.guild.members.fetch(targetMessage.author.id);
const executingMember = await pluginData.guild.members.fetch(interaction.user.id);
const utility = pluginData.getPlugin(UtilityPlugin);
if (targetUserOnly && !canActOn(pluginData, executingMember, targetMember)) {
interaction.followUp({ ephemeral: true, content: "Cannot clean users messages: insufficient permissions" });
return;
}
try {
interaction.followUp(`Cleaning... Amount: ${amount}, User Only: ${targetUserOnly}, Pins: ${deletePins}`);
utility.clean(
{ count: amount, user, channel: targetMessage.channel.id, "delete-pins": deletePins },
targetMessage,
const targetMessage = interaction.channel
? await interaction.channel.messages.fetch(interaction.targetId)
: await (pluginData.guild.channels.resolve(interaction.channelId) as TextChannel).messages.fetch(
interaction.targetId,
);
} catch (e) {
interaction.followUp({ ephemeral: true, content: "Plugin error, please check your BOT_ALERTs" });
if (e instanceof RecoverablePluginError && e.code === ERRORS.NO_MUTE_ROLE_IN_CONFIG) {
pluginData.getPlugin(LogsPlugin).log(LogType.BOT_ALERT, {
body: `Failed to clean in <#${interaction.channelId}> in ContextMenu action \`${actionName}\``,
});
} else {
throw e;
}
const targetUserOnly = false;
const deletePins = false;
const user = undefined;
try {
interaction.followUp(`Cleaning... Amount: ${amount}, User Only: ${targetUserOnly}, Pins: ${deletePins}`);
const utility = pluginData.getPlugin(UtilityPlugin);
utility.clean({ count: amount, user, channel: targetMessage.channel.id, "delete-pins": deletePins }, targetMessage);
} catch (e) {
interaction.followUp({ ephemeral: true, content: "Plugin error, please check your BOT_ALERTs" });
if (e instanceof RecoverablePluginError && e.code === ERRORS.NO_MUTE_ROLE_IN_CONFIG) {
pluginData.getPlugin(LogsPlugin).log(LogType.BOT_ALERT, {
body: `Failed to clean in <#${interaction.channelId}> in ContextMenu action \`clean\`:_ ${e}`,
});
} else {
throw e;
}
},
});
}
}