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

@ -1,10 +1,10 @@
import { SlashCommandBuilder, CommandInteraction } from "discord.js";
export default {
data: new SlashCommandBuilder()
.setName("ping")
.setDescription("Replies with Pong!"),
async execute(interaction: CommandInteraction) {
await interaction.reply("Pong!");
},
data: new SlashCommandBuilder()
.setName("ping")
.setDescription("Replies with Pong!"),
async execute(interaction: CommandInteraction) {
await interaction.reply("Pong!");
},
};

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

@ -3,53 +3,53 @@ import { getChatResponse, MistralMessage, Models, InputPrice, OutputPrice, Retur
import { User, connectToDb, addUser, getUser, incrementQuota } from "../../libs/mysql.js";
export default {
data: new SlashCommandBuilder()
.setName("ask")
.setDescription("Make a single request to mistral API")
.setDMPermission(false)
.addStringOption(option =>
option.setName("prompt").setDescription("The prompt to send to the API").setRequired(true)
),
async execute(interaction: ChatInputCommandInteraction) {
if (interaction.member?.user.id == undefined) {
return;
}
data: new SlashCommandBuilder()
.setName("ask")
.setDescription("Make a single request to mistral API")
.setDMPermission(false)
.addStringOption(option =>
option.setName("prompt").setDescription("The prompt to send to the API").setRequired(true)
),
async execute(interaction: ChatInputCommandInteraction) {
if (interaction.member?.user.id == undefined) {
return;
}
await interaction.deferReply();
await interaction.deferReply();
const connection = await connectToDb();
const connection = await connectToDb();
var user: User[] = await getUser(connection, interaction.member?.user.id);
var user: User[] = await getUser(connection, interaction.member?.user.id);
if (!user[0]) {
await addUser(connection, interaction.member?.user.username, interaction.member?.user.id);
user = await getUser(connection, interaction.member?.user.id);
}
if (!user[0]) {
await addUser(connection, interaction.member?.user.username, interaction.member?.user.id);
user = await getUser(connection, interaction.member?.user.id);
}
if (user[0].quota > 0.4) {
interaction.editReply("You have exceed your quota.")
connection.end();
return;
}
if (user[0].quota > 0.4) {
interaction.editReply("You have exceed your quota.")
connection.end();
return;
}
const prompt: string | null = interaction.options.getString("prompt");
const prompt: string | null = interaction.options.getString("prompt");
const messages: MistralMessage[] = [
{
role: "system",
content: Prompts.default
},
{
role: "user",
content: prompt ? prompt : ""
},
]
const messages: MistralMessage[] = [
{
role: "system",
content: Prompts.default
},
{
role: "user",
content: prompt ? prompt : ""
},
]
const response: ReturnedValue = await getChatResponse(messages, Models.multi_tiny);
const response: ReturnedValue = await getChatResponse(messages, Models.multi_tiny);
await incrementQuota(connection, interaction.member?.user.id, InputPrice.multi_tiny * response.promptUsage + OutputPrice.multi_tiny * response.responseUsage);
connection.end();
await incrementQuota(connection, interaction.member?.user.id, InputPrice.multi_tiny * response.promptUsage + OutputPrice.multi_tiny * response.responseUsage);
connection.end();
interaction.editReply(response.message);
},
interaction.editReply(response.message);
},
};