l1l1th/src/index.js
Ashley Graves ce28ba511e prompt
2024-10-03 11:45:11 +02:00

87 lines
No EOL
2.7 KiB
JavaScript

const { REST, Routes, Client, Collection, GatewayIntentBits, Events } = require("discord.js");
const { default: Groq } = require("groq-sdk");
const path = require("node:path");
const fs = require("node:fs");
require("dotenv").config();
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.commands = new Collection();
client.groq = new Groq({ apiKey: process.env.GROQ_API_KEY });
//process.env.PROMPT = fs.readFileSync(path.join(__dirname, "prompt.txt"));
client.prompts = [];
var promptsDir = path.join(__dirname, "prompts");
const commands = [];
const foldersPath = path.join(__dirname, "commands");
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith(".js"));
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.`);
}
}
}
client.on(Events.InteractionCreate, async interaction => {
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
await command.execute(interaction);
} catch (err) {
console.error(err);
const data = {
content: `${err.name}: ${err.message}`,
ephemeral: true
};
if (interaction.replied || interaction.deferred) {
await interaction.followUp(data);
} else {
await interaction.reply(data);
}
}
});
client.once(Events.ClientReady, async() => {
console.log(`Ready! Logged in as ${client.user.displayName}`);
var user = client.user.toJSON();
for (const prompt of fs.readdirSync(promptsDir)) {
var data = fs.readFileSync(path.join(promptsDir, prompt), "utf-8");
for (const info in user) {
data = data.replaceAll(`{{${info}}}`, user[info]);
}
client.prompts[prompt.substring(0, prompt.lastIndexOf("."))] = data;
}
const rest = new REST().setToken(client.token);
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);
const data = await rest.put(
Routes.applicationCommands(client.user.id), { body: commands },
);
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(error);
}
});
client.login(process.env.TOKEN);