mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 12:25:02 +00:00
Port Cases plugin
This commit is contained in:
parent
a848e00fdd
commit
479cb56928
7 changed files with 349 additions and 0 deletions
59
backend/src/plugins/Cases/functions/postToCaseLogChannel.ts
Normal file
59
backend/src/plugins/Cases/functions/postToCaseLogChannel.ts
Normal file
|
@ -0,0 +1,59 @@
|
|||
import { plugin, PluginData } from "knub";
|
||||
import { CasesPluginType } from "../types";
|
||||
import { Message, MessageContent, MessageFile, TextChannel } from "eris";
|
||||
import { isDiscordRESTError } from "../../../utils";
|
||||
import { LogType } from "../../../data/LogType";
|
||||
import { Case } from "../../../data/entities/Case";
|
||||
import { getCaseEmbed } from "./getCaseEmbed";
|
||||
import { resolveCaseId } from "./resolveCaseId";
|
||||
|
||||
export async function postToCaseLogChannel(
|
||||
pluginData: PluginData<CasesPluginType>,
|
||||
content: MessageContent,
|
||||
file: MessageFile = null,
|
||||
): Promise<Message> {
|
||||
const caseLogChannelId = pluginData.config.get().case_log_channel;
|
||||
if (!caseLogChannelId) return;
|
||||
|
||||
const caseLogChannel = pluginData.guild.channels.get(caseLogChannelId);
|
||||
if (!caseLogChannel || !(caseLogChannel instanceof TextChannel)) return;
|
||||
|
||||
let result;
|
||||
try {
|
||||
result = await caseLogChannel.createMessage(content, file);
|
||||
} catch (e) {
|
||||
if (isDiscordRESTError(e) && (e.code === 50013 || e.code === 50001)) {
|
||||
console.warn(
|
||||
`Missing permissions to post mod cases in <#${caseLogChannel.id}> in guild ${pluginData.guild.name} (${pluginData.guild.id})`,
|
||||
);
|
||||
pluginData.state.logs.log(LogType.BOT_ALERT, {
|
||||
body: `Missing permissions to post mod cases in <#${caseLogChannel.id}>`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
throw e;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function postCaseToCaseLogChannel(
|
||||
pluginData: PluginData<CasesPluginType>,
|
||||
caseOrCaseId: Case | number,
|
||||
): Promise<Message> {
|
||||
const theCase = await pluginData.state.cases.find(resolveCaseId(caseOrCaseId));
|
||||
if (!theCase) return;
|
||||
|
||||
const caseEmbed = await getCaseEmbed(pluginData, caseOrCaseId);
|
||||
if (!caseEmbed) return;
|
||||
|
||||
try {
|
||||
return postToCaseLogChannel(pluginData, caseEmbed);
|
||||
} catch (e) {
|
||||
pluginData.state.logs.log(LogType.BOT_ALERT, {
|
||||
body: `Failed to post case #${theCase.case_number} to the case log channel`,
|
||||
});
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue