From ac8636a6e76570a5e90e7e2d16ca5f6baade73d9 Mon Sep 17 00:00:00 2001 From: ToastedToast Date: Sat, 13 Jul 2024 21:54:46 +0800 Subject: [PATCH] chore: fix errors --- api/db/queries/guilds.ts | 22 ++++++++++++++++------ api/index.ts | 5 +++-- bot/commands.ts | 23 ++++++++--------------- bot/index.ts | 5 +++-- bot/utils/handleLevelChange.ts | 2 +- eslint.config.mjs | 12 ++++++------ 6 files changed, 37 insertions(+), 32 deletions(-) diff --git a/api/db/queries/guilds.ts b/api/db/queries/guilds.ts index 297fa00..c0eb8a7 100644 --- a/api/db/queries/guilds.ts +++ b/api/db/queries/guilds.ts @@ -25,22 +25,20 @@ export async function updateGuild(guild: Guild): Promise<[QueryError | null, nul return new Promise((resolve, reject) => { pool.query( ` - INSERT INTO guilds (id, name, icon, members, updates_enabled, updates_channel) + INSERT INTO guilds (id, name, icon, members, cooldown) VALUES (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE name = VALUES(name), icon = VALUES(icon), members = VALUES(members), - updates_enabled = VALUES(updates_enabled), - updates_channel = VALUES(updates_channel) + cooldown = VALUES(cooldown) `, [ guild.id, guild.name, guild.icon, guild.members, - guild.updates_enabled, - guild.updates_channel, + guild.cooldown ], (err, results) => { console.dir(results, { depth: null }); @@ -54,6 +52,18 @@ export async function updateGuild(guild: Guild): Promise<[QueryError | null, nul }); } +export async function setCooldown(guildId: string, cooldown: number): Promise<[QueryError, null] | [null, Guild]> { + return new Promise((resolve, reject) => { + pool.query("UPDATE guilds SET cooldown = ? WHERE id = ?", [cooldown, guildId], (err, results) => { + if (err) { + reject([err, null]); + } else { + resolve([null, (results as Guild[])[0]]); + } + }); + }) +} + interface BotInfo { total_guilds: number; total_members: number; @@ -93,7 +103,7 @@ export async function getUsersCount(): Promise<[QueryError | null, number]> { if (err) { reject([err, null]); } else { - resolve([null, (results[0] as { count: number }).count]); + resolve([null, (results as { count: number }[])[0].count]); } }); }); diff --git a/api/index.ts b/api/index.ts index 8c7855e..80b16ad 100644 --- a/api/index.ts +++ b/api/index.ts @@ -1,7 +1,7 @@ import express, { type NextFunction, type Request, type Response } from "express"; import cors from "cors"; import path from "path"; -import { getBotInfo, getGuild, getUser, getUsers, initTables, pool, updateGuild, getUpdates, enableUpdates, disableUpdates } from "./db"; +import { getBotInfo, getGuild, getUser, getUsers, initTables, pool, updateGuild, getUpdates, enableUpdates, disableUpdates, setCooldown } from "./db"; const app = express(); const PORT = 18103; @@ -269,7 +269,7 @@ app.post("/admin/:action/:guild/:target", authMiddleware, async (req, res) => { } case "set": try { - const data = await adminCooldownSet(guild, extraData.cooldown); + const data = await setCooldown(guild, extraData.cooldown); return res.status(200).json(data); } catch (error) { return res.status(500).json({ message: "Internal server error" }); @@ -303,6 +303,7 @@ app.get("/leaderboard/:guild", async (req, res) => { app.get("/", async (_req, res) => { // TODO: handle error + // eslint-disable-next-line @typescript-eslint/no-unused-vars const [err, botInfo] = await getBotInfo(); res.render("index", { botInfo }); }); diff --git a/bot/commands.ts b/bot/commands.ts index 795b480..01fb3d0 100644 --- a/bot/commands.ts +++ b/bot/commands.ts @@ -1,7 +1,7 @@ // Commands taken from https://github.com/NiaAxern/discord-youtube-subscriber-count/blob/main/src/commands/utilities.ts import client from '.'; -import { ActionRowBuilder, ButtonBuilder, ButtonStyle, type CommandInteraction, ChannelType } from 'discord.js'; +import { ActionRowBuilder, ButtonBuilder, ButtonStyle, type CommandInteraction, ChannelType, type APIApplicationCommandOption } from 'discord.js'; import { heapStats } from 'bun:jsc'; import { getGuildLeaderboard, makeGETRequest, getRoles, removeRole, addRole, enableUpdates, disableUpdates, getCooldown, setCooldown, checkIfGuildHasUpdatesEnabled } from './utils/requestAPI'; import convertToLevels from './utils/convertToLevels'; @@ -9,7 +9,7 @@ import quickEmbed from './utils/quickEmbed'; interface Command { data: { - options: any[]; + options: APIApplicationCommandOption[]; name: string; description: string; integration_types: number[]; @@ -27,7 +27,7 @@ const commands: Record = { integration_types: [0, 1], contexts: [0, 1, 2], }, - execute: async (interaction: { reply: (arg0: { ephemeral: boolean; content: string; }) => Promise; client: { ws: { ping: any; }; }; }) => { + execute: async (interaction) => { await interaction .reply({ ephemeral: false, @@ -44,7 +44,7 @@ const commands: Record = { integration_types: [0, 1], contexts: [0, 1, 2], }, - execute: async (interaction: { reply: (arg0: { ephemeral: boolean; content: string; }) => Promise; }) => { + execute: async (interaction) => { await client.application?.commands?.fetch().catch(console.error); const chat_commands = client.application?.commands.cache.map((a) => { return `: ${a.description}`; @@ -65,7 +65,7 @@ const commands: Record = { integration_types: [0, 1], contexts: [0, 1, 2], }, - execute: async (interaction: { reply: (arg0: { ephemeral: boolean; content: string; }) => Promise; }) => { + execute: async (interaction) => { await interaction .reply({ ephemeral: true, @@ -82,7 +82,7 @@ const commands: Record = { integration_types: [0, 1], contexts: [0, 1, 2], }, - execute: async (interaction: { reply: (arg0: { ephemeral: boolean; content: string; }) => Promise; }) => { + execute: async (interaction) => { await interaction .reply({ ephemeral: false, @@ -101,7 +101,7 @@ const commands: Record = { integration_types: [0, 1], contexts: [0, 1, 2], }, - execute: async (interaction: { reply: (arg0: { ephemeral: boolean; content: string; }) => Promise; }) => { + execute: async (interaction) => { const heap = heapStats(); Bun.gc(false); await interaction @@ -205,7 +205,7 @@ const commands: Record = { }, interaction); // Add a field for each user with a mention - leaderboard.leaderboard.forEach((entry: { user_id: any; xp: any; }, index: number) => { + leaderboard.leaderboard.forEach((entry: { user_id: string; xp: number; }, index: number) => { leaderboardEmbed.addFields([ { name: `${index + 1}.`, @@ -273,7 +273,6 @@ const commands: Record = { options: [ { name: 'action', - id: 'action', description: 'Select an action', type: 3, required: true, @@ -294,15 +293,12 @@ const commands: Record = { }, { name: 'role', - id: 'role', description: 'Enter the role name. Required for add and remove actions.', type: 8, required: false, - choices: [] }, { name: 'level', - id: 'level', description: 'Enter the level. Required for add action.', type: 4, required: false, @@ -373,7 +369,6 @@ const commands: Record = { data: { options: [{ name: 'action', - id: 'action', description: 'Note that enabling is in THIS channel and will override the current updates channel!', type: 3, required: true, @@ -450,7 +445,6 @@ const commands: Record = { data: { options: [{ name: 'action', - id: 'action', description: 'Select an action', type: 3, required: true, @@ -466,7 +460,6 @@ const commands: Record = { ] },{ name: 'cooldown', - id: 'cooldown', description: 'Enter the cooldown in seconds. Required for set action.', type: 4, required: false, diff --git a/bot/index.ts b/bot/index.ts index 69f2cc3..2e39eef 100644 --- a/bot/index.ts +++ b/bot/index.ts @@ -22,14 +22,15 @@ const rest = new REST().setToken(discordToken) const getAppId: {id?: string | null} = await rest.get(Routes.currentApplication()) || { id: null } if (!getAppId?.id) throw 'No application ID was able to be found with this token' -const data: any = await rest.put( +const data = await rest.put( Routes.applicationCommands(getAppId.id), { body: [...commandsMap.values()].map((a) => { return a.data; }), }, -); + // eslint-disable-next-line @typescript-eslint/no-explicit-any +) as any[]; console.log( `Successfully reloaded ${data.length} application (/) commands.`, diff --git a/bot/utils/handleLevelChange.ts b/bot/utils/handleLevelChange.ts index f8ebb29..b110ddc 100644 --- a/bot/utils/handleLevelChange.ts +++ b/bot/utils/handleLevelChange.ts @@ -2,7 +2,7 @@ import type { TextChannel } from "discord.js"; import client from ".."; -export default async function(guild, user, level) { +export default async function(guild: string, user: string, level: number) { const hasUpdates = await checkIfGuildHasUpdatesEnabled(guild); if (!hasUpdates.enabled) return; diff --git a/eslint.config.mjs b/eslint.config.mjs index dd68544..a105b08 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -1,10 +1,10 @@ // @ts-check -import eslint from '@eslint/js'; -import tseslint from 'typescript-eslint'; +import eslint from "@eslint/js"; +import tseslint from "typescript-eslint"; export default tseslint.config( - eslint.configs.recommended, - ...tseslint.configs.recommended, - ...tseslint.configs.stylistic, -); \ No newline at end of file + eslint.configs.recommended, + ...tseslint.configs.recommended, + ...tseslint.configs.stylistic, +);