added two new commands

This commit is contained in:
Lukian LEIZOUR 2024-03-17 13:17:44 +01:00
parent 2901c12bb9
commit 8da6a759a6
12 changed files with 271 additions and 194 deletions

View file

@ -0,0 +1,11 @@
import { SlashCommandBuilder, CommandInteraction } from "discord.js";
import { helpEmbed } from "../../libs/discord.js";
export default {
data: new SlashCommandBuilder()
.setName("help")
.setDescription("Send the help message"),
async execute(interaction: CommandInteraction) {
await interaction.reply({ embeds: [ helpEmbed() ] });
},
};

View file

@ -0,0 +1,25 @@
import { SlashCommandBuilder, CommandInteraction } from "discord.js";
import { connectToDb, getUser } from "../../libs/mysql.js";
import { errorEmbed, quotaEmbed } from "../../libs/discord.js";
export default {
data: new SlashCommandBuilder()
.setName("quota")
.setDescription("Send you quota")
.setDMPermission(false),
async execute(interaction: CommandInteraction) {
await interaction.deferReply();
const connection = await connectToDb();
const user: any[] = await getUser(connection, interaction.member?.user.id ? interaction.member?.user.id : "");
connection.end();
if (!user[0]) {
return interaction.editReply({ embeds: [ errorEmbed("Try asking something to the bot before requesting your quota.") ] });
}
interaction.editReply({ embeds: [ quotaEmbed(user[0].quota) ] });
},
};

View file

@ -11,7 +11,7 @@ export default {
setInterval(async () => {
await checkReset();
}, 10 * 60 * 1000);
}, 1000); //10 * 60 *
setInterval(async () => {
client.user?.setPresence({ activities: [{ name: '/ask | Version 3.0 !', type: 3 }] });

View file

@ -1,7 +1,48 @@
import { Events, Message, Collection } from "discord.js";
import { Events, Message, Collection, EmbedBuilder } from "discord.js";
import { getChatResponse, MistralMessage, Models, InputPrice, OutputPrice, ReturnedValue, Prompts } from "../libs/mistralai.js";
import { User, connectToDb, addUser, getUser, incrementQuota } from "../libs/mysql.js";
export function helpEmbed() {
return new EmbedBuilder()
.setTitle("Help :")
.setDescription(
`
**Commands**
- \`/help\` : display this message
- \`/ask\` : make a single request to mistralAi API
- \`/quota\` : send how many credits you have left for the month
**Other way to use the bot**
- You can DM the bot and it will answer you and remember the 6 previous messages
**Quota**
- You have 0.4$ of free credits
`
)
.setFooter({ text: "Bot by @ninja_jambon."})
.setColor("#000000");
}
export function errorEmbed(error: string) {
return new EmbedBuilder()
.setTitle("Error")
.setDescription(error)
.setFooter({ text: "Bot by @ninja_jambon."})
.setColor("#000000");
}
export function quotaEmbed(quota: number) {
return new EmbedBuilder()
.setTitle("Quota left")
.setDescription(`You have ${0.4 - quota}$ left this month.`)
.setFooter({ text: "Bot by @ninja_jambon."})
.setColor("#000000");
}
export async function getMessages(message: Message, channelid: string, userid: string): Promise<MistralMessage[]> {
var discordMessages = await message.channel.messages.fetch({ limit: 7 })
discordMessages.filter((m) => m.content && (m.author.id == message.author.id || m.author.id == process.env.BOT_ID))

View file

@ -2,7 +2,7 @@ import * as fs from "fs";
import { connectToDb, resetQuota } from "./mysql.js";
function getLastResetDate(): number {
const data: string = fs.readFileSync("../data/lastreset.txt", "utf8");
const data: string = fs.readFileSync("./src/data/lastreset.txt", "utf8");
return parseInt(data);
}
@ -11,7 +11,7 @@ export async function checkReset() {
const now = Date.now() / 1000;
if (now - lastResetDate > 1000 * 60 * 60 * 24 * 30) {
fs.writeFileSync("../data/lastreset.txt", now.toString());
fs.writeFileSync("./src/data/lastreset.txt", now.toString());
const connection = await connectToDb();