When updating cases, update existing case embed in case log channel if possible
This commit is contained in:
parent
7cd58c2b6d
commit
19a82b767f
4 changed files with 46 additions and 4 deletions
|
@ -29,6 +29,12 @@ export class Case {
|
||||||
|
|
||||||
@Column() pp_name: string;
|
@Column() pp_name: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ID of the channel and message where this case was logged.
|
||||||
|
* Format: "channelid-messageid"
|
||||||
|
*/
|
||||||
|
@Column() log_message_id: string;
|
||||||
|
|
||||||
@OneToMany(
|
@OneToMany(
|
||||||
type => CaseNote,
|
type => CaseNote,
|
||||||
note => note.case,
|
note => note.case,
|
||||||
|
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
import { CaseArgs, CasesPluginType } from "../types";
|
import { CaseArgs, CasesPluginType } from "../types";
|
||||||
import { resolveUser } from "../../../utils";
|
import { resolveUser } from "../../../utils";
|
||||||
import { PluginData } from "knub";
|
import { plugin, PluginData } from "knub";
|
||||||
import { createCaseNote } from "./createCaseNote";
|
import { createCaseNote } from "./createCaseNote";
|
||||||
import { postCaseToCaseLogChannel } from "./postToCaseLogChannel";
|
import { postCaseToCaseLogChannel } from "./postToCaseLogChannel";
|
||||||
import { logger } from "../../../logger";
|
import { logger } from "../../../logger";
|
||||||
|
|
|
@ -12,7 +12,7 @@ export async function postToCaseLogChannel(
|
||||||
pluginData: PluginData<CasesPluginType>,
|
pluginData: PluginData<CasesPluginType>,
|
||||||
content: MessageContent,
|
content: MessageContent,
|
||||||
file: MessageFile = null,
|
file: MessageFile = null,
|
||||||
): Promise<Message> {
|
): Promise<Message | null> {
|
||||||
const caseLogChannelId = pluginData.config.get().case_log_channel;
|
const caseLogChannelId = pluginData.config.get().case_log_channel;
|
||||||
if (!caseLogChannelId) return;
|
if (!caseLogChannelId) return;
|
||||||
|
|
||||||
|
@ -42,15 +42,30 @@ export async function postToCaseLogChannel(
|
||||||
export async function postCaseToCaseLogChannel(
|
export async function postCaseToCaseLogChannel(
|
||||||
pluginData: PluginData<CasesPluginType>,
|
pluginData: PluginData<CasesPluginType>,
|
||||||
caseOrCaseId: Case | number,
|
caseOrCaseId: Case | number,
|
||||||
): Promise<Message> {
|
): Promise<Message | null> {
|
||||||
const theCase = await pluginData.state.cases.find(resolveCaseId(caseOrCaseId));
|
const theCase = await pluginData.state.cases.find(resolveCaseId(caseOrCaseId));
|
||||||
if (!theCase) return;
|
if (!theCase) return;
|
||||||
|
|
||||||
const caseEmbed = await getCaseEmbed(pluginData, caseOrCaseId);
|
const caseEmbed = await getCaseEmbed(pluginData, caseOrCaseId);
|
||||||
if (!caseEmbed) return;
|
if (!caseEmbed) return;
|
||||||
|
|
||||||
|
if (theCase.log_message_id) {
|
||||||
|
const [channelId, messageId] = theCase.log_message_id.split("-");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return postToCaseLogChannel(pluginData, caseEmbed);
|
await pluginData.client.editMessage(channelId, messageId, caseEmbed);
|
||||||
|
return;
|
||||||
|
} catch (e) {} // tslint:disable-line:no-empty
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
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) {
|
} catch (e) {
|
||||||
pluginData.state.logs.log(LogType.BOT_ALERT, {
|
pluginData.state.logs.log(LogType.BOT_ALERT, {
|
||||||
body: `Failed to post case #${theCase.case_number} to the case log channel`,
|
body: `Failed to post case #${theCase.case_number} to the case log channel`,
|
||||||
|
|
Loading…
Add table
Reference in a new issue