Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions api/db/queries/guilds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand All @@ -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;
Expand Down Expand Up @@ -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]);
}
});
});
Expand Down
5 changes: 3 additions & 2 deletions api/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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" });
Expand Down Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to fix at some point

Copy link
Contributor Author

@ToastedDev ToastedDev Jul 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could maybe return 500 if it errors? but then we would need an error page...

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'll handle the error page stuff 👍

const [err, botInfo] = await getBotInfo();
res.render("index", { botInfo });
});
Expand Down
23 changes: 8 additions & 15 deletions bot/commands.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// 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';
import quickEmbed from './utils/quickEmbed';

interface Command {
data: {
options: any[];
options: APIApplicationCommandOption[];
name: string;
description: string;
integration_types: number[];
Expand All @@ -27,7 +27,7 @@ const commands: Record<string, Command> = {
integration_types: [0, 1],
contexts: [0, 1, 2],
},
execute: async (interaction: { reply: (arg0: { ephemeral: boolean; content: string; }) => Promise<any>; client: { ws: { ping: any; }; }; }) => {
execute: async (interaction) => {
await interaction
.reply({
ephemeral: false,
Expand All @@ -44,7 +44,7 @@ const commands: Record<string, Command> = {
integration_types: [0, 1],
contexts: [0, 1, 2],
},
execute: async (interaction: { reply: (arg0: { ephemeral: boolean; content: string; }) => Promise<any>; }) => {
execute: async (interaction) => {
await client.application?.commands?.fetch().catch(console.error);
const chat_commands = client.application?.commands.cache.map((a) => {
return `</${a.name}:${a.id}>: ${a.description}`;
Expand All @@ -65,7 +65,7 @@ const commands: Record<string, Command> = {
integration_types: [0, 1],
contexts: [0, 1, 2],
},
execute: async (interaction: { reply: (arg0: { ephemeral: boolean; content: string; }) => Promise<any>; }) => {
execute: async (interaction) => {
await interaction
.reply({
ephemeral: true,
Expand All @@ -82,7 +82,7 @@ const commands: Record<string, Command> = {
integration_types: [0, 1],
contexts: [0, 1, 2],
},
execute: async (interaction: { reply: (arg0: { ephemeral: boolean; content: string; }) => Promise<any>; }) => {
execute: async (interaction) => {
await interaction
.reply({
ephemeral: false,
Expand All @@ -101,7 +101,7 @@ const commands: Record<string, Command> = {
integration_types: [0, 1],
contexts: [0, 1, 2],
},
execute: async (interaction: { reply: (arg0: { ephemeral: boolean; content: string; }) => Promise<any>; }) => {
execute: async (interaction) => {
const heap = heapStats();
Bun.gc(false);
await interaction
Expand Down Expand Up @@ -205,7 +205,7 @@ const commands: Record<string, Command> = {
}, 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}.`,
Expand Down Expand Up @@ -273,7 +273,6 @@ const commands: Record<string, Command> = {
options: [
{
name: 'action',
id: 'action',
description: 'Select an action',
type: 3,
required: true,
Expand All @@ -294,15 +293,12 @@ const commands: Record<string, Command> = {
},
{
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,
Expand Down Expand Up @@ -373,7 +369,6 @@ const commands: Record<string, Command> = {
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,
Expand Down Expand Up @@ -450,7 +445,6 @@ const commands: Record<string, Command> = {
data: {
options: [{
name: 'action',
id: 'action',
description: 'Select an action',
type: 3,
required: true,
Expand All @@ -466,7 +460,6 @@ const commands: Record<string, Command> = {
]
},{
name: 'cooldown',
id: 'cooldown',
description: 'Enter the cooldown in seconds. Required for set action.',
type: 4,
required: false,
Expand Down
5 changes: 3 additions & 2 deletions bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to fix at some point

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah there needs to be a real type for it, i'll try to see if there's a type for it in d.js or @d.js/rest

) as any[];

console.log(
`Successfully reloaded ${data.length} application (/) commands.`,
Expand Down
2 changes: 1 addition & 1 deletion bot/utils/handleLevelChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
12 changes: 6 additions & 6 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -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,
);
eslint.configs.recommended,
...tseslint.configs.recommended,
...tseslint.configs.stylistic,
);