Refactor firstmessage command to use new registration system

This commit is contained in:
kmmiio99o 2025-09-27 23:25:01 +02:00
parent f058a2f7ba
commit e42628ed02
2 changed files with 100 additions and 110 deletions

View file

@ -8,6 +8,7 @@ import {
import { pluginListCommand, themeListCommand } from "./src/commands/lists";
import { petPetCommand } from "./src/commands/petpet";
import { konoSelfCommand, konoSendCommand } from "./src/commands/konochan";
import { firstMessageCommand } from "./src/commands/firstmessage";
import settings from "./settings";
// Initialize storage with default values
@ -35,6 +36,7 @@ if (!storage.enabledCommands) {
themeList: true,
konoself: true,
konosend: true,
firstmessage: true,
};
}
@ -48,6 +50,7 @@ const commandMap = {
themeList: themeListCommand,
konoself: konoSelfCommand,
konosend: konoSendCommand,
firstmessage: firstMessageCommand,
};
// Store registered commands for cleanup

View file

@ -1,7 +1,5 @@
import { registerCommand } from "@vendetta/commands";
import { findByProps } from "@vendetta/metro";
import { url } from "@vendetta/metro/common";
import { storage } from "@vendetta/plugin";
const messageUtil = findByProps(
"sendBotMessage",
@ -9,114 +7,6 @@ const messageUtil = findByProps(
"receiveMessage",
);
const APIUtils = findByProps("getAPIBaseURL", "get");
let commands = [];
export const onLoad = () => {
if (!storage?.enabledCommands?.firstmessage) return;
commands.push(
registerCommand({
name: "firstmessage",
displayName: "firstmessage",
description: "Tired of scrolling to first message?",
displayDescription: "Tired of scrolling to first message?",
type: 1,
inputType: 1,
applicationId: "-1",
options: [
{
name: "user",
displayName: "user",
description:
"Target user to get their first message in this server/dm",
displayDescription:
"Target user to get their first message in this server/dm",
type: 6,
required: false,
},
{
name: "channel",
displayName: "channel",
description: "Target channel to get first message of",
displayDescription: "Target channel to get first message of",
type: 7,
required: false,
},
{
name: "send",
displayName: "send",
description: "Whether to send the resulting url",
displayDescription: "Whether to send the resulting url",
type: 5,
required: false,
},
],
async execute(args, ctx) {
const user = args.find((o: any) => o.name === "user")?.value;
const channel = args.find((o: any) => o.name === "channel")?.value;
const send = args.find((o: any) => o.name === "send")?.value;
const guildId = ctx.guild?.id;
const channelId = ctx.channel.id;
const isDM = ctx.channel.type === 1;
let result = "https://discord.com/channels/";
try {
// Handle DM error cases first
if (isDM && (channel || (user && channel))) {
messageUtil.sendBotMessage(
channelId,
"This combination cannot be used in DMs!",
);
return { type: 4 };
}
if (!user && !channel) {
if (isDM) {
const message = await getFirstDMMessage(channelId);
result += `@me/${channelId}/${message.id}`;
} else {
const message = await getFirstGuildMessage(guildId);
result += `${guildId}/${message.channel_id}/${message.id}`;
}
} else if (user) {
if (isDM) {
const message = await getFirstDMMessage(channelId, user);
result += `@me/${channelId}/${message.id}`;
} else {
const message = await getFirstGuildMessage(guildId, user);
result += `${guildId}/${message.channel_id}/${message.id}`;
}
} else if (channel) {
const message = await getFirstGuildMessage(guildId, null, channel);
result += `${guildId}/${channel}/${message.id}`;
} else {
const message = await getFirstGuildMessage(guildId, user, channel);
result += `${guildId}/${channel}/${message.id}`;
}
if (send) {
messageUtil.sendBotMessage(channelId, result);
return { type: 4 };
} else {
return url.openDeeplink(result);
}
} catch (error) {
messageUtil.sendBotMessage(
channelId,
"Failed to fetch the first message. Please try again.",
);
return { type: 4 };
}
},
}),
);
};
export const onUnload = () => {
for (const unregisterCommands of commands) unregisterCommands();
};
const getFirstGuildMessage = async (
guildId: number,
@ -144,3 +34,100 @@ const getFirstDMMessage = async (dmId: number, userId?: number) => {
})
).body.messages[0][0];
};
export const firstMessageCommand = {
name: "firstmessage",
displayName: "firstmessage",
description: "Tired of scrolling to first message?",
displayDescription: "Tired of scrolling to first message?",
options: [
{
name: "user",
displayName: "user",
description: "Target user to get their first message in this server/dm",
displayDescription:
"Target user to get their first message in this server/dm",
type: 6,
required: false,
},
{
name: "channel",
displayName: "channel",
description: "Target channel to get first message of",
displayDescription: "Target channel to get first message of",
type: 7,
required: false,
},
{
name: "send",
displayName: "send",
description: "Whether to send the resulting url",
displayDescription: "Whether to send the resulting url",
type: 5,
required: false,
},
],
applicationId: "-1",
inputType: 1,
type: 1,
execute: async (args: any, ctx: any) => {
const options = new Map(args.map((option: any) => [option.name, option]));
const user = options.get("user")?.value;
const channel = options.get("channel")?.value;
const send = options.get("send")?.value;
const guildId = ctx.guild?.id;
const channelId = ctx.channel.id;
const isDM = ctx.channel.type === 1;
try {
// Handle DM error cases first
if (isDM && (channel || (user && channel))) {
messageUtil.sendBotMessage(
channelId,
"This combination cannot be used in DMs!",
);
return { type: 4 };
}
let result = "https://discord.com/channels/";
if (!user && !channel) {
if (isDM) {
const message = await getFirstDMMessage(channelId);
result += `@me/${channelId}/${message.id}`;
} else {
const message = await getFirstGuildMessage(guildId);
result += `${guildId}/${message.channel_id}/${message.id}`;
}
} else if (user) {
if (isDM) {
const message = await getFirstDMMessage(channelId, user);
result += `@me/${channelId}/${message.id}`;
} else {
const message = await getFirstGuildMessage(guildId, user);
result += `${guildId}/${message.channel_id}/${message.id}`;
}
} else if (channel) {
const message = await getFirstGuildMessage(guildId, null, channel);
result += `${guildId}/${channel}/${message.id}`;
} else {
const message = await getFirstGuildMessage(guildId, user, channel);
result += `${guildId}/${channel}/${message.id}`;
}
if (send) {
messageUtil.sendBotMessage(channelId, result);
return { type: 4 };
} else {
return url.openDeeplink(result);
}
} catch (error) {
messageUtil.sendBotMessage(
channelId,
"Failed to fetch the first message. Please try again.",
);
return { type: 4 };
}
},
};