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": {
"@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"
}

View file

@ -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"

View file

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

View file

@ -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",

View file

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

View file

@ -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);

View file

@ -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) {

View file

@ -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);

View file

@ -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<MistralMessage[]> {
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,
}
]

View file

@ -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);

View file

@ -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");

View file

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