discord-freepbx-manager/index.js
2025-01-25 09:40:38 -07:00

134 lines
3.7 KiB
JavaScript

require("dotenv").config();
const mariadb = require("mariadb");
const pool = mariadb.createPool({
host: process.env.DB_HOST,
port: process.env.DB_PORT || 3306,
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: "asterisk",
connectionLimit: 5,
});
const FreepbxManager = require("./freepbx");
const fpbx = new FreepbxManager({
url: process.env.FREEPBX_URL,
clientId: process.env.FREEPBX_CLIENT_ID,
clientSecret: process.env.FREEPBX_CLIENT_SECRET,
dbPool: pool,
});
const Discord = require('discord.js');
const client = new Discord.Client({
intents: ["GuildMembers", "Guilds", "GuildPresences"]
});
const colors = require('colors');
const log = {
log: (msg) => {
console.log(msg);
},
info: (msg) => {
console.log(`${colors.cyan("[INFO]")} ${msg}`);
},
warn: (msg) => {
console.log(`${colors.yellow("[WARN]")} ${msg}`);
},
error: (msg) => {
console.log(`${colors.red("[ERROR]")} ${msg}`);
},
success: (msg) => {
console.log(`${colors.green("[SUCCESS]")} ${msg}`);
},
debug: (msg) => {
console.log(`${colors.magenta("[DEBUG]")} ${msg}`);
}
}
// Put clients in global for use elsewhere.
global.pool = pool;
global.fpbx = fpbx;
global.client = client;
global.log = log;
client.on('ready', async () => {
log.success(`Logged in as ${client.user.displayName}`);
const commands = require("./commands")
// Command registration
log.info("Registering commands...")
await (async () => {
try {
const rest = new Discord.REST().setToken(client.token);
// Global Commands
log.info(`Registering global commands`);
rest.put(Discord.Routes.applicationCommands(client.user.id), { body: commands }).then(() => {
log.success("Global commands registered")
}).catch((error) => {
log.error(error)
});
} catch (error) {
log.error(error)
}
})();
});
client.on('interactionCreate', async interaction => {
if (!interaction.inGuild()) return interaction.reply({ content: "This bot is not designed to be used in DMs.", ephemeral: true });
switch (interaction.type) {
case Discord.InteractionType.ApplicationCommand:
const command = require(`./interactionHandlers/commands/${interaction.commandName}`);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
log.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
break;
case Discord.InteractionType.MessageComponent:
const component = require(`./interactionHandlers/components/${interaction.customId}`);
if (!component) return;
try {
await component.execute(interaction);
} catch (error) {
log.error(error);
await interaction.reply({ content: 'There was an error while executing this component!', ephemeral: true });
}
break;
}
});
// DEBUG: Insert 200 extensions into discord_users table
// (async () => {
// try {
// const conn = await pool.getConnection();
// for (let i = 1; i <= 200; i++) {
// const ext = await fpbx.getNextAvailableExtension();
// await fpbx.addExtension(ext, `Test User ${i}`)
// await conn.query("INSERT INTO discord_users (extension, discordId) VALUES (?, ?)", [ext, '289884287765839882']);
// log.debug(`Inserted extension ${ext} into discord_users table.`);
// }
// log.success("Inserted 200 extensions into discord_users table.");
// conn.release();
// } catch (err) {
// log.error(`Failed to insert extensions: ${err}`);
// }
// })();
// Startup
require("./migrations")(pool).then(() => {
log.success("Database migrations complete.");
log.info("Starting Discord client...");
client.login(process.env.DISCORD_TOKEN);
}).catch((err) => {
log.error(`Database migrations failed: ${err}`);
process.exit(1);
});