When updating cases, update existing case embed in case log channel if possible

This commit is contained in:
Dragory 2020-08-10 02:35:47 +03:00
parent 7cd58c2b6d
commit 19a82b767f
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
4 changed files with 46 additions and 4 deletions

View file

@ -29,6 +29,12 @@ export class Case {
@Column() pp_name: string;
/**
* ID of the channel and message where this case was logged.
* Format: "channelid-messageid"
*/
@Column() log_message_id: string;
@OneToMany(
type => CaseNote,
note => note.case,

View file

@ -0,0 +1,21 @@
import { MigrationInterface, QueryRunner } from "typeorm";
import { TableColumn } from "typeorm/index";
export class AddLogMessageIdToCases1597015567215 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<any> {
await queryRunner.addColumn(
"cases",
new TableColumn({
name: "log_message_id",
type: "varchar",
length: "64",
isNullable: true,
default: null,
}),
);
}
public async down(queryRunner: QueryRunner): Promise<any> {
await queryRunner.dropColumn("cases", "log_message_id");
}
}

View file

@ -1,6 +1,6 @@
import { CaseArgs, CasesPluginType } from "../types";
import { resolveUser } from "../../../utils";
import { PluginData } from "knub";
import { plugin, PluginData } from "knub";
import { createCaseNote } from "./createCaseNote";
import { postCaseToCaseLogChannel } from "./postToCaseLogChannel";
import { logger } from "../../../logger";

View file

@ -12,7 +12,7 @@ export async function postToCaseLogChannel(
pluginData: PluginData<CasesPluginType>,
content: MessageContent,
file: MessageFile = null,
): Promise<Message> {
): Promise<Message | null> {
const caseLogChannelId = pluginData.config.get().case_log_channel;
if (!caseLogChannelId) return;
@ -42,15 +42,30 @@ export async function postToCaseLogChannel(
export async function postCaseToCaseLogChannel(
pluginData: PluginData<CasesPluginType>,
caseOrCaseId: Case | number,
): Promise<Message> {
): Promise<Message | null> {
const theCase = await pluginData.state.cases.find(resolveCaseId(caseOrCaseId));
if (!theCase) return;
const caseEmbed = await getCaseEmbed(pluginData, caseOrCaseId);
if (!caseEmbed) return;
if (theCase.log_message_id) {
const [channelId, messageId] = theCase.log_message_id.split("-");
try {
await pluginData.client.editMessage(channelId, messageId, caseEmbed);
return;
} catch (e) {} // tslint:disable-line:no-empty
}
try {
return postToCaseLogChannel(pluginData, caseEmbed);
const postedMessage = await postToCaseLogChannel(pluginData, caseEmbed);
if (postedMessage) {
await pluginData.state.cases.update(theCase.id, {
log_message_id: `${postedMessage.channel.id}-${postedMessage.id}`,
});
}
return postedMessage;
} catch (e) {
pluginData.state.logs.log(LogType.BOT_ALERT, {
body: `Failed to post case #${theCase.case_number} to the case log channel`,