Files
PluralFlux-infra/src/bot.js

100 lines
2.8 KiB
JavaScript
Raw Normal View History

2026-02-23 10:38:04 -05:00
import { Client, Events, Message } from '@fluxerjs/core';
import { messageHelper } from "./helpers/messageHelper.js";
import {enums} from "./enums.js";
import {commands} from "./commands.js";
import {webhookHelper} from "./helpers/webhookHelper.js";
2026-02-23 12:27:25 -05:00
import env from 'dotenv';
2026-02-22 09:54:01 -05:00
import {utils} from "./helpers/utils.js";
2026-02-16 11:07:43 -05:00
2026-02-23 12:27:25 -05:00
env.config({path: './.env'});
2026-02-23 12:27:25 -05:00
export const token = process.env.FLUXER_BOT_TOKEN;
if (!token) {
console.error("Missing FLUXER_BOT_TOKEN environment variable.");
process.exit(1);
}
2026-02-23 10:38:04 -05:00
export const client = new Client({ intents: 0 });
2026-02-14 11:37:04 -05:00
client.on(Events.MessageCreate, async (message) => {
2026-02-23 10:38:04 -05:00
await handleMessageCreate(message);
});
2026-02-23 10:38:04 -05:00
/**
* Calls functions based off the contents of a message object.
*
* @async
* @param {Message} message - The message object
*
**/
export const handleMessageCreate = async function(message) {
try {
2026-02-14 11:37:04 -05:00
// Parse command and arguments
const content = message.content.trim();
2026-02-23 10:38:04 -05:00
// Ignore bots and messages without content
if (message.author.bot || content.length === 0) return;
// If message doesn't start with the bot prefix, it could still be a message with a proxy tag. If it's not, return.
2026-02-14 11:37:04 -05:00
if (!content.startsWith(messageHelper.prefix)) {
return await webhookHelper.sendMessageAsMember(client, message);
2026-02-14 11:37:04 -05:00
}
2026-02-14 11:37:04 -05:00
const commandName = content.slice(messageHelper.prefix.length).split(" ")[0];
2026-02-14 11:37:04 -05:00
// If there's no command name (ie just the prefix)
if (!commandName) return await message.reply(enums.help.SHORT_DESC_PLURALFLUX);
2026-02-14 11:37:04 -05:00
const args = messageHelper.parseCommandArgs(content, commandName);
let command = commands.commandsMap.get(commandName)
if (!command) {
const commandFromAlias = commands.aliasesMap.get(commandName);
command = commandFromAlias ? commands.commandsMap.get(commandFromAlias.command) : null;
}
if (command) {
await command.execute(message, args);
}
else {
2026-02-19 19:32:37 -05:00
await message.reply(enums.err.COMMAND_NOT_RECOGNIZED);
}
}
catch(error) {
2026-02-16 11:07:43 -05:00
console.error(error);
}
2026-02-23 10:38:04 -05:00
}
2026-02-14 11:37:04 -05:00
client.on(Events.Ready, () => {
console.log(`Logged in as ${client.user?.username}`);
});
let guildCount = 0;
client.on(Events.GuildCreate, () => {
guildCount++;
2026-02-21 23:05:15 -05:00
debouncePrintGuilds();
});
function printGuilds() {
console.log(`Serving ${client.guilds.size} guild(s)`);
}
2026-02-21 23:05:15 -05:00
const debouncePrintGuilds = utils.debounce(printGuilds, 2000);
export const debounceLogin = utils.debounce(client.login, 60000);
export const login = async function() {
2026-02-23 10:38:04 -05:00
try {
await client.login(token);
2026-02-23 10:38:04 -05:00
// await db.check_connection();
} catch (err) {
console.error('Login failed:', err);
process.exit(1);
}
}
function main()
{
login();
}
main();