mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-15 05:41:51 +00:00
Merge branch 'feat/context-menu-mod-menu' of github.com:Obliie/Zeppelin
This commit is contained in:
commit
91339bb01e
25 changed files with 1146 additions and 235 deletions
|
@ -83,6 +83,21 @@ export class GuildCases extends BaseGuildRepository {
|
|||
});
|
||||
}
|
||||
|
||||
async getRecentByUserId(userId: string, count: number, skip = 0): Promise<Case[]> {
|
||||
return this.cases.find({
|
||||
relations: this.getRelations(),
|
||||
where: {
|
||||
guild_id: this.guildId,
|
||||
user_id: userId,
|
||||
},
|
||||
skip,
|
||||
take: count,
|
||||
order: {
|
||||
case_number: "DESC",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async getTotalCasesByModId(modId: string): Promise<number> {
|
||||
return this.cases.count({
|
||||
where: {
|
||||
|
|
|
@ -7,6 +7,7 @@ import {
|
|||
Message,
|
||||
MessageCreateOptions,
|
||||
MessageMentionOptions,
|
||||
ModalSubmitInteraction,
|
||||
PermissionsBitField,
|
||||
TextBasedChannel,
|
||||
} from "discord.js";
|
||||
|
@ -104,6 +105,7 @@ export async function sendSuccessMessage(
|
|||
channel: TextBasedChannel,
|
||||
body: string,
|
||||
allowedMentions?: MessageMentionOptions,
|
||||
responseInteraction?: ModalSubmitInteraction,
|
||||
): Promise<Message | undefined> {
|
||||
const emoji = pluginData.fullConfig.success_emoji || undefined;
|
||||
const formattedBody = successMessage(body, emoji);
|
||||
|
@ -111,13 +113,19 @@ export async function sendSuccessMessage(
|
|||
? { content: formattedBody, allowedMentions }
|
||||
: { content: formattedBody };
|
||||
|
||||
return channel
|
||||
.send({ ...content }) // Force line break
|
||||
.catch((err) => {
|
||||
const channelInfo = "guild" in channel ? `${channel.id} (${channel.guild.id})` : channel.id;
|
||||
logger.warn(`Failed to send success message to ${channelInfo}): ${err.code} ${err.message}`);
|
||||
return undefined;
|
||||
});
|
||||
if (responseInteraction) {
|
||||
await responseInteraction
|
||||
.editReply({ content: formattedBody, embeds: [], components: [] })
|
||||
.catch((err) => logger.error(`Interaction reply failed: ${err}`));
|
||||
} else {
|
||||
return channel
|
||||
.send({ ...content }) // Force line break
|
||||
.catch((err) => {
|
||||
const channelInfo = "guild" in channel ? `${channel.id} (${channel.guild.id})` : channel.id;
|
||||
logger.warn(`Failed to send success message to ${channelInfo}): ${err.code} ${err.message}`);
|
||||
return undefined;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export async function sendErrorMessage(
|
||||
|
@ -125,6 +133,7 @@ export async function sendErrorMessage(
|
|||
channel: TextBasedChannel,
|
||||
body: string,
|
||||
allowedMentions?: MessageMentionOptions,
|
||||
responseInteraction?: ModalSubmitInteraction,
|
||||
): Promise<Message | undefined> {
|
||||
const emoji = pluginData.fullConfig.error_emoji || undefined;
|
||||
const formattedBody = errorMessage(body, emoji);
|
||||
|
@ -132,13 +141,19 @@ export async function sendErrorMessage(
|
|||
? { content: formattedBody, allowedMentions }
|
||||
: { content: formattedBody };
|
||||
|
||||
return channel
|
||||
.send({ ...content }) // Force line break
|
||||
.catch((err) => {
|
||||
const channelInfo = "guild" in channel ? `${channel.id} (${channel.guild.id})` : channel.id;
|
||||
logger.warn(`Failed to send error message to ${channelInfo}): ${err.code} ${err.message}`);
|
||||
return undefined;
|
||||
});
|
||||
if (responseInteraction) {
|
||||
await responseInteraction
|
||||
.editReply({ content: formattedBody, embeds: [], components: [] })
|
||||
.catch((err) => logger.error(`Interaction reply failed: ${err}`));
|
||||
} else {
|
||||
return channel
|
||||
.send({ ...content }) // Force line break
|
||||
.catch((err) => {
|
||||
const channelInfo = "guild" in channel ? `${channel.id} (${channel.guild.id})` : channel.id;
|
||||
logger.warn(`Failed to send error message to ${channelInfo}): ${err.code} ${err.message}`);
|
||||
return undefined;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function getBaseUrl(pluginData: AnyPluginData<any>) {
|
||||
|
|
|
@ -1,32 +1,34 @@
|
|||
import { PluginOptions } from "knub";
|
||||
import { GuildContextMenuLinks } from "../../data/GuildContextMenuLinks";
|
||||
import { GuildCases } from "../../data/GuildCases";
|
||||
import { makeIoTsConfigParser } from "../../pluginUtils";
|
||||
import { trimPluginDescription } from "../../utils";
|
||||
import { CasesPlugin } from "../Cases/CasesPlugin";
|
||||
import { LogsPlugin } from "../Logs/LogsPlugin";
|
||||
import { ModActionsPlugin } from "../ModActions/ModActionsPlugin";
|
||||
import { MutesPlugin } from "../Mutes/MutesPlugin";
|
||||
import { UtilityPlugin } from "../Utility/UtilityPlugin";
|
||||
import { zeppelinGuildPlugin } from "../ZeppelinPluginBlueprint";
|
||||
import { ContextClickedEvt } from "./events/ContextClickedEvt";
|
||||
import { BanCmd } from "./commands/BanUserCtxCmd";
|
||||
import { CleanCmd } from "./commands/CleanMessageCtxCmd";
|
||||
import { ModMenuCmd } from "./commands/ModMenuUserCtxCmd";
|
||||
import { MuteCmd } from "./commands/MuteUserCtxCmd";
|
||||
import { NoteCmd } from "./commands/NoteUserCtxCmd";
|
||||
import { WarnCmd } from "./commands/WarnUserCtxCmd";
|
||||
import { ConfigSchema, ContextMenuPluginType } from "./types";
|
||||
import { loadAllCommands } from "./utils/loadAllCommands";
|
||||
|
||||
const defaultOptions: PluginOptions<ContextMenuPluginType> = {
|
||||
config: {
|
||||
can_use: false,
|
||||
|
||||
user_muteindef: false,
|
||||
user_mute1d: false,
|
||||
user_mute1h: false,
|
||||
user_info: false,
|
||||
|
||||
message_clean10: false,
|
||||
message_clean25: false,
|
||||
message_clean50: false,
|
||||
can_open_mod_menu: false,
|
||||
},
|
||||
overrides: [
|
||||
{
|
||||
level: ">=50",
|
||||
config: {
|
||||
can_use: true,
|
||||
|
||||
can_open_mod_menu: true,
|
||||
},
|
||||
},
|
||||
],
|
||||
|
@ -34,24 +36,25 @@ const defaultOptions: PluginOptions<ContextMenuPluginType> = {
|
|||
|
||||
export const ContextMenuPlugin = zeppelinGuildPlugin<ContextMenuPluginType>()({
|
||||
name: "context_menu",
|
||||
showInDocs: false,
|
||||
showInDocs: true,
|
||||
info: {
|
||||
prettyName: "Context Menus",
|
||||
description: trimPluginDescription(`
|
||||
This plugin provides command shortcuts via context menus
|
||||
`),
|
||||
configSchema: ConfigSchema,
|
||||
},
|
||||
|
||||
dependencies: () => [MutesPlugin, LogsPlugin, UtilityPlugin],
|
||||
dependencies: () => [CasesPlugin, MutesPlugin, ModActionsPlugin, LogsPlugin, UtilityPlugin],
|
||||
configParser: makeIoTsConfigParser(ConfigSchema),
|
||||
|
||||
defaultOptions,
|
||||
|
||||
// prettier-ignore
|
||||
events: [
|
||||
ContextClickedEvt,
|
||||
],
|
||||
contextMenuCommands: [ModMenuCmd, NoteCmd, WarnCmd, MuteCmd, BanCmd, CleanCmd],
|
||||
|
||||
beforeLoad(pluginData) {
|
||||
const { state, guild } = pluginData;
|
||||
|
||||
state.contextMenuLinks = new GuildContextMenuLinks(guild.id);
|
||||
},
|
||||
|
||||
afterLoad(pluginData) {
|
||||
loadAllCommands(pluginData);
|
||||
state.cases = GuildCases.getGuildInstance(guild.id);
|
||||
},
|
||||
});
|
||||
|
|
127
backend/src/plugins/ContextMenus/actions/ban.ts
Normal file
127
backend/src/plugins/ContextMenus/actions/ban.ts
Normal file
|
@ -0,0 +1,127 @@
|
|||
import {
|
||||
ActionRowBuilder,
|
||||
ButtonInteraction,
|
||||
ContextMenuCommandInteraction,
|
||||
ModalBuilder,
|
||||
ModalSubmitInteraction,
|
||||
TextInputBuilder,
|
||||
TextInputStyle,
|
||||
} from "discord.js";
|
||||
import humanizeDuration from "humanize-duration";
|
||||
import { GuildPluginData } from "knub";
|
||||
import { canActOn } from "src/pluginUtils";
|
||||
import { ModActionsPlugin } from "src/plugins/ModActions/ModActionsPlugin";
|
||||
import { logger } from "../../../logger";
|
||||
import { convertDelayStringToMS, renderUserUsername } from "../../../utils";
|
||||
import { CaseArgs } from "../../Cases/types";
|
||||
import { MODAL_TIMEOUT } from "../commands/ModMenuUserCtxCmd";
|
||||
import { ContextMenuPluginType, ModMenuActionType } from "../types";
|
||||
import { updateAction } from "./update";
|
||||
|
||||
async function banAction(
|
||||
pluginData: GuildPluginData<ContextMenuPluginType>,
|
||||
duration: string | undefined,
|
||||
reason: string | undefined,
|
||||
evidence: string | undefined,
|
||||
target: string,
|
||||
interaction: ButtonInteraction | ContextMenuCommandInteraction,
|
||||
submitInteraction: ModalSubmitInteraction,
|
||||
) {
|
||||
const interactionToReply = interaction.isButton() ? interaction : submitInteraction;
|
||||
const executingMember = await pluginData.guild.members.fetch(interaction.user.id);
|
||||
const userCfg = await pluginData.config.getMatchingConfig({
|
||||
channelId: interaction.channelId,
|
||||
member: executingMember,
|
||||
});
|
||||
|
||||
const modactions = pluginData.getPlugin(ModActionsPlugin);
|
||||
if (!userCfg.can_use || !(await modactions.hasBanPermission(executingMember, interaction.channelId))) {
|
||||
await interactionToReply
|
||||
.editReply({ content: "Cannot ban: insufficient permissions", embeds: [], components: [] })
|
||||
.catch((err) => logger.error(`Ban interaction reply failed: ${err}`));
|
||||
return;
|
||||
}
|
||||
|
||||
const targetMember = await pluginData.guild.members.fetch(target);
|
||||
if (!canActOn(pluginData, executingMember, targetMember)) {
|
||||
await interactionToReply
|
||||
.editReply({ content: "Cannot ban: insufficient permissions", embeds: [], components: [] })
|
||||
.catch((err) => logger.error(`Ban interaction reply failed: ${err}`));
|
||||
return;
|
||||
}
|
||||
|
||||
const caseArgs: Partial<CaseArgs> = {
|
||||
modId: executingMember.id,
|
||||
};
|
||||
|
||||
const durationMs = duration ? convertDelayStringToMS(duration)! : undefined;
|
||||
const result = await modactions.banUserId(target, reason, { caseArgs }, durationMs);
|
||||
if (result.status === "failed") {
|
||||
await interactionToReply
|
||||
.editReply({ content: "Error: Failed to ban user", embeds: [], components: [] })
|
||||
.catch((err) => logger.error(`Ban interaction reply failed: ${err}`));
|
||||
return;
|
||||
}
|
||||
|
||||
const userName = renderUserUsername(targetMember.user);
|
||||
const messageResultText = result.notifyResult.text ? ` (${result.notifyResult.text})` : "";
|
||||
const banMessage = `Banned **${userName}** ${
|
||||
durationMs ? `for ${humanizeDuration(durationMs)}` : "indefinitely"
|
||||
} (Case #${result.case.case_number})${messageResultText}`;
|
||||
|
||||
if (evidence) {
|
||||
await updateAction(pluginData, executingMember, result.case, evidence);
|
||||
}
|
||||
|
||||
await interactionToReply
|
||||
.editReply({ content: banMessage, embeds: [], components: [] })
|
||||
.catch((err) => logger.error(`Ban interaction reply failed: ${err}`));
|
||||
}
|
||||
|
||||
export async function launchBanActionModal(
|
||||
pluginData: GuildPluginData<ContextMenuPluginType>,
|
||||
interaction: ButtonInteraction | ContextMenuCommandInteraction,
|
||||
target: string,
|
||||
) {
|
||||
const modalId = `${ModMenuActionType.BAN}:${interaction.id}`;
|
||||
const modal = new ModalBuilder().setCustomId(modalId).setTitle("Ban");
|
||||
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 evidenceIn = new TextInputBuilder()
|
||||
.setCustomId("evidence")
|
||||
.setLabel("Evidence (Optional)")
|
||||
.setRequired(false)
|
||||
.setStyle(TextInputStyle.Paragraph);
|
||||
const durationRow = new ActionRowBuilder<TextInputBuilder>().addComponents(durationIn);
|
||||
const reasonRow = new ActionRowBuilder<TextInputBuilder>().addComponents(reasonIn);
|
||||
const evidenceRow = new ActionRowBuilder<TextInputBuilder>().addComponents(evidenceIn);
|
||||
modal.addComponents(durationRow, reasonRow, evidenceRow);
|
||||
|
||||
await interaction.showModal(modal);
|
||||
await interaction
|
||||
.awaitModalSubmit({ time: MODAL_TIMEOUT, filter: (i) => i.customId == modalId })
|
||||
.then(async (submitted) => {
|
||||
if (interaction.isButton()) {
|
||||
await submitted.deferUpdate().catch((err) => logger.error(`Ban interaction defer failed: ${err}`));
|
||||
} else if (interaction.isContextMenuCommand()) {
|
||||
await submitted
|
||||
.deferReply({ ephemeral: true })
|
||||
.catch((err) => logger.error(`Ban interaction defer failed: ${err}`));
|
||||
}
|
||||
|
||||
const duration = submitted.fields.getTextInputValue("duration");
|
||||
const reason = submitted.fields.getTextInputValue("reason");
|
||||
const evidence = submitted.fields.getTextInputValue("evidence");
|
||||
|
||||
await banAction(pluginData, duration, reason, evidence, target, interaction, submitted);
|
||||
})
|
||||
.catch((err) => logger.error(`Ban modal interaction failed: ${err}`));
|
||||
}
|
|
@ -1,16 +1,26 @@
|
|||
import { ContextMenuCommandInteraction, TextChannel } from "discord.js";
|
||||
import {
|
||||
ActionRowBuilder,
|
||||
Message,
|
||||
MessageContextMenuCommandInteraction,
|
||||
ModalBuilder,
|
||||
ModalSubmitInteraction,
|
||||
TextInputBuilder,
|
||||
TextInputStyle,
|
||||
} from "discord.js";
|
||||
import { GuildPluginData } from "knub";
|
||||
import { ERRORS, RecoverablePluginError } from "../../../RecoverablePluginError";
|
||||
import { logger } from "../../../logger";
|
||||
import { UtilityPlugin } from "../../../plugins/Utility/UtilityPlugin";
|
||||
import { LogsPlugin } from "../../Logs/LogsPlugin";
|
||||
import { ContextMenuPluginType } from "../types";
|
||||
import { MODAL_TIMEOUT } from "../commands/ModMenuUserCtxCmd";
|
||||
import { ContextMenuPluginType, ModMenuActionType } from "../types";
|
||||
|
||||
export async function cleanAction(
|
||||
pluginData: GuildPluginData<ContextMenuPluginType>,
|
||||
amount: number,
|
||||
interaction: ContextMenuCommandInteraction,
|
||||
target: string,
|
||||
targetMessage: Message,
|
||||
targetChannel: string,
|
||||
interaction: ModalSubmitInteraction,
|
||||
) {
|
||||
await interaction.deferReply({ ephemeral: true });
|
||||
const executingMember = await pluginData.guild.members.fetch(interaction.user.id);
|
||||
const userCfg = await pluginData.config.getMatchingConfig({
|
||||
channelId: interaction.channelId,
|
||||
|
@ -18,33 +28,59 @@ export async function cleanAction(
|
|||
});
|
||||
const utility = pluginData.getPlugin(UtilityPlugin);
|
||||
|
||||
if (!userCfg.can_use || !(await utility.hasPermission(executingMember, interaction.channelId, "can_clean"))) {
|
||||
await interaction.followUp({ content: "Cannot clean: insufficient permissions" });
|
||||
if (!userCfg.can_use || !(await utility.hasPermission(executingMember, targetChannel, "can_clean"))) {
|
||||
await interaction
|
||||
.editReply({ content: "Cannot clean: insufficient permissions", embeds: [], components: [] })
|
||||
.catch((err) => logger.error(`Clean interaction reply failed: ${err}`));
|
||||
return;
|
||||
}
|
||||
|
||||
const targetMessage = interaction.channel
|
||||
? await interaction.channel.messages.fetch(interaction.targetId)
|
||||
: await (pluginData.guild.channels.resolve(interaction.channelId) as TextChannel).messages.fetch(
|
||||
interaction.targetId,
|
||||
);
|
||||
await interaction
|
||||
.editReply({
|
||||
content: `Cleaning ${amount} messages from ${target}...`,
|
||||
embeds: [],
|
||||
components: [],
|
||||
})
|
||||
.catch((err) => logger.error(`Clean interaction reply failed: ${err}`));
|
||||
|
||||
const targetUserOnly = false;
|
||||
const deletePins = false;
|
||||
const user = undefined;
|
||||
|
||||
try {
|
||||
await 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) {
|
||||
await 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).logBotAlert({
|
||||
body: `Failed to clean in <#${interaction.channelId}> in ContextMenu action \`clean\`:_ ${e}`,
|
||||
});
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
await utility.clean({ count: amount, channel: targetChannel, "response-interaction": interaction }, targetMessage);
|
||||
}
|
||||
|
||||
export async function launchCleanActionModal(
|
||||
pluginData: GuildPluginData<ContextMenuPluginType>,
|
||||
interaction: MessageContextMenuCommandInteraction,
|
||||
target: string,
|
||||
) {
|
||||
const modalId = `${ModMenuActionType.CLEAN}:${interaction.id}`;
|
||||
const modal = new ModalBuilder().setCustomId(modalId).setTitle("Clean");
|
||||
const amountIn = new TextInputBuilder().setCustomId("amount").setLabel("Amount").setStyle(TextInputStyle.Short);
|
||||
const amountRow = new ActionRowBuilder<TextInputBuilder>().addComponents(amountIn);
|
||||
modal.addComponents(amountRow);
|
||||
|
||||
await interaction.showModal(modal);
|
||||
await interaction
|
||||
.awaitModalSubmit({ time: MODAL_TIMEOUT, filter: (i) => i.customId == modalId })
|
||||
.then(async (submitted) => {
|
||||
await submitted
|
||||
.deferReply({ ephemeral: true })
|
||||
.catch((err) => logger.error(`Clean interaction defer failed: ${err}`));
|
||||
|
||||
const amount = submitted.fields.getTextInputValue("amount");
|
||||
if (isNaN(Number(amount))) {
|
||||
interaction
|
||||
.editReply({ content: `Error: Amount '${amount}' is invalid`, embeds: [], components: [] })
|
||||
.catch((err) => logger.error(`Clean interaction reply failed: ${err}`));
|
||||
return;
|
||||
}
|
||||
|
||||
await cleanAction(
|
||||
pluginData,
|
||||
Number(amount),
|
||||
target,
|
||||
interaction.targetMessage,
|
||||
interaction.channelId,
|
||||
submitted,
|
||||
);
|
||||
})
|
||||
.catch((err) => logger.error(`Clean modal interaction failed: ${err}`));
|
||||
}
|
||||
|
|
|
@ -1,21 +1,36 @@
|
|||
import { ContextMenuCommandInteraction } from "discord.js";
|
||||
import {
|
||||
ActionRowBuilder,
|
||||
ButtonInteraction,
|
||||
ContextMenuCommandInteraction,
|
||||
ModalBuilder,
|
||||
ModalSubmitInteraction,
|
||||
TextInputBuilder,
|
||||
TextInputStyle,
|
||||
} from "discord.js";
|
||||
import humanizeDuration from "humanize-duration";
|
||||
import { GuildPluginData } from "knub";
|
||||
import { canActOn } from "src/pluginUtils";
|
||||
import { ModActionsPlugin } from "src/plugins/ModActions/ModActionsPlugin";
|
||||
import { ERRORS, RecoverablePluginError } from "../../../RecoverablePluginError";
|
||||
import { logger } from "../../../logger";
|
||||
import { convertDelayStringToMS } from "../../../utils";
|
||||
import { CaseArgs } from "../../Cases/types";
|
||||
import { LogsPlugin } from "../../Logs/LogsPlugin";
|
||||
import { MutesPlugin } from "../../Mutes/MutesPlugin";
|
||||
import { ContextMenuPluginType } from "../types";
|
||||
import { MODAL_TIMEOUT } from "../commands/ModMenuUserCtxCmd";
|
||||
import { ContextMenuPluginType, ModMenuActionType } from "../types";
|
||||
import { updateAction } from "./update";
|
||||
|
||||
export async function muteAction(
|
||||
async function muteAction(
|
||||
pluginData: GuildPluginData<ContextMenuPluginType>,
|
||||
duration: string | undefined,
|
||||
interaction: ContextMenuCommandInteraction,
|
||||
reason: string | undefined,
|
||||
evidence: string | undefined,
|
||||
target: string,
|
||||
interaction: ButtonInteraction | ContextMenuCommandInteraction,
|
||||
submitInteraction: ModalSubmitInteraction,
|
||||
) {
|
||||
await interaction.deferReply({ ephemeral: true });
|
||||
const interactionToReply = interaction.isButton() ? interaction : submitInteraction;
|
||||
const executingMember = await pluginData.guild.members.fetch(interaction.user.id);
|
||||
const userCfg = await pluginData.config.getMatchingConfig({
|
||||
channelId: interaction.channelId,
|
||||
|
@ -24,43 +39,112 @@ 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 interactionToReply
|
||||
.editReply({
|
||||
content: "Cannot mute: insufficient permissions",
|
||||
embeds: [],
|
||||
components: [],
|
||||
})
|
||||
.catch((err) => logger.error(`Mute interaction reply failed: ${err}`));
|
||||
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 interactionToReply
|
||||
.editReply({
|
||||
content: "Cannot mute: insufficient permissions",
|
||||
embeds: [],
|
||||
components: [],
|
||||
})
|
||||
.catch((err) => logger.error(`Mute interaction reply failed: ${err}`));
|
||||
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 });
|
||||
if (evidence) {
|
||||
await updateAction(pluginData, executingMember, result.case, evidence);
|
||||
}
|
||||
|
||||
await interactionToReply
|
||||
.editReply({ content: muteMessage, embeds: [], components: [] })
|
||||
.catch((err) => logger.error(`Mute interaction reply failed: ${err}`));
|
||||
} catch (e) {
|
||||
await interaction.followUp({ ephemeral: true, content: "Plugin error, please check your BOT_ALERTs" });
|
||||
await interactionToReply
|
||||
.editReply({
|
||||
content: "Plugin error, please check your BOT_ALERTs",
|
||||
embeds: [],
|
||||
components: [],
|
||||
})
|
||||
.catch((err) => logger.error(`Mute interaction reply failed: ${err}`));
|
||||
|
||||
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 | ContextMenuCommandInteraction,
|
||||
target: string,
|
||||
) {
|
||||
const modalId = `${ModMenuActionType.MUTE}:${interaction.id}`;
|
||||
const modal = new ModalBuilder().setCustomId(modalId).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 evidenceIn = new TextInputBuilder()
|
||||
.setCustomId("evidence")
|
||||
.setLabel("Evidence (Optional)")
|
||||
.setRequired(false)
|
||||
.setStyle(TextInputStyle.Paragraph);
|
||||
const durationRow = new ActionRowBuilder<TextInputBuilder>().addComponents(durationIn);
|
||||
const reasonRow = new ActionRowBuilder<TextInputBuilder>().addComponents(reasonIn);
|
||||
const evidenceRow = new ActionRowBuilder<TextInputBuilder>().addComponents(evidenceIn);
|
||||
modal.addComponents(durationRow, reasonRow, evidenceRow);
|
||||
|
||||
await interaction.showModal(modal);
|
||||
await interaction
|
||||
.awaitModalSubmit({ time: MODAL_TIMEOUT, filter: (i) => i.customId == modalId })
|
||||
.then(async (submitted) => {
|
||||
if (interaction.isButton()) {
|
||||
await submitted.deferUpdate().catch((err) => logger.error(`Mute interaction defer failed: ${err}`));
|
||||
} else if (interaction.isContextMenuCommand()) {
|
||||
await submitted
|
||||
.deferReply({ ephemeral: true })
|
||||
.catch((err) => logger.error(`Mute interaction defer failed: ${err}`));
|
||||
}
|
||||
|
||||
const duration = submitted.fields.getTextInputValue("duration");
|
||||
const reason = submitted.fields.getTextInputValue("reason");
|
||||
const evidence = submitted.fields.getTextInputValue("evidence");
|
||||
|
||||
await muteAction(pluginData, duration, reason, evidence, target, interaction, submitted);
|
||||
})
|
||||
.catch((err) => logger.error(`Mute modal interaction failed: ${err}`));
|
||||
}
|
||||
|
|
112
backend/src/plugins/ContextMenus/actions/note.ts
Normal file
112
backend/src/plugins/ContextMenus/actions/note.ts
Normal file
|
@ -0,0 +1,112 @@
|
|||
import {
|
||||
ActionRowBuilder,
|
||||
ButtonInteraction,
|
||||
ContextMenuCommandInteraction,
|
||||
ModalBuilder,
|
||||
ModalSubmitInteraction,
|
||||
TextInputBuilder,
|
||||
TextInputStyle,
|
||||
} from "discord.js";
|
||||
import { GuildPluginData } from "knub";
|
||||
import { canActOn } from "src/pluginUtils";
|
||||
import { ModActionsPlugin } from "src/plugins/ModActions/ModActionsPlugin";
|
||||
import { CaseTypes } from "../../../data/CaseTypes";
|
||||
import { logger } from "../../../logger";
|
||||
import { CasesPlugin } from "../../../plugins/Cases/CasesPlugin";
|
||||
import { renderUserUsername } from "../../../utils";
|
||||
import { LogsPlugin } from "../../Logs/LogsPlugin";
|
||||
import { MODAL_TIMEOUT } from "../commands/ModMenuUserCtxCmd";
|
||||
import { ContextMenuPluginType, ModMenuActionType } from "../types";
|
||||
|
||||
async function noteAction(
|
||||
pluginData: GuildPluginData<ContextMenuPluginType>,
|
||||
reason: string,
|
||||
target: string,
|
||||
interaction: ButtonInteraction | ContextMenuCommandInteraction,
|
||||
submitInteraction: ModalSubmitInteraction,
|
||||
) {
|
||||
const interactionToReply = interaction.isButton() ? interaction : submitInteraction;
|
||||
const executingMember = await pluginData.guild.members.fetch(interaction.user.id);
|
||||
const userCfg = await pluginData.config.getMatchingConfig({
|
||||
channelId: interaction.channelId,
|
||||
member: executingMember,
|
||||
});
|
||||
|
||||
const modactions = pluginData.getPlugin(ModActionsPlugin);
|
||||
if (!userCfg.can_use || !(await modactions.hasNotePermission(executingMember, interaction.channelId))) {
|
||||
await interactionToReply
|
||||
.editReply({
|
||||
content: "Cannot note: insufficient permissions",
|
||||
embeds: [],
|
||||
components: [],
|
||||
})
|
||||
.catch((err) => logger.error(`Note interaction reply failed: ${err}`));
|
||||
return;
|
||||
}
|
||||
|
||||
const targetMember = await pluginData.guild.members.fetch(target);
|
||||
if (!canActOn(pluginData, executingMember, targetMember)) {
|
||||
await interactionToReply
|
||||
.editReply({
|
||||
content: "Cannot note: insufficient permissions",
|
||||
embeds: [],
|
||||
components: [],
|
||||
})
|
||||
.catch((err) => logger.error(`Note interaction reply failed: ${err}`));
|
||||
return;
|
||||
}
|
||||
|
||||
const casesPlugin = pluginData.getPlugin(CasesPlugin);
|
||||
const createdCase = await casesPlugin.createCase({
|
||||
userId: target,
|
||||
modId: executingMember.id,
|
||||
type: CaseTypes.Note,
|
||||
reason,
|
||||
});
|
||||
|
||||
pluginData.getPlugin(LogsPlugin).logMemberNote({
|
||||
mod: interaction.user,
|
||||
user: targetMember.user,
|
||||
caseNumber: createdCase.case_number,
|
||||
reason,
|
||||
});
|
||||
|
||||
const userName = renderUserUsername(targetMember.user);
|
||||
await interactionToReply
|
||||
.editReply({
|
||||
content: `Note added on **${userName}** (Case #${createdCase.case_number})`,
|
||||
embeds: [],
|
||||
components: [],
|
||||
})
|
||||
.catch((err) => logger.error(`Note interaction reply failed: ${err}`));
|
||||
}
|
||||
|
||||
export async function launchNoteActionModal(
|
||||
pluginData: GuildPluginData<ContextMenuPluginType>,
|
||||
interaction: ButtonInteraction | ContextMenuCommandInteraction,
|
||||
target: string,
|
||||
) {
|
||||
const modalId = `${ModMenuActionType.NOTE}:${interaction.id}`;
|
||||
const modal = new ModalBuilder().setCustomId(modalId).setTitle("Note");
|
||||
const reasonIn = new TextInputBuilder().setCustomId("reason").setLabel("Note").setStyle(TextInputStyle.Paragraph);
|
||||
const reasonRow = new ActionRowBuilder<TextInputBuilder>().addComponents(reasonIn);
|
||||
modal.addComponents(reasonRow);
|
||||
|
||||
await interaction.showModal(modal);
|
||||
await interaction
|
||||
.awaitModalSubmit({ time: MODAL_TIMEOUT, filter: (i) => i.customId == modalId })
|
||||
.then(async (submitted) => {
|
||||
if (interaction.isButton()) {
|
||||
await submitted.deferUpdate().catch((err) => logger.error(`Note interaction defer failed: ${err}`));
|
||||
} else if (interaction.isContextMenuCommand()) {
|
||||
await submitted
|
||||
.deferReply({ ephemeral: true })
|
||||
.catch((err) => logger.error(`Note interaction defer failed: ${err}`));
|
||||
}
|
||||
|
||||
const reason = submitted.fields.getTextInputValue("reason");
|
||||
|
||||
await noteAction(pluginData, reason, target, interaction, submitted);
|
||||
})
|
||||
.catch((err) => logger.error(`Note modal interaction failed: ${err}`));
|
||||
}
|
28
backend/src/plugins/ContextMenus/actions/update.ts
Normal file
28
backend/src/plugins/ContextMenus/actions/update.ts
Normal file
|
@ -0,0 +1,28 @@
|
|||
import { GuildMember } from "discord.js";
|
||||
import { GuildPluginData } from "knub";
|
||||
import { CaseTypes } from "../../../data/CaseTypes";
|
||||
import { Case } from "../../../data/entities/Case";
|
||||
import { CasesPlugin } from "../../../plugins/Cases/CasesPlugin";
|
||||
import { LogsPlugin } from "../../../plugins/Logs/LogsPlugin";
|
||||
import { ContextMenuPluginType } from "../types";
|
||||
|
||||
export async function updateAction(
|
||||
pluginData: GuildPluginData<ContextMenuPluginType>,
|
||||
executingMember: GuildMember,
|
||||
theCase: Case,
|
||||
value: string,
|
||||
) {
|
||||
const casesPlugin = pluginData.getPlugin(CasesPlugin);
|
||||
await casesPlugin.createCaseNote({
|
||||
caseId: theCase.case_number,
|
||||
modId: executingMember.id,
|
||||
body: value,
|
||||
});
|
||||
|
||||
pluginData.getPlugin(LogsPlugin).logCaseUpdate({
|
||||
mod: executingMember.user,
|
||||
caseNumber: theCase.case_number,
|
||||
caseType: CaseTypes[theCase.type],
|
||||
note: value,
|
||||
});
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
import { ContextMenuCommandInteraction } from "discord.js";
|
||||
import { GuildPluginData } from "knub";
|
||||
import { UtilityPlugin } from "../../../plugins/Utility/UtilityPlugin";
|
||||
import { ContextMenuPluginType } from "../types";
|
||||
|
||||
export async function userInfoAction(
|
||||
pluginData: GuildPluginData<ContextMenuPluginType>,
|
||||
interaction: ContextMenuCommandInteraction,
|
||||
) {
|
||||
await 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,
|
||||
});
|
||||
const utility = pluginData.getPlugin(UtilityPlugin);
|
||||
|
||||
if (userCfg.can_use && (await utility.hasPermission(executingMember, interaction.channelId, "can_userinfo"))) {
|
||||
const embed = await utility.userInfo(interaction.targetId, interaction.user.id);
|
||||
if (!embed) {
|
||||
await interaction.followUp({ content: "Cannot info: internal error" });
|
||||
return;
|
||||
}
|
||||
await interaction.followUp({ embeds: [embed] });
|
||||
} else {
|
||||
await interaction.followUp({ content: "Cannot info: insufficient permissions" });
|
||||
}
|
||||
}
|
119
backend/src/plugins/ContextMenus/actions/warn.ts
Normal file
119
backend/src/plugins/ContextMenus/actions/warn.ts
Normal file
|
@ -0,0 +1,119 @@
|
|||
import {
|
||||
ActionRowBuilder,
|
||||
ButtonInteraction,
|
||||
ContextMenuCommandInteraction,
|
||||
ModalBuilder,
|
||||
ModalSubmitInteraction,
|
||||
TextInputBuilder,
|
||||
TextInputStyle,
|
||||
} from "discord.js";
|
||||
import { GuildPluginData } from "knub";
|
||||
import { canActOn } from "src/pluginUtils";
|
||||
import { ModActionsPlugin } from "src/plugins/ModActions/ModActionsPlugin";
|
||||
import { logger } from "../../../logger";
|
||||
import { renderUserUsername } from "../../../utils";
|
||||
import { CaseArgs } from "../../Cases/types";
|
||||
import { MODAL_TIMEOUT } from "../commands/ModMenuUserCtxCmd";
|
||||
import { ContextMenuPluginType, ModMenuActionType } from "../types";
|
||||
import { updateAction } from "./update";
|
||||
|
||||
async function warnAction(
|
||||
pluginData: GuildPluginData<ContextMenuPluginType>,
|
||||
reason: string,
|
||||
evidence: string | undefined,
|
||||
target: string,
|
||||
interaction: ButtonInteraction | ContextMenuCommandInteraction,
|
||||
submitInteraction: ModalSubmitInteraction,
|
||||
) {
|
||||
const interactionToReply = interaction.isButton() ? interaction : submitInteraction;
|
||||
const executingMember = await pluginData.guild.members.fetch(interaction.user.id);
|
||||
const userCfg = await pluginData.config.getMatchingConfig({
|
||||
channelId: interaction.channelId,
|
||||
member: executingMember,
|
||||
});
|
||||
|
||||
const modactions = pluginData.getPlugin(ModActionsPlugin);
|
||||
if (!userCfg.can_use || !(await modactions.hasWarnPermission(executingMember, interaction.channelId))) {
|
||||
await interactionToReply
|
||||
.editReply({
|
||||
content: "Cannot warn: insufficient permissions",
|
||||
embeds: [],
|
||||
components: [],
|
||||
})
|
||||
.catch((err) => logger.error(`Warn interaction reply failed: ${err}`));
|
||||
return;
|
||||
}
|
||||
|
||||
const targetMember = await pluginData.guild.members.fetch(target);
|
||||
if (!canActOn(pluginData, executingMember, targetMember)) {
|
||||
await interactionToReply
|
||||
.editReply({
|
||||
content: "Cannot warn: insufficient permissions",
|
||||
embeds: [],
|
||||
components: [],
|
||||
})
|
||||
.catch((err) => logger.error(`Warn interaction reply failed: ${err}`));
|
||||
return;
|
||||
}
|
||||
|
||||
const caseArgs: Partial<CaseArgs> = {
|
||||
modId: executingMember.id,
|
||||
};
|
||||
|
||||
const result = await modactions.warnMember(targetMember, reason, { caseArgs });
|
||||
if (result.status === "failed") {
|
||||
await interactionToReply
|
||||
.editReply({ content: "Error: Failed to warn user", embeds: [], components: [] })
|
||||
.catch((err) => logger.error(`Warn interaction reply failed: ${err}`));
|
||||
return;
|
||||
}
|
||||
|
||||
const userName = renderUserUsername(targetMember.user);
|
||||
const messageResultText = result.notifyResult.text ? ` (${result.notifyResult.text})` : "";
|
||||
const muteMessage = `Warned **${userName}** (Case #${result.case.case_number})${messageResultText}`;
|
||||
|
||||
if (evidence) {
|
||||
await updateAction(pluginData, executingMember, result.case, evidence);
|
||||
}
|
||||
|
||||
await interactionToReply
|
||||
.editReply({ content: muteMessage, embeds: [], components: [] })
|
||||
.catch((err) => logger.error(`Warn interaction reply failed: ${err}`));
|
||||
}
|
||||
|
||||
export async function launchWarnActionModal(
|
||||
pluginData: GuildPluginData<ContextMenuPluginType>,
|
||||
interaction: ButtonInteraction | ContextMenuCommandInteraction,
|
||||
target: string,
|
||||
) {
|
||||
const modalId = `${ModMenuActionType.WARN}:${interaction.id}`;
|
||||
const modal = new ModalBuilder().setCustomId(modalId).setTitle("Warn");
|
||||
const reasonIn = new TextInputBuilder().setCustomId("reason").setLabel("Reason").setStyle(TextInputStyle.Paragraph);
|
||||
const evidenceIn = new TextInputBuilder()
|
||||
.setCustomId("evidence")
|
||||
.setLabel("Evidence (Optional)")
|
||||
.setRequired(false)
|
||||
.setStyle(TextInputStyle.Paragraph);
|
||||
const reasonRow = new ActionRowBuilder<TextInputBuilder>().addComponents(reasonIn);
|
||||
const evidenceRow = new ActionRowBuilder<TextInputBuilder>().addComponents(evidenceIn);
|
||||
modal.addComponents(reasonRow, evidenceRow);
|
||||
|
||||
await interaction.showModal(modal);
|
||||
await interaction
|
||||
.awaitModalSubmit({ time: MODAL_TIMEOUT, filter: (i) => i.customId == modalId })
|
||||
.then(async (submitted) => {
|
||||
if (interaction.isButton()) {
|
||||
await submitted.deferUpdate().catch((err) => logger.error(`Warn interaction defer failed: ${err}`));
|
||||
} else if (interaction.isContextMenuCommand()) {
|
||||
await submitted
|
||||
.deferReply({ ephemeral: true })
|
||||
.catch((err) => logger.error(`Warn interaction defer failed: ${err}`));
|
||||
}
|
||||
|
||||
const reason = submitted.fields.getTextInputValue("reason");
|
||||
const evidence = submitted.fields.getTextInputValue("evidence");
|
||||
|
||||
await warnAction(pluginData, reason, evidence, target, interaction, submitted);
|
||||
})
|
||||
.catch((err) => logger.error(`Warn modal interaction failed: ${err}`));
|
||||
}
|
11
backend/src/plugins/ContextMenus/commands/BanUserCtxCmd.ts
Normal file
11
backend/src/plugins/ContextMenus/commands/BanUserCtxCmd.ts
Normal file
|
@ -0,0 +1,11 @@
|
|||
import { PermissionFlagsBits } from "discord.js";
|
||||
import { guildPluginUserContextMenuCommand } from "knub";
|
||||
import { launchBanActionModal } from "../actions/ban";
|
||||
|
||||
export const BanCmd = guildPluginUserContextMenuCommand({
|
||||
name: "Ban",
|
||||
defaultMemberPermissions: PermissionFlagsBits.BanMembers.toString(),
|
||||
async run({ pluginData, interaction }) {
|
||||
await launchBanActionModal(pluginData, interaction, interaction.targetId);
|
||||
},
|
||||
});
|
|
@ -0,0 +1,11 @@
|
|||
import { PermissionFlagsBits } from "discord.js";
|
||||
import { guildPluginMessageContextMenuCommand } from "knub";
|
||||
import { launchCleanActionModal } from "../actions/clean";
|
||||
|
||||
export const CleanCmd = guildPluginMessageContextMenuCommand({
|
||||
name: "Clean",
|
||||
defaultMemberPermissions: PermissionFlagsBits.ManageMessages.toString(),
|
||||
async run({ pluginData, interaction }) {
|
||||
await launchCleanActionModal(pluginData, interaction, interaction.targetId);
|
||||
},
|
||||
});
|
339
backend/src/plugins/ContextMenus/commands/ModMenuUserCtxCmd.ts
Normal file
339
backend/src/plugins/ContextMenus/commands/ModMenuUserCtxCmd.ts
Normal file
|
@ -0,0 +1,339 @@
|
|||
import {
|
||||
APIEmbed,
|
||||
ActionRowBuilder,
|
||||
ButtonBuilder,
|
||||
ButtonInteraction,
|
||||
ButtonStyle,
|
||||
ContextMenuCommandInteraction,
|
||||
GuildMember,
|
||||
PermissionFlagsBits,
|
||||
User,
|
||||
} from "discord.js";
|
||||
import { GuildPluginData, guildPluginUserContextMenuCommand } from "knub";
|
||||
import { Case } from "../../../data/entities/Case";
|
||||
import { logger } from "../../../logger";
|
||||
import { ModActionsPlugin } from "../../../plugins/ModActions/ModActionsPlugin";
|
||||
import { SECONDS, UnknownUser, emptyEmbedValue, renderUserUsername, resolveUser, trimLines } from "../../../utils";
|
||||
import { asyncMap } from "../../../utils/async";
|
||||
import { getChunkedEmbedFields } from "../../../utils/getChunkedEmbedFields";
|
||||
import { getGuildPrefix } from "../../../utils/getGuildPrefix";
|
||||
import { CasesPlugin } from "../../Cases/CasesPlugin";
|
||||
import { UtilityPlugin } from "../../Utility/UtilityPlugin";
|
||||
import { getUserInfoEmbed } from "../../Utility/functions/getUserInfoEmbed";
|
||||
import { launchBanActionModal } from "../actions/ban";
|
||||
import { launchMuteActionModal } from "../actions/mute";
|
||||
import { launchNoteActionModal } from "../actions/note";
|
||||
import { launchWarnActionModal } from "../actions/warn";
|
||||
import {
|
||||
ContextMenuPluginType,
|
||||
LoadModMenuPageFn,
|
||||
ModMenuActionOpts,
|
||||
ModMenuActionType,
|
||||
ModMenuNavigationType,
|
||||
} from "../types";
|
||||
|
||||
export const MODAL_TIMEOUT = 60 * SECONDS;
|
||||
const MOD_MENU_TIMEOUT = 60 * SECONDS;
|
||||
const CASES_PER_PAGE = 10;
|
||||
|
||||
export const ModMenuCmd = guildPluginUserContextMenuCommand({
|
||||
name: "Mod Menu",
|
||||
defaultMemberPermissions: PermissionFlagsBits.ViewAuditLog.toString(),
|
||||
async run({ pluginData, interaction }) {
|
||||
await interaction
|
||||
.deferReply({ ephemeral: true })
|
||||
.catch((err) => logger.error(`Mod menu interaction defer failed: ${err}`));
|
||||
|
||||
// Run permission checks for executing user.
|
||||
const executingMember = await pluginData.guild.members.fetch(interaction.user.id);
|
||||
const userCfg = await pluginData.config.getMatchingConfig({
|
||||
channelId: interaction.channelId,
|
||||
member: executingMember,
|
||||
});
|
||||
const utility = pluginData.getPlugin(UtilityPlugin);
|
||||
if (
|
||||
!userCfg.can_use ||
|
||||
(await !utility.hasPermission(executingMember, interaction.channelId, "can_open_mod_menu"))
|
||||
) {
|
||||
await interaction
|
||||
.followUp({ content: "Error: Insufficient Permissions" })
|
||||
.catch((err) => logger.error(`Mod menu interaction follow up failed: ${err}`));
|
||||
return;
|
||||
}
|
||||
|
||||
const user = await resolveUser(pluginData.client, interaction.targetId);
|
||||
if (!user.id) {
|
||||
await interaction
|
||||
.followUp("Error: User not found")
|
||||
.catch((err) => logger.error(`Mod menu interaction follow up failed: ${err}`));
|
||||
return;
|
||||
}
|
||||
|
||||
// Load cases and display mod menu
|
||||
const cases: Case[] = await pluginData.state.cases.with("notes").getByUserId(user.id);
|
||||
const userName =
|
||||
user instanceof UnknownUser && cases.length ? cases[cases.length - 1].user_name : renderUserUsername(user);
|
||||
const casesPlugin = pluginData.getPlugin(CasesPlugin);
|
||||
const totalCases = cases.length;
|
||||
const totalPages: number = Math.max(Math.ceil(totalCases / CASES_PER_PAGE), 1);
|
||||
const prefix = getGuildPrefix(pluginData);
|
||||
const infoEmbed = await getUserInfoEmbed(pluginData, user.id, false);
|
||||
displayModMenu(
|
||||
pluginData,
|
||||
interaction,
|
||||
totalPages,
|
||||
async (page) => {
|
||||
const pageCases: Case[] = await pluginData.state.cases
|
||||
.with("notes")
|
||||
.getRecentByUserId(user.id, CASES_PER_PAGE, (page - 1) * CASES_PER_PAGE);
|
||||
const lines = await asyncMap(pageCases, (c) => casesPlugin.getCaseSummary(c, true, interaction.targetId));
|
||||
|
||||
const firstCaseNum = (page - 1) * CASES_PER_PAGE + 1;
|
||||
const lastCaseNum = Math.min(page * CASES_PER_PAGE, totalCases);
|
||||
const title =
|
||||
lines.length == 0
|
||||
? `${userName}`
|
||||
: `Most recent cases for ${userName} | ${firstCaseNum}-${lastCaseNum} of ${totalCases}`;
|
||||
|
||||
const embed = {
|
||||
author: {
|
||||
name: title,
|
||||
icon_url: user instanceof User ? user.displayAvatarURL() : undefined,
|
||||
},
|
||||
fields: [
|
||||
...getChunkedEmbedFields(
|
||||
emptyEmbedValue,
|
||||
lines.length == 0 ? `No cases found for **${userName}**` : lines.join("\n"),
|
||||
),
|
||||
{
|
||||
name: emptyEmbedValue,
|
||||
value: trimLines(
|
||||
lines.length == 0 ? "" : `Use \`${prefix}case <num>\` to see more information about an individual case`,
|
||||
),
|
||||
},
|
||||
],
|
||||
footer: { text: `Page ${page}/${totalPages}` },
|
||||
} satisfies APIEmbed;
|
||||
|
||||
return embed;
|
||||
},
|
||||
infoEmbed,
|
||||
executingMember,
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
async function displayModMenu(
|
||||
pluginData: GuildPluginData<ContextMenuPluginType>,
|
||||
interaction: ContextMenuCommandInteraction,
|
||||
totalPages: number,
|
||||
loadPage: LoadModMenuPageFn,
|
||||
infoEmbed: APIEmbed | null,
|
||||
executingMember: GuildMember,
|
||||
) {
|
||||
if (interaction.deferred == false) {
|
||||
await interaction.deferReply().catch((err) => logger.error(`Mod menu interaction defer failed: ${err}`));
|
||||
}
|
||||
|
||||
const firstButton = new ButtonBuilder()
|
||||
.setStyle(ButtonStyle.Secondary)
|
||||
.setEmoji("⏪")
|
||||
.setCustomId(serializeCustomId({ action: ModMenuActionType.PAGE, target: ModMenuNavigationType.FIRST }))
|
||||
.setDisabled(true);
|
||||
const prevButton = new ButtonBuilder()
|
||||
.setStyle(ButtonStyle.Secondary)
|
||||
.setEmoji("⬅")
|
||||
.setCustomId(serializeCustomId({ action: ModMenuActionType.PAGE, target: ModMenuNavigationType.PREV }))
|
||||
.setDisabled(true);
|
||||
const infoButton = new ButtonBuilder()
|
||||
.setStyle(ButtonStyle.Primary)
|
||||
.setLabel("Info")
|
||||
.setEmoji("ℹ")
|
||||
.setCustomId(serializeCustomId({ action: ModMenuActionType.PAGE, target: ModMenuNavigationType.INFO }))
|
||||
.setDisabled(infoEmbed != null ? false : true);
|
||||
const nextButton = new ButtonBuilder()
|
||||
.setStyle(ButtonStyle.Secondary)
|
||||
.setEmoji("➡")
|
||||
.setCustomId(serializeCustomId({ action: ModMenuActionType.PAGE, target: ModMenuNavigationType.NEXT }))
|
||||
.setDisabled(totalPages > 1 ? false : true);
|
||||
const lastButton = new ButtonBuilder()
|
||||
.setStyle(ButtonStyle.Secondary)
|
||||
.setEmoji("⏩")
|
||||
.setCustomId(serializeCustomId({ action: ModMenuActionType.PAGE, target: ModMenuNavigationType.LAST }))
|
||||
.setDisabled(totalPages > 1 ? false : true);
|
||||
const navigationButtons = [firstButton, prevButton, infoButton, nextButton, lastButton] satisfies ButtonBuilder[];
|
||||
|
||||
const modactions = pluginData.getPlugin(ModActionsPlugin);
|
||||
const moderationButtons = [
|
||||
new ButtonBuilder()
|
||||
.setStyle(ButtonStyle.Primary)
|
||||
.setLabel("Note")
|
||||
.setEmoji("📝")
|
||||
.setDisabled(!(await modactions.hasNotePermission(executingMember, interaction.channelId)))
|
||||
.setCustomId(serializeCustomId({ action: ModMenuActionType.NOTE, target: interaction.targetId })),
|
||||
new ButtonBuilder()
|
||||
.setStyle(ButtonStyle.Primary)
|
||||
.setLabel("Warn")
|
||||
.setEmoji("⚠️")
|
||||
.setDisabled(!(await modactions.hasWarnPermission(executingMember, interaction.channelId)))
|
||||
.setCustomId(serializeCustomId({ action: ModMenuActionType.WARN, target: interaction.targetId })),
|
||||
new ButtonBuilder()
|
||||
.setStyle(ButtonStyle.Primary)
|
||||
.setLabel("Mute")
|
||||
.setEmoji("🔇")
|
||||
.setDisabled(!(await modactions.hasMutePermission(executingMember, interaction.channelId)))
|
||||
.setCustomId(serializeCustomId({ action: ModMenuActionType.MUTE, target: interaction.targetId })),
|
||||
new ButtonBuilder()
|
||||
.setStyle(ButtonStyle.Primary)
|
||||
.setLabel("Ban")
|
||||
.setEmoji("🚫")
|
||||
.setDisabled(!(await modactions.hasBanPermission(executingMember, interaction.channelId)))
|
||||
.setCustomId(serializeCustomId({ action: ModMenuActionType.BAN, target: interaction.targetId })),
|
||||
] satisfies ButtonBuilder[];
|
||||
|
||||
const navigationRow = new ActionRowBuilder<ButtonBuilder>().addComponents(navigationButtons);
|
||||
const moderationRow = new ActionRowBuilder<ButtonBuilder>().addComponents(moderationButtons);
|
||||
|
||||
let page = 1;
|
||||
await interaction
|
||||
.editReply({
|
||||
embeds: [await loadPage(page)],
|
||||
components: [navigationRow, moderationRow],
|
||||
})
|
||||
.then(async (currentPage) => {
|
||||
const collector = await currentPage.createMessageComponentCollector({
|
||||
time: MOD_MENU_TIMEOUT,
|
||||
});
|
||||
|
||||
collector.on("collect", async (i) => {
|
||||
const opts = deserializeCustomId(i.customId);
|
||||
if (opts.action == ModMenuActionType.PAGE) {
|
||||
await i.deferUpdate().catch((err) => logger.error(`Mod menu defer failed: ${err}`));
|
||||
}
|
||||
|
||||
// Update displayed embed if any navigation buttons were used
|
||||
if (opts.action == ModMenuActionType.PAGE && opts.target == ModMenuNavigationType.INFO && infoEmbed != null) {
|
||||
infoButton
|
||||
.setLabel("Cases")
|
||||
.setEmoji("📋")
|
||||
.setCustomId(serializeCustomId({ action: ModMenuActionType.PAGE, target: ModMenuNavigationType.CASES }));
|
||||
firstButton.setDisabled(true);
|
||||
prevButton.setDisabled(true);
|
||||
nextButton.setDisabled(true);
|
||||
lastButton.setDisabled(true);
|
||||
|
||||
await i
|
||||
.editReply({
|
||||
embeds: [infoEmbed],
|
||||
components: [navigationRow, moderationRow],
|
||||
})
|
||||
.catch((err) => logger.error(`Mod menu info view failed: ${err}`));
|
||||
} else if (opts.action == ModMenuActionType.PAGE && opts.target == ModMenuNavigationType.CASES) {
|
||||
infoButton
|
||||
.setLabel("Info")
|
||||
.setEmoji("ℹ")
|
||||
.setCustomId(serializeCustomId({ action: ModMenuActionType.PAGE, target: ModMenuNavigationType.INFO }));
|
||||
updateNavButtonState(firstButton, prevButton, nextButton, lastButton, page, totalPages);
|
||||
|
||||
await i
|
||||
.editReply({
|
||||
embeds: [await loadPage(page)],
|
||||
components: [navigationRow, moderationRow],
|
||||
})
|
||||
.catch((err) => logger.error(`Mod menu cases view failed: ${err}`));
|
||||
} else if (opts.action == ModMenuActionType.PAGE) {
|
||||
let pageDelta = 0;
|
||||
switch (opts.target) {
|
||||
case ModMenuNavigationType.PREV:
|
||||
pageDelta = -1;
|
||||
break;
|
||||
case ModMenuNavigationType.NEXT:
|
||||
pageDelta = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
let newPage = 1;
|
||||
if (opts.target == ModMenuNavigationType.PREV || opts.target == ModMenuNavigationType.NEXT) {
|
||||
newPage = Math.max(Math.min(page + pageDelta, totalPages), 1);
|
||||
} else if (opts.target == ModMenuNavigationType.FIRST) {
|
||||
newPage = 1;
|
||||
} else if (opts.target == ModMenuNavigationType.LAST) {
|
||||
newPage = totalPages;
|
||||
}
|
||||
|
||||
if (newPage != page) {
|
||||
updateNavButtonState(firstButton, prevButton, nextButton, lastButton, newPage, totalPages);
|
||||
|
||||
await i
|
||||
.editReply({
|
||||
embeds: [await loadPage(newPage)],
|
||||
components: [navigationRow, moderationRow],
|
||||
})
|
||||
.catch((err) => logger.error(`Mod menu navigation failed: ${err}`));
|
||||
|
||||
page = newPage;
|
||||
}
|
||||
} else if (opts.action == ModMenuActionType.NOTE) {
|
||||
await launchNoteActionModal(pluginData, i as ButtonInteraction, opts.target);
|
||||
} else if (opts.action == ModMenuActionType.WARN) {
|
||||
await launchWarnActionModal(pluginData, i as ButtonInteraction, opts.target);
|
||||
} else if (opts.action == ModMenuActionType.MUTE) {
|
||||
await launchMuteActionModal(pluginData, i as ButtonInteraction, opts.target);
|
||||
} else if (opts.action == ModMenuActionType.BAN) {
|
||||
await launchBanActionModal(pluginData, i as ButtonInteraction, opts.target);
|
||||
}
|
||||
|
||||
collector.resetTimer();
|
||||
});
|
||||
|
||||
// Remove components on timeout.
|
||||
collector.on("end", async (_, reason) => {
|
||||
if (reason !== "messageDelete") {
|
||||
await interaction
|
||||
.editReply({
|
||||
components: [],
|
||||
})
|
||||
.catch((err) => logger.error(`Mod menu timeout failed: ${err}`));
|
||||
}
|
||||
});
|
||||
})
|
||||
.catch((err) => logger.error(`Mod menu setup failed: ${err}`));
|
||||
}
|
||||
|
||||
function serializeCustomId(opts: ModMenuActionOpts) {
|
||||
return `${opts.action}:${opts.target}`;
|
||||
}
|
||||
|
||||
function deserializeCustomId(customId: string): ModMenuActionOpts {
|
||||
const opts: ModMenuActionOpts = {
|
||||
action: customId.split(":")[0] as ModMenuActionType,
|
||||
target: customId.split(":")[1],
|
||||
};
|
||||
|
||||
return opts;
|
||||
}
|
||||
|
||||
function updateNavButtonState(
|
||||
firstButton: ButtonBuilder,
|
||||
prevButton: ButtonBuilder,
|
||||
nextButton: ButtonBuilder,
|
||||
lastButton: ButtonBuilder,
|
||||
currentPage: number,
|
||||
totalPages: number,
|
||||
) {
|
||||
if (currentPage > 1) {
|
||||
firstButton.setDisabled(false);
|
||||
prevButton.setDisabled(false);
|
||||
} else {
|
||||
firstButton.setDisabled(true);
|
||||
prevButton.setDisabled(true);
|
||||
}
|
||||
|
||||
if (currentPage == totalPages) {
|
||||
nextButton.setDisabled(true);
|
||||
lastButton.setDisabled(true);
|
||||
} else {
|
||||
nextButton.setDisabled(false);
|
||||
lastButton.setDisabled(false);
|
||||
}
|
||||
}
|
11
backend/src/plugins/ContextMenus/commands/MuteUserCtxCmd.ts
Normal file
11
backend/src/plugins/ContextMenus/commands/MuteUserCtxCmd.ts
Normal file
|
@ -0,0 +1,11 @@
|
|||
import { PermissionFlagsBits } from "discord.js";
|
||||
import { guildPluginUserContextMenuCommand } from "knub";
|
||||
import { launchMuteActionModal } from "../actions/mute";
|
||||
|
||||
export const MuteCmd = guildPluginUserContextMenuCommand({
|
||||
name: "Mute",
|
||||
defaultMemberPermissions: PermissionFlagsBits.ModerateMembers.toString(),
|
||||
async run({ pluginData, interaction }) {
|
||||
await launchMuteActionModal(pluginData, interaction, interaction.targetId);
|
||||
},
|
||||
});
|
11
backend/src/plugins/ContextMenus/commands/NoteUserCtxCmd.ts
Normal file
11
backend/src/plugins/ContextMenus/commands/NoteUserCtxCmd.ts
Normal file
|
@ -0,0 +1,11 @@
|
|||
import { PermissionFlagsBits } from "discord.js";
|
||||
import { guildPluginUserContextMenuCommand } from "knub";
|
||||
import { launchNoteActionModal } from "../actions/note";
|
||||
|
||||
export const NoteCmd = guildPluginUserContextMenuCommand({
|
||||
name: "Note",
|
||||
defaultMemberPermissions: PermissionFlagsBits.ManageMessages.toString(),
|
||||
async run({ pluginData, interaction }) {
|
||||
await launchNoteActionModal(pluginData, interaction, interaction.targetId);
|
||||
},
|
||||
});
|
11
backend/src/plugins/ContextMenus/commands/WarnUserCtxCmd.ts
Normal file
11
backend/src/plugins/ContextMenus/commands/WarnUserCtxCmd.ts
Normal file
|
@ -0,0 +1,11 @@
|
|||
import { PermissionFlagsBits } from "discord.js";
|
||||
import { guildPluginUserContextMenuCommand } from "knub";
|
||||
import { launchWarnActionModal } from "../actions/warn";
|
||||
|
||||
export const WarnCmd = guildPluginUserContextMenuCommand({
|
||||
name: "Warn",
|
||||
defaultMemberPermissions: PermissionFlagsBits.ManageMessages.toString(),
|
||||
async run({ pluginData, interaction }) {
|
||||
await launchWarnActionModal(pluginData, interaction, interaction.targetId);
|
||||
},
|
||||
});
|
|
@ -1,12 +0,0 @@
|
|||
import { contextMenuEvt } from "../types";
|
||||
import { routeContextAction } from "../utils/contextRouter";
|
||||
|
||||
export const ContextClickedEvt = contextMenuEvt({
|
||||
event: "interactionCreate",
|
||||
|
||||
async listener(meta) {
|
||||
if (!meta.args.interaction.isContextMenuCommand()) return;
|
||||
const inter = meta.args.interaction;
|
||||
await routeContextAction(meta.pluginData, inter);
|
||||
},
|
||||
});
|
|
@ -1,25 +1,43 @@
|
|||
import { APIEmbed, Awaitable } from "discord.js";
|
||||
import * as t from "io-ts";
|
||||
import { BasePluginType, guildPluginEventListener } from "knub";
|
||||
import { GuildContextMenuLinks } from "../../data/GuildContextMenuLinks";
|
||||
import { BasePluginType } from "knub";
|
||||
import { GuildCases } from "../../data/GuildCases";
|
||||
|
||||
export const ConfigSchema = t.type({
|
||||
can_use: t.boolean,
|
||||
|
||||
user_muteindef: t.boolean,
|
||||
user_mute1d: t.boolean,
|
||||
user_mute1h: t.boolean,
|
||||
user_info: t.boolean,
|
||||
message_clean10: t.boolean,
|
||||
message_clean25: t.boolean,
|
||||
message_clean50: t.boolean,
|
||||
can_open_mod_menu: t.boolean,
|
||||
});
|
||||
export type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
|
||||
|
||||
export interface ContextMenuPluginType extends BasePluginType {
|
||||
config: TConfigSchema;
|
||||
state: {
|
||||
contextMenuLinks: GuildContextMenuLinks;
|
||||
cases: GuildCases;
|
||||
};
|
||||
}
|
||||
|
||||
export const contextMenuEvt = guildPluginEventListener<ContextMenuPluginType>();
|
||||
export const enum ModMenuActionType {
|
||||
PAGE = "page",
|
||||
NOTE = "note",
|
||||
WARN = "warn",
|
||||
CLEAN = "clean",
|
||||
MUTE = "mute",
|
||||
BAN = "ban",
|
||||
}
|
||||
|
||||
export const enum ModMenuNavigationType {
|
||||
FIRST = "first",
|
||||
PREV = "prev",
|
||||
NEXT = "next",
|
||||
LAST = "last",
|
||||
INFO = "info",
|
||||
CASES = "cases",
|
||||
}
|
||||
|
||||
export interface ModMenuActionOpts {
|
||||
action: ModMenuActionType;
|
||||
target: string;
|
||||
}
|
||||
|
||||
export type LoadModMenuPageFn = (page: number) => Awaitable<APIEmbed>;
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
import { ContextMenuCommandInteraction } from "discord.js";
|
||||
import { GuildPluginData } from "knub";
|
||||
import { ContextMenuPluginType } from "../types";
|
||||
import { hardcodedActions } from "./hardcodedContextOptions";
|
||||
|
||||
export async function routeContextAction(
|
||||
pluginData: GuildPluginData<ContextMenuPluginType>,
|
||||
interaction: ContextMenuCommandInteraction,
|
||||
) {
|
||||
const contextLink = await pluginData.state.contextMenuLinks.get(interaction.commandId);
|
||||
if (!contextLink) return;
|
||||
hardcodedActions[contextLink.action_name](pluginData, interaction);
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
import { cleanAction } from "../actions/clean";
|
||||
import { muteAction } from "../actions/mute";
|
||||
import { userInfoAction } from "../actions/userInfo";
|
||||
|
||||
export const hardcodedContext: Record<string, string> = {
|
||||
user_muteindef: "Mute Indefinitely",
|
||||
user_mute1d: "Mute for 1 day",
|
||||
user_mute1h: "Mute for 1 hour",
|
||||
user_info: "Get Info",
|
||||
message_clean10: "Clean 10 messages",
|
||||
message_clean25: "Clean 25 messages",
|
||||
message_clean50: "Clean 50 messages",
|
||||
};
|
||||
|
||||
export const hardcodedActions = {
|
||||
user_muteindef: (pluginData, interaction) => muteAction(pluginData, undefined, interaction),
|
||||
user_mute1d: (pluginData, interaction) => muteAction(pluginData, "1d", interaction),
|
||||
user_mute1h: (pluginData, interaction) => muteAction(pluginData, "1h", interaction),
|
||||
user_info: (pluginData, interaction) => userInfoAction(pluginData, interaction),
|
||||
message_clean10: (pluginData, interaction) => cleanAction(pluginData, 10, interaction),
|
||||
message_clean25: (pluginData, interaction) => cleanAction(pluginData, 25, interaction),
|
||||
message_clean50: (pluginData, interaction) => cleanAction(pluginData, 50, interaction),
|
||||
};
|
|
@ -1,39 +0,0 @@
|
|||
import { ApplicationCommandData, ApplicationCommandType } from "discord.js";
|
||||
import { GuildPluginData } from "knub";
|
||||
import { LogsPlugin } from "../../../plugins/Logs/LogsPlugin";
|
||||
import { ContextMenuPluginType } from "../types";
|
||||
import { hardcodedContext } from "./hardcodedContextOptions";
|
||||
|
||||
export async function loadAllCommands(pluginData: GuildPluginData<ContextMenuPluginType>) {
|
||||
const comms = await pluginData.client.application!.commands;
|
||||
const cfg = pluginData.config.get();
|
||||
const newCommands: ApplicationCommandData[] = [];
|
||||
const addedNames: string[] = [];
|
||||
|
||||
for (const [name, label] of Object.entries(hardcodedContext)) {
|
||||
if (!cfg[name]) continue;
|
||||
|
||||
const type = name.startsWith("user") ? ApplicationCommandType.User : ApplicationCommandType.Message;
|
||||
const data: ApplicationCommandData = {
|
||||
type,
|
||||
name: label,
|
||||
};
|
||||
|
||||
addedNames.push(name);
|
||||
newCommands.push(data);
|
||||
}
|
||||
|
||||
const setCommands = await comms.set(newCommands, pluginData.guild.id).catch((e) => {
|
||||
pluginData.getPlugin(LogsPlugin).logBotAlert({ body: `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]);
|
||||
}
|
||||
}
|
|
@ -41,7 +41,12 @@ import { CreateUnbanCaseOnManualUnbanEvt } from "./events/CreateUnbanCaseOnManua
|
|||
import { PostAlertOnMemberJoinEvt } from "./events/PostAlertOnMemberJoinEvt";
|
||||
import { banUserId } from "./functions/banUserId";
|
||||
import { clearTempban } from "./functions/clearTempban";
|
||||
import { hasMutePermission } from "./functions/hasMutePerm";
|
||||
import {
|
||||
hasBanPermission,
|
||||
hasMutePermission,
|
||||
hasNotePermission,
|
||||
hasWarnPermission,
|
||||
} from "./functions/hasModActionPerm";
|
||||
import { kickMember } from "./functions/kickMember";
|
||||
import { offModActionsEvent } from "./functions/offModActionsEvent";
|
||||
import { onModActionsEvent } from "./functions/onModActionsEvent";
|
||||
|
@ -158,7 +163,7 @@ export const ModActionsPlugin = zeppelinGuildPlugin<ModActionsPluginType>()({
|
|||
public: {
|
||||
warnMember(pluginData) {
|
||||
return (member: GuildMember, reason: string, warnOptions?: WarnOptions) => {
|
||||
warnMember(pluginData, member, reason, warnOptions);
|
||||
return warnMember(pluginData, member, reason, warnOptions);
|
||||
};
|
||||
},
|
||||
|
||||
|
@ -170,7 +175,7 @@ export const ModActionsPlugin = zeppelinGuildPlugin<ModActionsPluginType>()({
|
|||
|
||||
banUserId(pluginData) {
|
||||
return (userId: string, reason?: string, banOptions?: BanOptions, banTime?: number) => {
|
||||
banUserId(pluginData, userId, reason, banOptions, banTime);
|
||||
return banUserId(pluginData, userId, reason, banOptions, banTime);
|
||||
};
|
||||
},
|
||||
|
||||
|
@ -180,12 +185,30 @@ export const ModActionsPlugin = zeppelinGuildPlugin<ModActionsPluginType>()({
|
|||
};
|
||||
},
|
||||
|
||||
hasNotePermission(pluginData) {
|
||||
return (member: GuildMember, channelId: Snowflake) => {
|
||||
return hasNotePermission(pluginData, member, channelId);
|
||||
};
|
||||
},
|
||||
|
||||
hasWarnPermission(pluginData) {
|
||||
return (member: GuildMember, channelId: Snowflake) => {
|
||||
return hasWarnPermission(pluginData, member, channelId);
|
||||
};
|
||||
},
|
||||
|
||||
hasMutePermission(pluginData) {
|
||||
return (member: GuildMember, channelId: Snowflake) => {
|
||||
return hasMutePermission(pluginData, member, channelId);
|
||||
};
|
||||
},
|
||||
|
||||
hasBanPermission(pluginData) {
|
||||
return (member: GuildMember, channelId: Snowflake) => {
|
||||
return hasBanPermission(pluginData, member, channelId);
|
||||
};
|
||||
},
|
||||
|
||||
on: mapToPublicFn(onModActionsEvent),
|
||||
off: mapToPublicFn(offModActionsEvent),
|
||||
getEventEmitter(pluginData) {
|
||||
|
|
35
backend/src/plugins/ModActions/functions/hasModActionPerm.ts
Normal file
35
backend/src/plugins/ModActions/functions/hasModActionPerm.ts
Normal file
|
@ -0,0 +1,35 @@
|
|||
import { GuildMember, Snowflake } from "discord.js";
|
||||
import { GuildPluginData } from "knub";
|
||||
import { ModActionsPluginType } from "../types";
|
||||
|
||||
export async function hasNotePermission(
|
||||
pluginData: GuildPluginData<ModActionsPluginType>,
|
||||
member: GuildMember,
|
||||
channelId: Snowflake,
|
||||
) {
|
||||
return (await pluginData.config.getMatchingConfig({ member, channelId })).can_note;
|
||||
}
|
||||
|
||||
export async function hasWarnPermission(
|
||||
pluginData: GuildPluginData<ModActionsPluginType>,
|
||||
member: GuildMember,
|
||||
channelId: Snowflake,
|
||||
) {
|
||||
return (await pluginData.config.getMatchingConfig({ member, channelId })).can_warn;
|
||||
}
|
||||
|
||||
export async function hasMutePermission(
|
||||
pluginData: GuildPluginData<ModActionsPluginType>,
|
||||
member: GuildMember,
|
||||
channelId: Snowflake,
|
||||
) {
|
||||
return (await pluginData.config.getMatchingConfig({ member, channelId })).can_mute;
|
||||
}
|
||||
|
||||
export async function hasBanPermission(
|
||||
pluginData: GuildPluginData<ModActionsPluginType>,
|
||||
member: GuildMember,
|
||||
channelId: Snowflake,
|
||||
) {
|
||||
return (await pluginData.config.getMatchingConfig({ member, channelId })).can_ban;
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
import { GuildMember, Snowflake } from "discord.js";
|
||||
import { GuildPluginData } from "knub";
|
||||
import { ModActionsPluginType } from "../types";
|
||||
|
||||
export async function hasMutePermission(
|
||||
pluginData: GuildPluginData<ModActionsPluginType>,
|
||||
member: GuildMember,
|
||||
channelId: Snowflake,
|
||||
) {
|
||||
return (await pluginData.config.getMatchingConfig({ member, channelId })).can_mute;
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
import { Message, Snowflake, TextChannel, User } from "discord.js";
|
||||
import { Message, ModalSubmitInteraction, Snowflake, TextChannel, User } from "discord.js";
|
||||
import { GuildPluginData } from "knub";
|
||||
import { allowTimeout } from "../../../RegExpRunner";
|
||||
import { commandTypeHelpers as ct } from "../../../commandTypes";
|
||||
|
@ -77,17 +77,24 @@ export interface CleanArgs {
|
|||
"has-invites"?: boolean;
|
||||
match?: RegExp;
|
||||
"to-id"?: string;
|
||||
"response-interaction"?: ModalSubmitInteraction;
|
||||
}
|
||||
|
||||
export async function cleanCmd(pluginData: GuildPluginData<UtilityPluginType>, args: CleanArgs | any, msg) {
|
||||
if (args.count > MAX_CLEAN_COUNT || args.count <= 0) {
|
||||
sendErrorMessage(pluginData, msg.channel, `Clean count must be between 1 and ${MAX_CLEAN_COUNT}`);
|
||||
sendErrorMessage(
|
||||
pluginData,
|
||||
msg.channel,
|
||||
`Clean count must be between 1 and ${MAX_CLEAN_COUNT}`,
|
||||
undefined,
|
||||
args["response-interaction"],
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const targetChannel = args.channel ? pluginData.guild.channels.cache.get(args.channel as Snowflake) : msg.channel;
|
||||
if (!targetChannel?.isTextBased()) {
|
||||
sendErrorMessage(pluginData, msg.channel, `Invalid channel specified`);
|
||||
sendErrorMessage(pluginData, msg.channel, `Invalid channel specified`, undefined, args["response-interaction"]);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -99,12 +106,21 @@ export async function cleanCmd(pluginData: GuildPluginData<UtilityPluginType>, a
|
|||
categoryId: targetChannel.parentId,
|
||||
});
|
||||
if (configForTargetChannel.can_clean !== true) {
|
||||
sendErrorMessage(pluginData, msg.channel, `Missing permissions to use clean on that channel`);
|
||||
sendErrorMessage(
|
||||
pluginData,
|
||||
msg.channel,
|
||||
`Missing permissions to use clean on that channel`,
|
||||
undefined,
|
||||
args["response-interaction"],
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const cleaningMessage = msg.channel.send("Cleaning...");
|
||||
let cleaningMessage: Message | undefined = undefined;
|
||||
if (!args["response-interaction"]) {
|
||||
cleaningMessage = await msg.channel.send("Cleaning...");
|
||||
}
|
||||
|
||||
const messagesToClean: Message[] = [];
|
||||
let beforeId = msg.id;
|
||||
|
@ -202,19 +218,31 @@ export async function cleanCmd(pluginData: GuildPluginData<UtilityPluginType>, a
|
|||
}
|
||||
}
|
||||
|
||||
responseMsg = await sendSuccessMessage(pluginData, msg.channel, responseText);
|
||||
responseMsg = await sendSuccessMessage(
|
||||
pluginData,
|
||||
msg.channel,
|
||||
responseText,
|
||||
undefined,
|
||||
args["response-interaction"],
|
||||
);
|
||||
} else {
|
||||
const responseText = `Found no messages to clean${note ? ` (${note})` : ""}!`;
|
||||
responseMsg = await sendErrorMessage(pluginData, msg.channel, responseText);
|
||||
responseMsg = await sendErrorMessage(
|
||||
pluginData,
|
||||
msg.channel,
|
||||
responseText,
|
||||
undefined,
|
||||
args["response-interaction"],
|
||||
);
|
||||
}
|
||||
|
||||
await (await cleaningMessage).delete();
|
||||
cleaningMessage?.delete();
|
||||
|
||||
if (targetChannel.id === msg.channel.id) {
|
||||
// Delete the !clean command and the bot response if a different channel wasn't specified
|
||||
// (so as not to spam the cleaned channel with the command itself)
|
||||
msg.delete().catch(noop);
|
||||
setTimeout(() => {
|
||||
msg.delete().catch(noop);
|
||||
responseMsg?.delete().catch(noop);
|
||||
}, CLEAN_COMMAND_DELETE_DELAY);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue