feat(tags): remove foreign key from tag_responses, use raw deletion events

This commit is contained in:
Dragory 2021-10-17 11:49:34 +03:00
parent 86b01f2991
commit 8b486e280c
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
5 changed files with 45 additions and 33 deletions

View file

@ -86,4 +86,18 @@ export class GuildTags extends BaseGuildRepository {
response_message_id: responseMessageId, response_message_id: responseMessageId,
}); });
} }
async deleteResponseByCommandMessageId(messageId: string): Promise<void> {
await this.tagResponses.delete({
guild_id: this.guildId,
command_message_id: messageId,
});
}
async deleteResponseByResponseMessageId(messageId: string): Promise<void> {
await this.tagResponses.delete({
guild_id: this.guildId,
response_message_id: messageId,
});
}
} }

View file

@ -73,6 +73,11 @@ export const TagsPlugin = zeppelinGuildPlugin<TagsPluginType>()({
TagCreateCmd, TagCreateCmd,
], ],
// prettier-ignore
events: [
onMessageDelete,
],
public: { public: {
renderTagBody: mapToPublicFn(renderTagBody), renderTagBody: mapToPublicFn(renderTagBody),
findTagByName: mapToPublicFn(findTagByName), findTagByName: mapToPublicFn(findTagByName),
@ -117,9 +122,6 @@ export const TagsPlugin = zeppelinGuildPlugin<TagsPluginType>()({
state.onMessageCreateFn = (msg) => onMessageCreate(pluginData, msg); state.onMessageCreateFn = (msg) => onMessageCreate(pluginData, msg);
state.savedMessages.events.on("create", state.onMessageCreateFn); state.savedMessages.events.on("create", state.onMessageCreateFn);
state.onMessageDeleteFn = (msg) => onMessageDelete(pluginData, msg);
state.savedMessages.events.on("delete", state.onMessageDeleteFn);
const timeAndDate = pluginData.getPlugin(TimeAndDatePlugin); const timeAndDate = pluginData.getPlugin(TimeAndDatePlugin);
const tz = timeAndDate.getGuildTz(); const tz = timeAndDate.getGuildTz();
@ -263,6 +265,5 @@ export const TagsPlugin = zeppelinGuildPlugin<TagsPluginType>()({
beforeUnload(pluginData) { beforeUnload(pluginData) {
pluginData.state.savedMessages.events.off("create", pluginData.state.onMessageCreateFn); pluginData.state.savedMessages.events.off("create", pluginData.state.onMessageCreateFn);
pluginData.state.savedMessages.events.off("delete", pluginData.state.onMessageDeleteFn);
}, },
}); });

View file

@ -54,7 +54,6 @@ export interface TagsPluginType extends BasePluginType {
logs: GuildLogs; logs: GuildLogs;
onMessageCreateFn; onMessageCreateFn;
onMessageDeleteFn;
tagFunctions: any; tagFunctions: any;
}; };

View file

@ -110,9 +110,7 @@ export async function onMessageCreate(pluginData: GuildPluginData<TagsPluginType
// Save the command-response message pair once the message is in our database // Save the command-response message pair once the message is in our database
const deleteWithCommand = tagResult.category?.delete_with_command ?? config.delete_with_command; const deleteWithCommand = tagResult.category?.delete_with_command ?? config.delete_with_command;
if (deleteWithCommand) { if (deleteWithCommand) {
pluginData.state.savedMessages.onceMessageAvailable(responseMsg.id, async () => { await pluginData.state.tags.addResponse(msg.id, responseMsg.id);
await pluginData.state.tags.addResponse(msg.id, responseMsg.id);
});
} }
const deleteInvoke = tagResult.category?.auto_delete_command ?? config.auto_delete_command; const deleteInvoke = tagResult.category?.auto_delete_command ?? config.auto_delete_command;

View file

@ -1,32 +1,32 @@
import { Snowflake, TextChannel } from "discord.js"; import { Snowflake, TextChannel } from "discord.js";
import { GuildPluginData } from "knub"; import { GuildPluginData, typedGuildEventListener } from "knub";
import { SavedMessage } from "../../../data/entities/SavedMessage"; import { SavedMessage } from "../../../data/entities/SavedMessage";
import { TagsPluginType } from "../types"; import { TagsPluginType } from "../types";
import { noop } from "../../../utils";
export async function onMessageDelete(pluginData: GuildPluginData<TagsPluginType>, msg: SavedMessage) { export const onMessageDelete = typedGuildEventListener({
// Command message was deleted -> delete the response as well event: "messageDelete",
const commandMsgResponse = await pluginData.state.tags.findResponseByCommandMessageId(msg.id); async listener({ pluginData, args: { message: msg } }) {
if (commandMsgResponse) { // Command message was deleted -> delete the response as well
const channel = pluginData.guild.channels.cache.get(msg.channel_id as Snowflake) as TextChannel; const commandMsgResponse = await pluginData.state.tags.findResponseByCommandMessageId(msg.id);
if (!channel) return; if (commandMsgResponse) {
const channel = pluginData.guild.channels.cache.get(msg.channelId) as TextChannel;
if (!channel) return;
const responseMsg = await pluginData.state.savedMessages.find(commandMsgResponse.response_message_id); await pluginData.state.tags.deleteResponseByCommandMessageId(msg.id);
if (!responseMsg || responseMsg.deleted_at != null) return; await channel.messages.delete(commandMsgResponse.response_message_id).catch(noop);
return;
}
await channel.messages.delete(commandMsgResponse.response_message_id as Snowflake); // Response was deleted -> delete the command message as well
return; const responseMsgResponse = await pluginData.state.tags.findResponseByResponseMessageId(msg.id);
} if (responseMsgResponse) {
const channel = pluginData.guild.channels.cache.get(msg.channelId) as TextChannel;
if (!channel) return;
// Response was deleted -> delete the command message as well await pluginData.state.tags.deleteResponseByResponseMessageId(msg.id);
const responseMsgResponse = await pluginData.state.tags.findResponseByResponseMessageId(msg.id); await channel.messages.delete(responseMsgResponse.command_message_id).catch(noop);
if (responseMsgResponse) { return;
const channel = pluginData.guild.channels.cache.get(msg.channel_id as Snowflake) as TextChannel; }
if (!channel) return; },
});
const commandMsg = await pluginData.state.savedMessages.find(responseMsgResponse.command_message_id);
if (!commandMsg || commandMsg.deleted_at != null) return;
await channel.messages.delete(responseMsgResponse.command_message_id as Snowflake);
return;
}
}