This commit is contained in:
Lukian LEIZOUR 2024-03-14 19:14:23 +01:00
parent 9341181f99
commit 236ca371bb
39 changed files with 659 additions and 1531 deletions

View file

@ -0,0 +1,21 @@
import { Events, Interaction } from "discord.js";
module.exports = {
name: Events.InteractionCreate,
async execute(interaction: Interaction) {
if (interaction.isChatInputCommand()) {
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
return console.error(`No command matching ${interaction.commandName} was found.`);
}
try {
await command.execute(interaction);
}
catch (error) {
console.error(error);
}
}
},
};

View file

@ -0,0 +1,43 @@
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";
module.exports = {
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);
if (!user[0]) {
await addUser(connection, message.author.username, message.author.id);
user = await getUser(connection, message.author.id);
}
if (user[0].quota > 0.4) {
message.reply("You have exceed your quota.")
connection.end();
return;
}
const messages: MistralMessage[] = await getMessages(message, message.channelId, message.author.id);
await message.channel.sendTyping();
const response: ReturnedValue = await getChatResponse(messages, Models.multi_tiny);
await incrementQuota(connection, message.author.id, InputPrice.multi_tiny * response.promptUsage + OutputPrice.multi_tiny * response.responseUsage);
connection.end();
message.reply(response.message);
}
},
};

14
src/events/ready.ts Normal file
View file

@ -0,0 +1,14 @@
import { Events, Client } from "discord.js";
import { checkReset } from "../libs/quotaReset";
module.exports = {
name: Events.ClientReady,
once: true,
execute(client: Client) {
console.log(`Ready! Logged in as ${client.user?.tag}`);
setInterval(async () => {
await checkReset();
}, 10 * 60 * 1000);
},
};