bug resolutions

This commit is contained in:
Lukian LEIZOUR 2024-03-15 21:48:59 +01:00
parent 414a9734c9
commit cb16fa9239
13 changed files with 78 additions and 81 deletions

View file

@ -1,3 +0,0 @@
{
"defaultPrompt": "You are an helpful assistant, you always answer in the language of the user."
}

7
package-lock.json generated
View file

@ -11,6 +11,7 @@
"dependencies": { "dependencies": {
"@mistralai/mistralai": "^0.1.3", "@mistralai/mistralai": "^0.1.3",
"@types/mysql": "^2.15.26", "@types/mysql": "^2.15.26",
"@types/node": "^20.11.27",
"discord.js": "^14.14.1", "discord.js": "^14.14.1",
"dotenv": "^16.4.5", "dotenv": "^16.4.5",
"mysql": "^2.18.1" "mysql": "^2.18.1"
@ -169,9 +170,9 @@
} }
}, },
"node_modules/@types/node": { "node_modules/@types/node": {
"version": "20.11.24", "version": "20.11.27",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.24.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.27.tgz",
"integrity": "sha512-Kza43ewS3xoLgCEpQrsT+xRo/EJej1y0kVYGiLFE1NEODXGzTfwiC6tXTLMQskn1X4/Rjlh0MQUvx9W+L9long==", "integrity": "sha512-qyUZfMnCg1KEz57r7pzFtSGt49f6RPkPBis3Vo4PbS7roQEDn22hiHzl/Lo1q4i4hDEgBJmBF/NTNg2XR0HbFg==",
"dependencies": { "dependencies": {
"undici-types": "~5.26.4" "undici-types": "~5.26.4"
} }

View file

@ -3,6 +3,7 @@
"version": "3.0.0", "version": "3.0.0",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"type": "module",
"scripts": { "scripts": {
"dev": "tsx watch src", "dev": "tsx watch src",
"build": "tsc", "build": "tsc",
@ -14,6 +15,7 @@
"dependencies": { "dependencies": {
"@mistralai/mistralai": "^0.1.3", "@mistralai/mistralai": "^0.1.3",
"@types/mysql": "^2.15.26", "@types/mysql": "^2.15.26",
"@types/node": "^20.11.27",
"discord.js": "^14.14.1", "discord.js": "^14.14.1",
"dotenv": "^16.4.5", "dotenv": "^16.4.5",
"mysql": "^2.18.1" "mysql": "^2.18.1"

View file

@ -1,6 +1,6 @@
import { SlashCommandBuilder, CommandInteraction } from "discord.js"; import { SlashCommandBuilder, CommandInteraction } from "discord.js";
module.exports = { export default {
data: new SlashCommandBuilder() data: new SlashCommandBuilder()
.setName("ping") .setName("ping")
.setDescription("Replies with Pong!"), .setDescription("Replies with Pong!"),

View file

@ -1,10 +1,8 @@
import { SlashCommandBuilder, ChatInputCommandInteraction } from "discord.js"; import { SlashCommandBuilder, ChatInputCommandInteraction } from "discord.js";
import { getChatResponse, MistralMessage, Models, InputPrice, OutputPrice, ReturnedValue } from "../../libs/mistralai"; import { getChatResponse, MistralMessage, Models, InputPrice, OutputPrice, ReturnedValue, Prompts } from "../../libs/mistralai.js";
import { User, connectToDb, addUser, getUser, incrementQuota } from "../../libs/mysql"; import { User, connectToDb, addUser, getUser, incrementQuota } from "../../libs/mysql.js";
const data = require("../../../config.json"); export default {
module.exports = {
data: new SlashCommandBuilder() data: new SlashCommandBuilder()
.setName("ask") .setName("ask")
.setDescription("Make a single request to mistral API") .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) option.setName("prompt").setDescription("The prompt to send to the API").setRequired(true)
), ),
async execute(interaction: ChatInputCommandInteraction) { async execute(interaction: ChatInputCommandInteraction) {
if (interaction.member?.user.id != '372437660167438337') { if (interaction.member?.user.id == undefined) {
return interaction.reply("you are not allowed to use this command"); return;
} }
await interaction.deferReply(); await interaction.deferReply();
@ -39,7 +37,7 @@ module.exports = {
const messages: MistralMessage[] = [ const messages: MistralMessage[] = [
{ {
role: "system", role: "system",
content: data.defaultPrompt content: Prompts.default
}, },
{ {
role: "user", role: "user",

View file

@ -1,6 +1,6 @@
import { Events, Interaction } from "discord.js"; import { Events, Interaction } from "discord.js";
module.exports = { export default {
name: Events.InteractionCreate, name: Events.InteractionCreate,
async execute(interaction: Interaction) { async execute(interaction: Interaction) {
if (interaction.isChatInputCommand()) { if (interaction.isChatInputCommand()) {

View file

@ -1,18 +1,14 @@
import { Events, Message } from "discord.js"; import { Events, Message } from "discord.js";
import { getChatResponse, MistralMessage, Models, InputPrice, OutputPrice, ReturnedValue } from "../libs/mistralai"; import { getChatResponse, MistralMessage, Models, InputPrice, OutputPrice, ReturnedValue } from "../libs/mistralai.js";
import { User, connectToDb, addUser, getUser, incrementQuota } from "../libs/mysql"; import { User, connectToDb, addUser, getUser, incrementQuota } from "../libs/mysql.js";
import { getMessages } from "../libs/discord"; import { getMessages } from "../libs/discord.js";
module.exports = { export default {
name: Events.MessageCreate, name: Events.MessageCreate,
async execute(message: Message) { async execute(message: Message) {
if (!message.guildId && message.author.id != process.env.BOT_ID) { if (!message.guildId && message.author.id != process.env.BOT_ID) {
const prompt: string = message.content; 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(); const connection = await connectToDb();
var user: User[] = await getUser(connection, message.author.id); var user: User[] = await getUser(connection, message.author.id);

View file

@ -1,7 +1,7 @@
import { Events, Client } from "discord.js"; import { Events, Client } from "discord.js";
import { checkReset } from "../libs/quotaReset"; import { checkReset } from "../libs/quotaReset.js";
module.exports = { export default {
name: Events.ClientReady, name: Events.ClientReady,
once: true, once: true,
execute(client: Client) { execute(client: Client) {

View file

@ -1,48 +1,64 @@
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import "dotenv/config"; 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({ const client: Client = new Client({
intents: [ intents: [],
GatewayIntentBits.Guilds, partials: []
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.DirectMessages,
],
partials: [
Partials.Channel,
Partials.Message,
]
}) })
client.commands = new Collection(); client.commands = new Collection();
const commands = []; const commands: any[] = [];
const foldersPath = path.join(__dirname, "commands");
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) { async function loadCommands() {
const commandsPath = path.join(foldersPath, folder); const foldersPath = './src/commands';
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = `./src/commands/${folder}`;
const commandFiles = fs const commandFiles = fs
.readdirSync(commandsPath) .readdirSync(commandsPath)
.filter((file) => file.endsWith(".ts") || file.endsWith(".js")); .filter((file) => file.endsWith(".ts") || file.endsWith(".js"));
for (const file of commandFiles) { for (const file of commandFiles) {
const filePath = path.join(commandsPath, file); const filePath = `./commands/${folder}/${file}`;
const command = require(filePath); const command = await import(filePath.replace(".ts", ".js"));
if ("data" in command && "execute" in command) { if ("data" in command.default && "execute" in command.default) {
client.commands.set(command.data.name, command); client.commands.set(command.default.data.name, command.default);
commands.push(command.data.toJSON()); commands.push(command.default.data.toJSON());
} }
else { else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); 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 : ""); const rest = new REST().setToken(process.env.DISCORD_TOKEN ? process.env.DISCORD_TOKEN : "");
(async () => { (async () => {
await loadCommands();
await loadEvents();
try { try {
console.log(`Started refreshing ${commands.length} application (/) commands.`); 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); client.login(process.env.DISCORD_TOKEN);

View file

@ -1,8 +1,6 @@
import { Events, Message, Collection } from "discord.js"; import { Events, Message, Collection } from "discord.js";
import { getChatResponse, MistralMessage, Models, InputPrice, OutputPrice, ReturnedValue } from "../libs/mistralai"; import { getChatResponse, MistralMessage, Models, InputPrice, OutputPrice, ReturnedValue, Prompts } from "../libs/mistralai.js";
import { User, connectToDb, addUser, getUser, incrementQuota } from "../libs/mysql"; import { User, connectToDb, addUser, getUser, incrementQuota } from "../libs/mysql.js";
const data = require("../../config.json");
export async function getMessages(message: Message, channelid: string, userid: string): Promise<MistralMessage[]> { export async function getMessages(message: Message, channelid: string, userid: string): Promise<MistralMessage[]> {
var discordMessages = await message.channel.messages.fetch({ limit: 7 }) 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[] = [ var messages: MistralMessage[] = [
{ {
role: "system", role: "system",
content: data.defaultPrompt content: Prompts.default,
} }
] ]

View file

@ -36,6 +36,10 @@ export interface ReturnedValue {
responseUsage: number, 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 apiKey = process.env.MISTRAL_API_KEY;
const client = new MistralClient(apiKey); const client = new MistralClient(apiKey);

View file

@ -1,5 +1,5 @@
import * as fs from "fs"; import * as fs from "fs";
import { connectToDb, resetQuota } from "./mysql"; import { connectToDb, resetQuota } from "./mysql.js";
function getLastResetDate(): number { function getLastResetDate(): number {
const data: string = fs.readFileSync("../data/lastreset.txt", "utf8"); const data: string = fs.readFileSync("../data/lastreset.txt", "utf8");

View file

@ -1,7 +1,9 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "es5", "target": "ES5",
"module": "commonjs", "module": "NodeNext",
"moduleResolution": "NodeNext",
"resolveJsonModule": true,
"strict": true, "strict": true,
"outDir": "./dist", "outDir": "./dist",
"skipLibCheck": true, "skipLibCheck": true,