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

feat: Context menu mod menu command

This commit is contained in:
Obliie 2023-07-15 21:34:26 +01:00
parent 5a4e50b19d
commit 771ed76f64
No known key found for this signature in database
GPG key ID: 9189A18F0D5B547E
17 changed files with 818 additions and 204 deletions

View file

@ -1,4 +1,11 @@
import { ContextMenuCommandInteraction } from "discord.js";
import {
ActionRowBuilder,
ButtonInteraction,
ModalBuilder,
ModalSubmitInteraction,
TextInputBuilder,
TextInputStyle,
} from "discord.js";
import humanizeDuration from "humanize-duration";
import { GuildPluginData } from "knub";
import { canActOn } from "src/pluginUtils";
@ -8,14 +15,16 @@ import { convertDelayStringToMS } from "../../../utils";
import { CaseArgs } from "../../Cases/types";
import { LogsPlugin } from "../../Logs/LogsPlugin";
import { MutesPlugin } from "../../Mutes/MutesPlugin";
import { MODAL_TIMEOUT } from "../commands/ModMenuCmd";
import { ContextMenuPluginType } from "../types";
export async function muteAction(
async function muteAction(
pluginData: GuildPluginData<ContextMenuPluginType>,
duration: string | undefined,
interaction: ContextMenuCommandInteraction,
reason: string | undefined,
target: string,
interaction: ButtonInteraction,
) {
await interaction.deferReply({ ephemeral: true });
const executingMember = await pluginData.guild.members.fetch(interaction.user.id);
const userCfg = await pluginData.config.getMatchingConfig({
channelId: interaction.channelId,
@ -24,43 +33,76 @@ export async function muteAction(
const modactions = pluginData.getPlugin(ModActionsPlugin);
if (!userCfg.can_use || !(await modactions.hasMutePermission(executingMember, interaction.channelId))) {
await interaction.followUp({ content: "Cannot mute: insufficient permissions" });
await interaction.editReply({ content: "Cannot mute: insufficient permissions", embeds: [], components: [] });
return;
}
const durationMs = duration ? convertDelayStringToMS(duration)! : undefined;
const mutes = pluginData.getPlugin(MutesPlugin);
const userId = interaction.targetId;
const targetMember = await pluginData.guild.members.fetch(interaction.targetId);
const targetMember = await pluginData.guild.members.fetch(target);
if (!canActOn(pluginData, executingMember, targetMember)) {
await interaction.followUp({ ephemeral: true, content: "Cannot mute: insufficient permissions" });
await interaction.editReply({ content: "Cannot mute: insufficient permissions", embeds: [], components: [] });
return;
}
const caseArgs: Partial<CaseArgs> = {
modId: executingMember.id,
};
const mutes = pluginData.getPlugin(MutesPlugin);
const durationMs = duration ? convertDelayStringToMS(duration)! : undefined;
try {
const result = await mutes.muteUser(userId, durationMs, "Context Menu Action", { caseArgs });
const result = await mutes.muteUser(target, durationMs, reason, { caseArgs });
const messageResultText = result.notifyResult.text ? ` (${result.notifyResult.text})` : "";
const muteMessage = `Muted **${result.case.user_name}** ${
durationMs ? `for ${humanizeDuration(durationMs)}` : "indefinitely"
} (Case #${result.case.case_number}) (user notified via ${
result.notifyResult.method ?? "dm"
})\nPlease update the new case with the \`update\` command`;
} (Case #${result.case.case_number})${messageResultText}`;
await interaction.followUp({ ephemeral: true, content: muteMessage });
await interaction.editReply({ content: muteMessage, embeds: [], components: [] });
} catch (e) {
await interaction.followUp({ ephemeral: true, content: "Plugin error, please check your BOT_ALERTs" });
await interaction.editReply({ content: "Plugin error, please check your BOT_ALERTs", embeds: [], components: [] });
if (e instanceof RecoverablePluginError && e.code === ERRORS.NO_MUTE_ROLE_IN_CONFIG) {
pluginData.getPlugin(LogsPlugin).logBotAlert({
body: `Failed to mute <@!${userId}> in ContextMenu action \`mute\` because a mute role has not been specified in server config`,
body: `Failed to mute <@!${target}> in ContextMenu action \`mute\` because a mute role has not been specified in server config`,
});
} else {
throw e;
}
}
}
export async function launchMuteActionModal(
pluginData: GuildPluginData<ContextMenuPluginType>,
interaction: ButtonInteraction,
target: string,
) {
const modal = new ModalBuilder().setCustomId("mute").setTitle("Mute");
const durationIn = new TextInputBuilder()
.setCustomId("duration")
.setLabel("Duration (Optional)")
.setRequired(false)
.setStyle(TextInputStyle.Short);
const reasonIn = new TextInputBuilder()
.setCustomId("reason")
.setLabel("Reason (Optional)")
.setRequired(false)
.setStyle(TextInputStyle.Paragraph);
const durationRow = new ActionRowBuilder<TextInputBuilder>().addComponents(durationIn);
const reasonRow = new ActionRowBuilder<TextInputBuilder>().addComponents(reasonIn);
modal.addComponents(durationRow, reasonRow);
await interaction.showModal(modal);
const submitted: ModalSubmitInteraction = await interaction.awaitModalSubmit({ time: MODAL_TIMEOUT });
if (submitted) {
await submitted.deferUpdate();
const duration = submitted.fields.getTextInputValue("duration");
const reason = submitted.fields.getTextInputValue("reason");
await muteAction(pluginData, duration, reason, target, interaction);
}
}