From cb16fa9239bea1f39c05f755d873b3570f033385 Mon Sep 17 00:00:00 2001 From: Lukian LEIZOUR Date: Fri, 15 Mar 2024 21:48:59 +0100 Subject: [PATCH] bug resolutions --- config.json | 3 - package-lock.json | 7 ++- package.json | 2 + src/commands/default/ping.ts | 2 +- src/commands/singleRequests/ask.ts | 14 ++--- src/events/interactionCreate.ts | 2 +- src/events/messageCreate.ts | 12 ++-- src/events/ready.ts | 4 +- src/index.ts | 93 +++++++++++++++--------------- src/libs/discord.ts | 8 +-- src/libs/mistralai.ts | 4 ++ src/libs/quotaReset.ts | 2 +- tsconfig.json | 6 +- 13 files changed, 78 insertions(+), 81 deletions(-) delete mode 100644 config.json diff --git a/config.json b/config.json deleted file mode 100644 index 78601fe..0000000 --- a/config.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "defaultPrompt": "You are an helpful assistant, you always answer in the language of the user." -} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 49929b4..cf997de 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "@mistralai/mistralai": "^0.1.3", "@types/mysql": "^2.15.26", + "@types/node": "^20.11.27", "discord.js": "^14.14.1", "dotenv": "^16.4.5", "mysql": "^2.18.1" @@ -169,9 +170,9 @@ } }, "node_modules/@types/node": { - "version": "20.11.24", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.24.tgz", - "integrity": "sha512-Kza43ewS3xoLgCEpQrsT+xRo/EJej1y0kVYGiLFE1NEODXGzTfwiC6tXTLMQskn1X4/Rjlh0MQUvx9W+L9long==", + "version": "20.11.27", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.27.tgz", + "integrity": "sha512-qyUZfMnCg1KEz57r7pzFtSGt49f6RPkPBis3Vo4PbS7roQEDn22hiHzl/Lo1q4i4hDEgBJmBF/NTNg2XR0HbFg==", "dependencies": { "undici-types": "~5.26.4" } diff --git a/package.json b/package.json index 7205e1e..ae2f22e 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "3.0.0", "description": "", "main": "index.js", + "type": "module", "scripts": { "dev": "tsx watch src", "build": "tsc", @@ -14,6 +15,7 @@ "dependencies": { "@mistralai/mistralai": "^0.1.3", "@types/mysql": "^2.15.26", + "@types/node": "^20.11.27", "discord.js": "^14.14.1", "dotenv": "^16.4.5", "mysql": "^2.18.1" diff --git a/src/commands/default/ping.ts b/src/commands/default/ping.ts index e395445..fa33fb9 100644 --- a/src/commands/default/ping.ts +++ b/src/commands/default/ping.ts @@ -1,6 +1,6 @@ import { SlashCommandBuilder, CommandInteraction } from "discord.js"; -module.exports = { +export default { data: new SlashCommandBuilder() .setName("ping") .setDescription("Replies with Pong!"), diff --git a/src/commands/singleRequests/ask.ts b/src/commands/singleRequests/ask.ts index a825724..0e4b34b 100644 --- a/src/commands/singleRequests/ask.ts +++ b/src/commands/singleRequests/ask.ts @@ -1,10 +1,8 @@ import { SlashCommandBuilder, ChatInputCommandInteraction } from "discord.js"; -import { getChatResponse, MistralMessage, Models, InputPrice, OutputPrice, ReturnedValue } from "../../libs/mistralai"; -import { User, connectToDb, addUser, getUser, incrementQuota } from "../../libs/mysql"; +import { getChatResponse, MistralMessage, Models, InputPrice, OutputPrice, ReturnedValue, Prompts } from "../../libs/mistralai.js"; +import { User, connectToDb, addUser, getUser, incrementQuota } from "../../libs/mysql.js"; -const data = require("../../../config.json"); - -module.exports = { +export default { data: new SlashCommandBuilder() .setName("ask") .setDescription("Make a single request to mistral API") @@ -13,8 +11,8 @@ module.exports = { option.setName("prompt").setDescription("The prompt to send to the API").setRequired(true) ), async execute(interaction: ChatInputCommandInteraction) { - if (interaction.member?.user.id != '372437660167438337') { - return interaction.reply("you are not allowed to use this command"); + if (interaction.member?.user.id == undefined) { + return; } await interaction.deferReply(); @@ -39,7 +37,7 @@ module.exports = { const messages: MistralMessage[] = [ { role: "system", - content: data.defaultPrompt + content: Prompts.default }, { role: "user", diff --git a/src/events/interactionCreate.ts b/src/events/interactionCreate.ts index 80dc5bc..03c4322 100644 --- a/src/events/interactionCreate.ts +++ b/src/events/interactionCreate.ts @@ -1,6 +1,6 @@ import { Events, Interaction } from "discord.js"; -module.exports = { +export default { name: Events.InteractionCreate, async execute(interaction: Interaction) { if (interaction.isChatInputCommand()) { diff --git a/src/events/messageCreate.ts b/src/events/messageCreate.ts index f35b8a6..1d97570 100644 --- a/src/events/messageCreate.ts +++ b/src/events/messageCreate.ts @@ -1,18 +1,14 @@ import { Events, Message } from "discord.js"; -import { getChatResponse, MistralMessage, Models, InputPrice, OutputPrice, ReturnedValue } from "../libs/mistralai"; -import { User, connectToDb, addUser, getUser, incrementQuota } from "../libs/mysql"; -import { getMessages } from "../libs/discord"; +import { getChatResponse, MistralMessage, Models, InputPrice, OutputPrice, ReturnedValue } from "../libs/mistralai.js"; +import { User, connectToDb, addUser, getUser, incrementQuota } from "../libs/mysql.js"; +import { getMessages } from "../libs/discord.js"; -module.exports = { +export default { name: Events.MessageCreate, async execute(message: Message) { if (!message.guildId && message.author.id != process.env.BOT_ID) { const prompt: string = message.content; - if (message.author.id != '372437660167438337') { - return message.reply("you are not allowed to use this command"); - } - const connection = await connectToDb(); var user: User[] = await getUser(connection, message.author.id); diff --git a/src/events/ready.ts b/src/events/ready.ts index 3506a70..132e92f 100644 --- a/src/events/ready.ts +++ b/src/events/ready.ts @@ -1,7 +1,7 @@ import { Events, Client } from "discord.js"; -import { checkReset } from "../libs/quotaReset"; +import { checkReset } from "../libs/quotaReset.js"; -module.exports = { +export default { name: Events.ClientReady, once: true, execute(client: Client) { diff --git a/src/index.ts b/src/index.ts index 0c4b89b..4ebde80 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,48 +1,64 @@ import * as fs from 'fs'; import * as path from 'path'; import "dotenv/config"; -import { Client, Collection, REST, Routes, RESTPutAPIApplicationCommandsResult, GatewayIntentBits, Partials } from 'discord.js'; +import { Client, Collection, REST, Routes, RESTPutAPIApplicationCommandsResult } from 'discord.js'; const client: Client = new Client({ - intents: [ - GatewayIntentBits.Guilds, - GatewayIntentBits.GuildMessages, - GatewayIntentBits.MessageContent, - GatewayIntentBits.DirectMessages, - ], - partials: [ - Partials.Channel, - Partials.Message, - ] + intents: [], + partials: [] }) client.commands = new Collection(); -const commands = []; -const foldersPath = path.join(__dirname, "commands"); -const commandFolders = fs.readdirSync(foldersPath); +const commands: any[] = []; -for (const folder of commandFolders) { - const commandsPath = path.join(foldersPath, folder); - const commandFiles = fs - .readdirSync(commandsPath) - .filter((file) => file.endsWith(".ts") || file.endsWith(".js")); +async function loadCommands() { + const foldersPath = './src/commands'; + const commandFolders = fs.readdirSync(foldersPath); - for (const file of commandFiles) { - const filePath = path.join(commandsPath, file); - const command = require(filePath); - if ("data" in command && "execute" in command) { - client.commands.set(command.data.name, command); - commands.push(command.data.toJSON()); - } - else { - console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); - } - } + for (const folder of commandFolders) { + const commandsPath = `./src/commands/${folder}`; + const commandFiles = fs + .readdirSync(commandsPath) + .filter((file) => file.endsWith(".ts") || file.endsWith(".js")); + + for (const file of commandFiles) { + const filePath = `./commands/${folder}/${file}`; + const command = await import(filePath.replace(".ts", ".js")); + if ("data" in command.default && "execute" in command.default) { + client.commands.set(command.default.data.name, command.default); + commands.push(command.default.data.toJSON()); + } + else { + console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); + } + } + } +} + +async function loadEvents() { + const eventsPath = "./src/events"; + const eventFiles = fs + .readdirSync(eventsPath) + .filter((file) => file.endsWith(".ts") || file.endsWith(".js")); + + for (const file of eventFiles) { + const filePath = `./events/${file}`; + const event = await import(filePath.replace(".ts", ".js")); + + if (event.default.once) { + client.once(event.default.name, (...args) => event.default.execute(...args)); + } + else { + client.on(event.default.name, (...args) => event.default.execute(...args)); + } + } } const rest = new REST().setToken(process.env.DISCORD_TOKEN ? process.env.DISCORD_TOKEN : ""); (async () => { + await loadCommands(); + await loadEvents(); try { console.log(`Started refreshing ${commands.length} application (/) commands.`); @@ -57,21 +73,4 @@ const rest = new REST().setToken(process.env.DISCORD_TOKEN ? process.env.DISCORD } })(); -const eventsPath = path.join(__dirname, "events"); -const eventFiles = fs - .readdirSync(eventsPath) - .filter((file) => file.endsWith(".ts") || file.endsWith(".js")); - -for (const file of eventFiles) { - const filePath = path.join(eventsPath, file); - const event = require(filePath); - - if (event.once) { - client.once(event.name, (...args) => event.execute(...args)); - } - else { - client.on(event.name, (...args) => event.execute(...args)); - } -} - client.login(process.env.DISCORD_TOKEN); diff --git a/src/libs/discord.ts b/src/libs/discord.ts index 3e09a8e..3141368 100644 --- a/src/libs/discord.ts +++ b/src/libs/discord.ts @@ -1,8 +1,6 @@ import { Events, Message, Collection } from "discord.js"; -import { getChatResponse, MistralMessage, Models, InputPrice, OutputPrice, ReturnedValue } from "../libs/mistralai"; -import { User, connectToDb, addUser, getUser, incrementQuota } from "../libs/mysql"; - -const data = require("../../config.json"); +import { getChatResponse, MistralMessage, Models, InputPrice, OutputPrice, ReturnedValue, Prompts } from "../libs/mistralai.js"; +import { User, connectToDb, addUser, getUser, incrementQuota } from "../libs/mysql.js"; export async function getMessages(message: Message, channelid: string, userid: string): Promise { var discordMessages = await message.channel.messages.fetch({ limit: 7 }) @@ -12,7 +10,7 @@ export async function getMessages(message: Message, channelid: string, userid: s var messages: MistralMessage[] = [ { role: "system", - content: data.defaultPrompt + content: Prompts.default, } ] diff --git a/src/libs/mistralai.ts b/src/libs/mistralai.ts index 9609a9a..8d32273 100644 --- a/src/libs/mistralai.ts +++ b/src/libs/mistralai.ts @@ -36,6 +36,10 @@ export interface ReturnedValue { responseUsage: number, } +export enum Prompts { + default = "You are an helpful assistant, you always answer in the language of the user.", +} + const apiKey = process.env.MISTRAL_API_KEY; const client = new MistralClient(apiKey); diff --git a/src/libs/quotaReset.ts b/src/libs/quotaReset.ts index e5ff5a1..8b8ecb9 100644 --- a/src/libs/quotaReset.ts +++ b/src/libs/quotaReset.ts @@ -1,5 +1,5 @@ import * as fs from "fs"; -import { connectToDb, resetQuota } from "./mysql"; +import { connectToDb, resetQuota } from "./mysql.js"; function getLastResetDate(): number { const data: string = fs.readFileSync("../data/lastreset.txt", "utf8"); diff --git a/tsconfig.json b/tsconfig.json index 145223f..bd66ca5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,9 @@ { "compilerOptions": { - "target": "es5", - "module": "commonjs", + "target": "ES5", + "module": "NodeNext", + "moduleResolution": "NodeNext", + "resolveJsonModule": true, "strict": true, "outDir": "./dist", "skipLibCheck": true,