From bcac18d40656d55b4ff84769d538467063aeb37d Mon Sep 17 00:00:00 2001 From: Yoru Date: Sun, 14 Apr 2024 17:56:07 +0300 Subject: [PATCH] Fix inconsistencies in types (#175) * performance improvement on handler * fix types --- src/commands/general/config.ts | 7 +++---- src/commands/general/ping.ts | 3 +-- src/commands/general/prefix.ts | 7 +++---- src/commands/help/help.ts | 3 +-- src/commands/osu/avatar.ts | 8 +++----- src/commands/osu/background.ts | 8 +++----- src/commands/osu/banner.ts | 8 +++----- src/commands/osu/compare.ts | 8 +++----- src/commands/osu/leaderboard.ts | 8 +++----- src/commands/osu/link.ts | 8 +++----- src/commands/osu/map.ts | 8 +++----- src/commands/osu/profile.ts | 8 +++----- src/commands/osu/recent.ts | 8 +++----- src/commands/osu/recentbest.ts | 8 +++----- src/commands/osu/recentlist.ts | 8 +++----- src/commands/osu/top.ts | 8 +++----- src/commands/osu/unlink.ts | 8 +++----- src/listeners/interactionCreate.ts | 3 ++- src/types/commands.ts | 6 ++++-- src/utils/args.ts | 24 +++++++++++------------- 20 files changed, 64 insertions(+), 93 deletions(-) diff --git a/src/commands/general/config.ts b/src/commands/general/config.ts index 80f66aba..5b1b7ba1 100644 --- a/src/commands/general/config.ts +++ b/src/commands/general/config.ts @@ -1,12 +1,11 @@ import { getUser, insertData } from "@utils/database"; import { ScoreEmbed } from "@type/database"; import { ApplicationCommandOptionType } from "lilybird"; -import type { ApplicationCommandData, GuildInteraction, Interaction } from "@lilybird/transformers"; +import type { ApplicationCommandData, GuildInteraction } from "@lilybird/transformers"; import type { EmbedStructure } from "lilybird"; -import type { SlashCommand } from "@lilybird/handlers"; +import type { SlashCommand } from "@type/commands"; export default { - post: "GLOBAL", data: { name: "config", description: "Set your account configurations", @@ -37,7 +36,7 @@ export default { run } satisfies SlashCommand; -async function run(interaction: Interaction): Promise { +async function run(interaction: GuildInteraction): Promise { await interaction.deferReply(); if (!interaction.inGuild()) return; diff --git a/src/commands/general/ping.ts b/src/commands/general/ping.ts index 7f94da40..ec031517 100644 --- a/src/commands/general/ping.ts +++ b/src/commands/general/ping.ts @@ -1,8 +1,7 @@ import { client } from "@utils/initalize"; -import type { SlashCommand } from "@lilybird/handlers"; +import type { SlashCommand } from "@type/commands"; export default { - post: "GLOBAL", data: { name: "ping", description: "pong!!" }, run: async (interaction) => { await interaction.deferReply(); diff --git a/src/commands/general/prefix.ts b/src/commands/general/prefix.ts index 9dc17d2c..c8950e37 100644 --- a/src/commands/general/prefix.ts +++ b/src/commands/general/prefix.ts @@ -2,8 +2,8 @@ import { getServer, insertData } from "@utils/database"; import { DEFAULT_PREFIX, MAX_AMOUNT_OF_PREFIXES } from "@utils/constants"; import { prefixesCache } from "@listeners/guildCreate"; import { ApplicationCommandOptionType, EmbedType, PermissionFlags } from "lilybird"; -import type { ApplicationCommandData, GuildInteraction, Interaction } from "@lilybird/transformers"; -import type { SlashCommand } from "@lilybird/handlers"; +import type { ApplicationCommandData, GuildInteraction } from "@lilybird/transformers"; +import type { SlashCommand } from "@type/commands"; const commands: Record, guildId: string }) => Promise> = { add, @@ -21,7 +21,6 @@ const PERMISSIONS_NEEDED = PermissionNames[Number(PERMISSIONS_NEEDED_INT)]; const PERMISSION_NEEDED_STRING = `Looks like you don't have the necessary permissions for this command. Permission(s) needed: \`${PERMISSIONS_NEEDED}\``; export default { - post: "GLOBAL", data: { name: "prefix", description: "Set, remove and list the bot's prefixes", @@ -48,7 +47,7 @@ export default { run } satisfies SlashCommand; -async function run(interaction: Interaction): Promise { +async function run(interaction: GuildInteraction): Promise { await interaction.deferReply(); if (!interaction.inGuild()) return; diff --git a/src/commands/help/help.ts b/src/commands/help/help.ts index 4c1aba8a..90fd0baa 100644 --- a/src/commands/help/help.ts +++ b/src/commands/help/help.ts @@ -1,8 +1,7 @@ import { helpBuilder } from "@builders/help"; -import type { SlashCommand } from "@lilybird/handlers"; +import type { SlashCommand } from "@type/commands"; export default { - post: "GLOBAL", data: { name: "help", description: "Get info about the bot." }, run: async (interaction) => { await interaction.deferReply(); diff --git a/src/commands/osu/avatar.ts b/src/commands/osu/avatar.ts index fc0f809c..4ed9cc0e 100644 --- a/src/commands/osu/avatar.ts +++ b/src/commands/osu/avatar.ts @@ -4,11 +4,10 @@ import { UserType } from "@type/commandArgs"; import { EmbedBuilderType } from "@type/embedBuilders"; import { avatarBuilder } from "@builders/avatar"; import { ApplicationCommandOptionType, EmbedType } from "lilybird"; -import type { ApplicationCommandData, Interaction } from "@lilybird/transformers"; -import type { SlashCommand } from "@lilybird/handlers"; +import type { ApplicationCommandData, GuildInteraction } from "@lilybird/transformers"; +import type { SlashCommand } from "@type/commands"; export default { - post: "GLOBAL", data: { name: "avatar", description: "Display the avatar of a user.", @@ -28,8 +27,7 @@ export default { run } satisfies SlashCommand; -async function run(interaction: Interaction): Promise { - if (!interaction.inGuild()) return; +async function run(interaction: GuildInteraction): Promise { await interaction.deferReply(); const args = getCommandArgs(interaction); diff --git a/src/commands/osu/background.ts b/src/commands/osu/background.ts index abb7d920..62f18f37 100644 --- a/src/commands/osu/background.ts +++ b/src/commands/osu/background.ts @@ -4,11 +4,10 @@ import { EmbedBuilderType } from "@type/embedBuilders"; import { backgroundBuilder } from "@builders/background"; import { client } from "@utils/initalize"; import { ApplicationCommandOptionType, EmbedType } from "lilybird"; -import type { ApplicationCommandData, Interaction } from "@lilybird/transformers"; -import type { SlashCommand } from "@lilybird/handlers"; +import type { ApplicationCommandData, GuildInteraction } from "@lilybird/transformers"; +import type { SlashCommand } from "@type/commands"; export default { - post: "GLOBAL", data: { name: "background", description: "Display background of a beatmap.", @@ -23,8 +22,7 @@ export default { run } satisfies SlashCommand; -async function run(interaction: Interaction): Promise { - if (!interaction.inGuild()) return; +async function run(interaction: GuildInteraction): Promise { await interaction.deferReply(); const args = getCommandArgs(interaction); diff --git a/src/commands/osu/banner.ts b/src/commands/osu/banner.ts index 8252c170..8dc363a7 100644 --- a/src/commands/osu/banner.ts +++ b/src/commands/osu/banner.ts @@ -4,11 +4,10 @@ import { client } from "@utils/initalize"; import { UserType } from "@type/commandArgs"; import { EmbedBuilderType } from "@type/embedBuilders"; import { ApplicationCommandOptionType, EmbedType } from "lilybird"; -import type { ApplicationCommandData, Interaction } from "@lilybird/transformers"; -import type { SlashCommand } from "@lilybird/handlers"; +import type { ApplicationCommandData, GuildInteraction } from "@lilybird/transformers"; +import type { SlashCommand } from "@type/commands"; export default { - post: "GLOBAL", data: { name: "banner", description: "Display the banner of a user.", @@ -28,8 +27,7 @@ export default { run } satisfies SlashCommand; -async function run(interaction: Interaction): Promise { - if (!interaction.inGuild()) return; +async function run(interaction: GuildInteraction): Promise { await interaction.deferReply(); const args = getCommandArgs(interaction); diff --git a/src/commands/osu/compare.ts b/src/commands/osu/compare.ts index eb92e9fa..037dc28b 100644 --- a/src/commands/osu/compare.ts +++ b/src/commands/osu/compare.ts @@ -5,11 +5,10 @@ import { compareBuilder } from "@builders/compare"; import { getBeatmapIdFromContext } from "@utils/osu"; import { EmbedBuilderType } from "@type/embedBuilders"; import { ApplicationCommandOptionType, EmbedType } from "lilybird"; -import type { ApplicationCommandData, Interaction } from "@lilybird/transformers"; -import type { SlashCommand } from "@lilybird/handlers"; +import type { ApplicationCommandData, GuildInteraction } from "@lilybird/transformers"; +import type { SlashCommand } from "@type/commands"; export default { - post: "GLOBAL", data: { name: "compare", description: "Display play(s) of a user on a beatmap.", @@ -71,8 +70,7 @@ export default { run } satisfies SlashCommand; -async function run(interaction: Interaction): Promise { - if (!interaction.inGuild()) return; +async function run(interaction: GuildInteraction): Promise { await interaction.deferReply(); const args = getCommandArgs(interaction); diff --git a/src/commands/osu/leaderboard.ts b/src/commands/osu/leaderboard.ts index 89854a35..1f7acbcd 100644 --- a/src/commands/osu/leaderboard.ts +++ b/src/commands/osu/leaderboard.ts @@ -8,11 +8,10 @@ import { mesageDataForButtons } from "@utils/cache"; import { ApplicationCommandOptionType, EmbedType } from "lilybird"; import type { LeaderboardBuilderOptions } from "@type/embedBuilders"; import type { Mod } from "osu-web.js"; -import type { ApplicationCommandData, Interaction } from "@lilybird/transformers"; -import type { SlashCommand } from "@lilybird/handlers"; +import type { ApplicationCommandData, GuildInteraction } from "@lilybird/transformers"; +import type { SlashCommand } from "@type/commands"; export default { - post: "GLOBAL", data: { name: "leaderboard", description: "Display the leaderboard of a beatmap", @@ -44,8 +43,7 @@ export default { run } satisfies SlashCommand; -async function run(interaction: Interaction): Promise { - if (!interaction.inGuild()) return; +async function run(interaction: GuildInteraction): Promise { await interaction.deferReply(); const args = getCommandArgs(interaction); diff --git a/src/commands/osu/link.ts b/src/commands/osu/link.ts index bcf57e80..9823bf28 100644 --- a/src/commands/osu/link.ts +++ b/src/commands/osu/link.ts @@ -1,17 +1,15 @@ import { encrypt } from "../.."; import { buildAuthUrl } from "@utils/osu"; import type { AuthScope } from "@type/osu"; -import type { SlashCommand } from "@lilybird/handlers"; -import type { ApplicationCommandData, Interaction } from "@lilybird/transformers"; +import type { SlashCommand } from "@type/commands"; +import type { ApplicationCommandData, GuildInteraction } from "@lilybird/transformers"; export default { - post: "GLOBAL", data: { name: "link", description: "Link your osu! account to the bot." }, run } satisfies SlashCommand; -async function run(interaction: Interaction): Promise { - if (!interaction.inGuild()) return; +async function run(interaction: GuildInteraction): Promise { await interaction.deferReply(true); const encryptedDiscordId = `${encrypt(interaction.member.user.id)}`; diff --git a/src/commands/osu/map.ts b/src/commands/osu/map.ts index b49b81bf..fbd13920 100644 --- a/src/commands/osu/map.ts +++ b/src/commands/osu/map.ts @@ -4,11 +4,10 @@ import { mapBuilder } from "@builders/map"; import { EmbedBuilderType } from "@type/embedBuilders"; import { ApplicationCommandOptionType, EmbedType } from "lilybird"; import type { Mod } from "osu-web.js"; -import type { ApplicationCommandData, Interaction } from "@lilybird/transformers"; -import type { SlashCommand } from "@lilybird/handlers"; +import type { ApplicationCommandData, GuildInteraction } from "@lilybird/transformers"; +import type { SlashCommand } from "@type/commands"; export default { - post: "GLOBAL", data: { name: "map", description: "Display statistics of a beatmap.", @@ -29,8 +28,7 @@ export default { run } satisfies SlashCommand; -async function run(interaction: Interaction): Promise { - if (!interaction.inGuild()) return; +async function run(interaction: GuildInteraction): Promise { await interaction.deferReply(); const args = getCommandArgs(interaction); diff --git a/src/commands/osu/profile.ts b/src/commands/osu/profile.ts index fdc7d78d..cff6d06c 100644 --- a/src/commands/osu/profile.ts +++ b/src/commands/osu/profile.ts @@ -4,11 +4,10 @@ import { client } from "@utils/initalize"; import { UserType } from "@type/commandArgs"; import { EmbedBuilderType } from "@type/embedBuilders"; import { ApplicationCommandOptionType, EmbedType } from "lilybird"; -import type { ApplicationCommandData, Interaction } from "@lilybird/transformers"; -import type { SlashCommand } from "@lilybird/handlers"; +import type { ApplicationCommandData, GuildInteraction } from "@lilybird/transformers"; +import type { SlashCommand } from "@type/commands"; export default { - post: "GLOBAL", data: { name: "profile", description: "Display statistics of a user.", @@ -34,8 +33,7 @@ export default { run } satisfies SlashCommand; -async function run(interaction: Interaction): Promise { - if (!interaction.inGuild()) return; +async function run(interaction: GuildInteraction): Promise { await interaction.deferReply(); const args = getCommandArgs(interaction); diff --git a/src/commands/osu/recent.ts b/src/commands/osu/recent.ts index ae589bca..d2d749b0 100644 --- a/src/commands/osu/recent.ts +++ b/src/commands/osu/recent.ts @@ -9,11 +9,10 @@ import { mesageDataForButtons } from "@utils/cache"; import { ApplicationCommandOptionType, EmbedType } from "lilybird"; import type { PlaysBuilderOptions } from "@type/embedBuilders"; import type { Mod } from "osu-web.js"; -import type { ApplicationCommandData, Interaction } from "@lilybird/transformers"; -import type { SlashCommand } from "@lilybird/handlers"; +import type { ApplicationCommandData, GuildInteraction } from "@lilybird/transformers"; +import type { SlashCommand } from "@type/commands"; export default { - post: "GLOBAL", data: { name: "recent", description: "Display recent play(s) of a user.", @@ -82,8 +81,7 @@ export default { run } satisfies SlashCommand; -async function run(interaction: Interaction): Promise { - if (!interaction.inGuild()) return; +async function run(interaction: GuildInteraction): Promise { await interaction.deferReply(); const args = getCommandArgs(interaction); diff --git a/src/commands/osu/recentbest.ts b/src/commands/osu/recentbest.ts index b117d9be..af8c68bc 100644 --- a/src/commands/osu/recentbest.ts +++ b/src/commands/osu/recentbest.ts @@ -9,11 +9,10 @@ import { mesageDataForButtons } from "@utils/cache"; import { ApplicationCommandOptionType, EmbedType } from "lilybird"; import type { PlaysBuilderOptions } from "@type/embedBuilders"; import type { Mod } from "osu-web.js"; -import type { ApplicationCommandData, Interaction } from "@lilybird/transformers"; -import type { SlashCommand } from "@lilybird/handlers"; +import type { ApplicationCommandData, GuildInteraction } from "@lilybird/transformers"; +import type { SlashCommand } from "@type/commands"; export default { - post: "GLOBAL", data: { name: "recentbest", description: "Display most recent top play(s) of a user.", @@ -84,8 +83,7 @@ export default { run } satisfies SlashCommand; -async function run(interaction: Interaction): Promise { - if (!interaction.inGuild()) return; +async function run(interaction: GuildInteraction): Promise { await interaction.deferReply(); const args = getCommandArgs(interaction); diff --git a/src/commands/osu/recentlist.ts b/src/commands/osu/recentlist.ts index bf211fda..c5b6d1a4 100644 --- a/src/commands/osu/recentlist.ts +++ b/src/commands/osu/recentlist.ts @@ -9,11 +9,10 @@ import { mesageDataForButtons } from "@utils/cache"; import { ApplicationCommandOptionType, EmbedType } from "lilybird"; import type { PlaysBuilderOptions } from "@type/embedBuilders"; import type { Mod } from "osu-web.js"; -import type { ApplicationCommandData, Interaction } from "@lilybird/transformers"; -import type { SlashCommand } from "@lilybird/handlers"; +import type { ApplicationCommandData, GuildInteraction } from "@lilybird/transformers"; +import type { SlashCommand } from "@type/commands"; export default { - post: "GLOBAL", data: { name: "recentlist", description: "Display a list of recent play(s) of a user.", @@ -89,8 +88,7 @@ export default { run } satisfies SlashCommand; -async function run(interaction: Interaction): Promise { - if (!interaction.inGuild()) return; +async function run(interaction: GuildInteraction): Promise { await interaction.deferReply(); const args = getCommandArgs(interaction); diff --git a/src/commands/osu/top.ts b/src/commands/osu/top.ts index d9ec0a1d..5eac4e58 100644 --- a/src/commands/osu/top.ts +++ b/src/commands/osu/top.ts @@ -9,11 +9,10 @@ import { mesageDataForButtons } from "@utils/cache"; import { ApplicationCommandOptionType, EmbedType } from "lilybird"; import type { PlaysBuilderOptions } from "@type/embedBuilders"; import type { Mod } from "osu-web.js"; -import type { ApplicationCommandData, Interaction } from "@lilybird/transformers"; -import type { SlashCommand } from "@lilybird/handlers"; +import type { ApplicationCommandData, GuildInteraction } from "@lilybird/transformers"; +import type { SlashCommand } from "@type/commands"; export default { - post: "GLOBAL", data: { name: "top", description: "Display top play(s) of a user.", @@ -84,8 +83,7 @@ export default { run } satisfies SlashCommand; -async function run(interaction: Interaction): Promise { - if (!interaction.inGuild()) return; +async function run(interaction: GuildInteraction): Promise { await interaction.deferReply(); const args = getCommandArgs(interaction); diff --git a/src/commands/osu/unlink.ts b/src/commands/osu/unlink.ts index b08bbcb7..07922dd7 100644 --- a/src/commands/osu/unlink.ts +++ b/src/commands/osu/unlink.ts @@ -1,16 +1,14 @@ import { getUser, removeUser } from "@utils/database"; import { slashCommandsIds } from "@utils/cache"; -import type { SlashCommand } from "@lilybird/handlers"; -import type { ApplicationCommandData, Interaction } from "@lilybird/transformers"; +import type { SlashCommand } from "@type/commands"; +import type { ApplicationCommandData, GuildInteraction } from "@lilybird/transformers"; export default { - post: "GLOBAL", data: { name: "unlink", description: "Unlink your osu! account from the bot." }, run } satisfies SlashCommand; -async function run(interaction: Interaction): Promise { - if (!interaction.inGuild()) return; +async function run(interaction: GuildInteraction): Promise { await interaction.deferReply(true); const linkCommand = slashCommandsIds.get("link"); diff --git a/src/listeners/interactionCreate.ts b/src/listeners/interactionCreate.ts index 08e7c413..bdff9ce5 100644 --- a/src/listeners/interactionCreate.ts +++ b/src/listeners/interactionCreate.ts @@ -18,7 +18,6 @@ export default { async function run(interaction: Interaction): Promise { await handleButton(interaction); if (interaction.isApplicationCommandInteraction() && interaction.inGuild()) { - const server = await interaction.client.rest.getGuild(interaction.guildId); const { username } = interaction.member.user; const commandDefault = applicationCommands.get(interaction.data.name); @@ -27,6 +26,7 @@ async function run(interaction: Interaction): Promise { try { await command.run(interaction); + const server = await interaction.client.rest.getGuild(interaction.guildId); await loadLogs(`INFO: [${server.name}] ${username} used slash command \`${command.data.name}\`${interaction.data.subCommand ? ` -> \`${interaction.data.subCommand}\`` : ""}`); const docs = getCommand(interaction.data.name); @@ -35,6 +35,7 @@ async function run(interaction: Interaction): Promise { else insertData({ table: "commands_slash", data: [ { name: "count", value: Number(docs.count ?? 0) + 1 } ], id: docs.id }); } catch (error) { + const server = await interaction.client.rest.getGuild(interaction.guildId); const err = error as Error; console.log(error); await loadLogs( diff --git a/src/types/commands.ts b/src/types/commands.ts index ecaee74c..b35f7cbc 100644 --- a/src/types/commands.ts +++ b/src/types/commands.ts @@ -1,4 +1,4 @@ -import type { ApplicationCommandData, GuildTextChannel, Interaction, Message } from "@lilybird/transformers"; +import type { ApplicationCommandData, GuildInteraction, GuildTextChannel, Message } from "@lilybird/transformers"; import type { Client, POSTApplicationCommandStructure } from "lilybird"; export interface MessageCommand { @@ -12,9 +12,11 @@ export interface MessageCommand { { client: Client, message: Message, args: Array, prefix: string, index: number | undefined, commandName: string, channel: GuildTextChannel }) => Promise; } +type Awaitable = Promise | T; + export interface SlashCommand { data: POSTApplicationCommandStructure; - run: (interaction: Interaction) => Promise; + run: (interaction: GuildInteraction) => Awaitable; } export interface DefaultSlashCommand { diff --git a/src/utils/args.ts b/src/utils/args.ts index c7ba7a9d..3151abdf 100644 --- a/src/utils/args.ts +++ b/src/utils/args.ts @@ -5,7 +5,7 @@ import { UserType } from "@type/commandArgs"; import { ModsEnum } from "osu-web.js"; import type { CommandArgs, Mods, ParsedArgs, User } from "@type/commandArgs"; import type { Mod } from "osu-web.js"; -import type { ApplicationCommandData, Interaction, Message } from "@lilybird/transformers"; +import type { ApplicationCommandData, GuildInteraction, Message } from "@lilybird/transformers"; interface BeatMapSetURL { url: string; @@ -71,11 +71,9 @@ function linkCommand(): string | undefined { return slashCommandsIds.get("link"); } -export function getCommandArgs(interaction: Interaction): CommandArgs | undefined { - if (!interaction.isApplicationCommandInteraction() || !interaction.inGuild()) return; - - const userArg = interaction.data.getString("username"); - const userAuthor = getUser(interaction.member.user.id); +export function getCommandArgs(interaction: GuildInteraction): CommandArgs | undefined { + const username = interaction.data.getString("username"); + const author = getUser(interaction.member.user.id); const discordUserId = interaction.data.getUser("discord"); const discordUser = getUser(discordUserId ?? ""); const mode = interaction.data.getString("mode") ?? Mode.OSU; @@ -107,18 +105,18 @@ export function getCommandArgs(interaction: Interaction) const user: User = discordUserId ? discordUser?.banchoId - ? { type: UserType.SUCCESS, banchoId: discordUser.banchoId, authorDb: userAuthor, mode, beatmapId } + ? { type: UserType.SUCCESS, banchoId: discordUser.banchoId, authorDb: author, mode, beatmapId } : { type: UserType.FAIL, beatmapId, - authorDb: userAuthor, + authorDb: author, failMessage: discordUserId ? `The user <@${discordUserId}> hasn't linked their account to the bot yet!` : `Please link your account to the bot using ${linkCommand()}!` } - : userArg - ? { type: UserType.SUCCESS, banchoId: userArg, mode, beatmapId, authorDb: userAuthor } - : userAuthor?.banchoId - ? { type: UserType.SUCCESS, banchoId: userAuthor.banchoId, mode, beatmapId, authorDb: userAuthor } - : { type: UserType.FAIL, beatmapId, authorDb: userAuthor, failMessage: "Please link your account to the bot using /link!" }; + : username + ? { type: UserType.SUCCESS, banchoId: username, mode, beatmapId, authorDb: author } + : author?.banchoId + ? { type: UserType.SUCCESS, banchoId: author.banchoId, mode, beatmapId, authorDb: author } + : { type: UserType.FAIL, beatmapId, authorDb: author, failMessage: "Please link your account to the bot using /link!" }; return { user, mods }; }