revert the changes that migrate datastore to array settings v2

This commit is contained in:
Elvyra 2025-01-19 19:43:33 +01:00
parent 6ddf203609
commit bc843c3625
2 changed files with 20 additions and 34 deletions

View file

@ -18,7 +18,7 @@
import { ApplicationCommandInputType, ApplicationCommandOptionType, findOption, registerCommand, sendBotMessage, unregisterCommand } from "@api/Commands"; import { ApplicationCommandInputType, ApplicationCommandOptionType, findOption, registerCommand, sendBotMessage, unregisterCommand } from "@api/Commands";
import * as DataStore from "@api/DataStore"; import * as DataStore from "@api/DataStore";
import { definePluginSettings, Settings } from "@api/Settings"; import { Settings } from "@api/Settings";
import { Devs } from "@utils/constants"; import { Devs } from "@utils/constants";
import definePlugin, { OptionType } from "@utils/types"; import definePlugin, { OptionType } from "@utils/types";
@ -32,12 +32,19 @@ interface Tag {
enabled: boolean; enabled: boolean;
} }
const getTag = (name: string) => settings.store.data.find((tt: Tag) => tt.name === name); const getTags = () => DataStore.get(DATA_KEY).then<Tag[]>(t => t ?? []);
const addTag = (tag: Tag) => { const getTag = (name: string) => DataStore.get(DATA_KEY).then<Tag | null>((t: Tag[]) => (t ?? []).find((tt: Tag) => tt.name === name) ?? null);
settings.store.data.push(tag); const addTag = async (tag: Tag) => {
const tags = await getTags();
tags.push(tag);
DataStore.set(DATA_KEY, tags);
return tags;
}; };
const removeTag = (name: string) => { const removeTag = async (name: string) => {
settings.store.data.filter((t: Tag) => t.name !== name); let tags = await getTags();
tags = await tags.filter((t: Tag) => t.name !== name);
DataStore.set(DATA_KEY, tags);
return tags;
}; };
function createTagCommand(tag: Tag) { function createTagCommand(tag: Tag) {
@ -46,7 +53,7 @@ function createTagCommand(tag: Tag) {
description: tag.name, description: tag.name,
inputType: ApplicationCommandInputType.BUILT_IN_TEXT, inputType: ApplicationCommandInputType.BUILT_IN_TEXT,
execute: async (_, ctx) => { execute: async (_, ctx) => {
if (!getTag(tag.name)) { if (!await getTag(tag.name)) {
sendBotMessage(ctx.channel.id, { sendBotMessage(ctx.channel.id, {
content: `${EMOTE} The tag **${tag.name}** does not exist anymore! Please reload ur Discord to fix :)` content: `${EMOTE} The tag **${tag.name}** does not exist anymore! Please reload ur Discord to fix :)`
}); });
@ -63,21 +70,6 @@ function createTagCommand(tag: Tag) {
} }
const settings = definePluginSettings({
data: {
type: OptionType.ARRAY,
hidden: true,
description: ""
},
migrated: {
type: OptionType.BOOLEAN,
description: "",
default: false,
hidden: true
}
});
export default definePlugin({ export default definePlugin({
name: "MessageTags", name: "MessageTags",
description: "Allows you to save messages and to use them with a simple command.", description: "Allows you to save messages and to use them with a simple command.",
@ -90,16 +82,9 @@ export default definePlugin({
default: true default: true
} }
}, },
settings,
async start() { async start() {
if (!settings.store.migrated) { for (const tag of await getTags()) createTagCommand(tag);
const data = await DataStore.get(DATA_KEY);
if (!!data) settings.store.data = data;
settings.store.migrated = true;
}
for (const tag of settings.store.data) createTagCommand(tag);
}, },
commands: [ commands: [
@ -180,7 +165,7 @@ export default definePlugin({
}; };
createTagCommand(tag); createTagCommand(tag);
addTag(tag); await addTag(tag);
sendBotMessage(ctx.channel.id, { sendBotMessage(ctx.channel.id, {
content: `${EMOTE} Successfully created the tag **${name}**!` content: `${EMOTE} Successfully created the tag **${name}**!`
@ -196,7 +181,7 @@ export default definePlugin({
}); });
unregisterCommand(name); unregisterCommand(name);
removeTag(name); await removeTag(name);
sendBotMessage(ctx.channel.id, { sendBotMessage(ctx.channel.id, {
content: `${EMOTE} Successfully deleted the tag **${name}**!` content: `${EMOTE} Successfully deleted the tag **${name}**!`
@ -210,7 +195,7 @@ export default definePlugin({
// @ts-ignore // @ts-ignore
title: "All Tags:", title: "All Tags:",
// @ts-ignore // @ts-ignore
description: settings.store.data description: (await getTags())
.map(tag => `\`${tag.name}\`: ${tag.message.slice(0, 72).replaceAll("\\n", " ")}${tag.message.length > 72 ? "..." : ""}`) .map(tag => `\`${tag.name}\`: ${tag.message.slice(0, 72).replaceAll("\\n", " ")}${tag.message.length > 72 ? "..." : ""}`)
.join("\n") || `${EMOTE} Woops! There are no tags yet, use \`/tags create\` to create one!`, .join("\n") || `${EMOTE} Woops! There are no tags yet, use \`/tags create\` to create one!`,
// @ts-ignore // @ts-ignore
@ -223,7 +208,7 @@ export default definePlugin({
} }
case "preview": { case "preview": {
const name: string = findOption(args[0].options, "tag-name", ""); const name: string = findOption(args[0].options, "tag-name", "");
const tag = getTag(name); const tag = await getTag(name);
if (!tag) if (!tag)
return sendBotMessage(ctx.channel.id, { return sendBotMessage(ctx.channel.id, {

View file

@ -49,6 +49,7 @@ export const settings = definePluginSettings({
], ],
onChange: () => forceUpdate() onChange: () => forceUpdate()
}, },
dmSectioncollapsed: { dmSectioncollapsed: {
type: OptionType.BOOLEAN, type: OptionType.BOOLEAN,
description: "Collapse DM sections", description: "Collapse DM sections",