2024-10-11 21:11:18 +02:00
|
|
|
const { REST, Routes, Client, Collection, GatewayIntentBits, Events, Partials, InteractionType, ActivityType } = require("discord.js");
|
2024-10-03 10:14:17 +02:00
|
|
|
const { default: Groq } = require("groq-sdk");
|
2024-10-10 18:08:19 +02:00
|
|
|
const { knex } = require("./db.js");
|
2024-10-03 10:14:17 +02:00
|
|
|
const path = require("node:path");
|
|
|
|
const fs = require("node:fs");
|
2024-10-03 09:54:53 +02:00
|
|
|
require("dotenv").config();
|
|
|
|
|
2024-10-10 12:44:52 +02:00
|
|
|
const client = new Client({
|
|
|
|
intents: Object.keys(GatewayIntentBits).map(i => GatewayIntentBits[i]),
|
|
|
|
partials: Object.keys(Partials).map(i => Partials[i])
|
|
|
|
});
|
2024-10-03 09:54:53 +02:00
|
|
|
|
|
|
|
client.commands = new Collection();
|
|
|
|
client.groq = new Groq({ apiKey: process.env.GROQ_API_KEY });
|
2024-10-03 11:45:11 +02:00
|
|
|
|
|
|
|
client.prompts = [];
|
|
|
|
var promptsDir = path.join(__dirname, "prompts");
|
2024-10-03 09:54:53 +02:00
|
|
|
|
|
|
|
const commands = [];
|
2024-10-03 10:14:17 +02:00
|
|
|
const foldersPath = path.join(__dirname, "commands");
|
2024-10-03 09:54:53 +02:00
|
|
|
const commandFolders = fs.readdirSync(foldersPath);
|
|
|
|
|
|
|
|
for (const folder of commandFolders) {
|
|
|
|
const commandsPath = path.join(foldersPath, folder);
|
2024-10-03 10:14:17 +02:00
|
|
|
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith(".js"));
|
2024-10-03 09:54:53 +02:00
|
|
|
for (const file of commandFiles) {
|
|
|
|
const filePath = path.join(commandsPath, file);
|
|
|
|
const command = require(filePath);
|
2024-10-03 10:14:17 +02:00
|
|
|
if ("data" in command && "execute" in command) {
|
2024-10-03 09:54:53 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-10-10 14:48:59 +02:00
|
|
|
if (interaction.isAutocomplete()) {
|
|
|
|
try {
|
|
|
|
await command.autocomplete(interaction);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
} else if (interaction.isCommand()) {
|
2024-10-11 21:08:19 +02:00
|
|
|
console.log(`${interaction.user.username} ran ${(interaction.isChatInputCommand() ? "/" : '') + interaction.commandName}`);
|
2024-10-10 14:48:59 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
interaction.defaultModel = "llama-3.1-70b-versatile";
|
|
|
|
await command.execute(interaction);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
const data = {
|
|
|
|
content: `${err.name}: ${err.message}`,
|
|
|
|
ephemeral: true
|
|
|
|
};
|
2024-10-11 21:08:19 +02:00
|
|
|
try {
|
|
|
|
if (interaction.replied || interaction.deferred) {
|
|
|
|
await interaction.followUp(data);
|
|
|
|
} else {
|
|
|
|
await interaction.reply(data);
|
|
|
|
}
|
|
|
|
} catch { }
|
2024-10-03 09:54:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-10-10 11:56:53 +02:00
|
|
|
client.once(Events.ClientReady, async () => {
|
2024-10-03 11:45:11 +02:00
|
|
|
console.log(`Ready! Logged in as ${client.user.displayName}`);
|
2024-10-11 21:11:18 +02:00
|
|
|
client.user.setActivity({
|
|
|
|
name: "kissing my sister",
|
|
|
|
type: ActivityType.Custom
|
2024-10-11 21:12:22 +02:00
|
|
|
});
|
2024-10-03 11:45:11 +02:00
|
|
|
|
2024-10-10 18:08:19 +02:00
|
|
|
if (!(await knex.schema.hasTable("blacklists")))
|
|
|
|
await knex.schema.createTable("blacklists", function (table) {
|
|
|
|
table.string("user").primary();
|
|
|
|
table.string("blacklist");
|
|
|
|
});
|
|
|
|
|
2024-10-11 14:07:01 +02:00
|
|
|
if (!(await knex.schema.hasTable("serverconfigs")))
|
|
|
|
await knex.schema.createTable("serverconfigs", function (table) {
|
|
|
|
table.string("id").primary();
|
|
|
|
table.string("data");
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!(await knex.schema.hasTable("userconfigs")))
|
|
|
|
await knex.schema.createTable("userconfigs", function (table) {
|
|
|
|
table.string("id").primary();
|
|
|
|
table.string("data");
|
|
|
|
});
|
|
|
|
|
2024-10-03 11:45:11 +02:00
|
|
|
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;
|
|
|
|
}
|
2024-10-03 09:54:53 +02:00
|
|
|
|
|
|
|
const rest = new REST().setToken(client.token);
|
|
|
|
|
2024-10-03 11:45:11 +02:00
|
|
|
try {
|
|
|
|
console.log(`Started refreshing ${commands.length} application (/) commands.`);
|
2024-10-03 09:54:53 +02:00
|
|
|
|
2024-10-03 11:45:11 +02:00
|
|
|
const data = await rest.put(
|
|
|
|
Routes.applicationCommands(client.user.id), { body: commands },
|
|
|
|
);
|
2024-10-03 09:54:53 +02:00
|
|
|
|
2024-10-03 11:45:11 +02:00
|
|
|
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
2024-10-03 09:54:53 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
client.login(process.env.TOKEN);
|