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

Context Menu Actions v1, clean and mute support with full options

This commit is contained in:
Dark 2021-08-13 05:01:08 +02:00
parent 2281dbcfef
commit ff774aa5f6
No known key found for this signature in database
GPG key ID: 384C4B4F5B1E25A8
19 changed files with 657 additions and 152 deletions

View file

@ -0,0 +1,19 @@
import * as t from "io-ts";
import { ContextActionBlueprint } from "../helpers";
import { CleanAction } from "./clean";
import { MuteAction } from "./mute";
export const availableActions: Record<string, ContextActionBlueprint<any>> = {
mute: MuteAction,
clean: CleanAction,
};
export const AvailableActions = t.type({
mute: MuteAction.configType,
clean: CleanAction.configType,
});
export const availableTypes: Record<string, string[]> = {
mute: ["USER"],
clean: ["MESSAGE"],
};

View file

@ -0,0 +1,64 @@
import { TextChannel } from "discord.js";
import * as t from "io-ts";
import { canActOn } from "src/pluginUtils";
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";
export const CleanAction = contextMenuAction({
configType: t.type({
amount: tNullable(t.number),
targetUserOnly: tNullable(t.boolean),
"delete-pins": tNullable(t.boolean),
}),
defaultConfig: {
amount: 10,
targetUserOnly: false,
"delete-pins": false,
},
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,
);
} 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;
}
}
},
});

View file

@ -0,0 +1,83 @@
import humanizeDuration from "humanize-duration";
import * as t from "io-ts";
import { canActOn } from "src/pluginUtils";
import { LogType } from "../../../data/LogType";
import { ERRORS, RecoverablePluginError } from "../../../RecoverablePluginError";
import { convertDelayStringToMS, tDelayString, tNullable } from "../../../utils";
import { CaseArgs } from "../../Cases/types";
import { LogsPlugin } from "../../Logs/LogsPlugin";
import { MutesPlugin } from "../../Mutes/MutesPlugin";
import { contextMenuAction } from "../helpers";
import { resolveActionContactMethods } from "../utils/resolveActionContactMethods";
export const MuteAction = contextMenuAction({
configType: t.type({
reason: tNullable(t.string),
duration: tNullable(tDelayString),
notify: tNullable(t.string),
notifyChannel: tNullable(t.string),
remove_roles_on_mute: tNullable(t.union([t.boolean, t.array(t.string)])),
restore_roles_on_mute: tNullable(t.union([t.boolean, t.array(t.string)])),
postInCaseLog: tNullable(t.boolean),
hide_case: tNullable(t.boolean),
}),
defaultConfig: {
notify: null, // Use defaults from ModActions
hide_case: false,
},
async apply({ pluginData, actionConfig, actionName, interaction }) {
const duration = actionConfig.duration ? convertDelayStringToMS(actionConfig.duration)! : undefined;
const reason = actionConfig.reason || "Context Menu Action";
const contactMethods = actionConfig.notify ? resolveActionContactMethods(pluginData, actionConfig) : undefined;
const rolesToRemove = actionConfig.remove_roles_on_mute;
const rolesToRestore = actionConfig.restore_roles_on_mute;
const caseArgs: Partial<CaseArgs> = {
modId: pluginData.client.user!.id,
automatic: true,
postInCaseLogOverride: actionConfig.postInCaseLog ?? undefined,
hide: Boolean(actionConfig.hide_case),
};
interaction.deferReply({ ephemeral: true });
const mutes = pluginData.getPlugin(MutesPlugin);
const userId = interaction.targetId;
const targetMember = await pluginData.guild.members.fetch(interaction.targetId);
const executingMember = await pluginData.guild.members.fetch(interaction.user.id);
if (!canActOn(pluginData, executingMember, targetMember)) {
interaction.followUp({ ephemeral: true, content: "Cannot mute: insufficient permissions" });
return;
}
try {
const result = await mutes.muteUser(
userId,
duration,
reason,
{ contactMethods, caseArgs, isAutomodAction: true },
rolesToRemove,
rolesToRestore,
);
const muteMessage = `Muted **${result.case.user_name}** ${
duration ? `for ${humanizeDuration(duration)}` : "indefinitely"
} (Case #${result.case.case_number}) (user notified via ${result.notifyResult.method ??
"dm"})\nPlease update the new case with the \`update\` command`;
interaction.followUp({ ephemeral: true, content: muteMessage });
} 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 mute <@!${userId}> in ContextMenu action \`${actionName}\` because a mute role has not been specified in server config`,
});
} else {
throw e;
}
}
},
});