forked from PluralFlux/PluralFlux
108 lines
3.4 KiB
JavaScript
108 lines
3.4 KiB
JavaScript
const {Client, Events, Message} = require('@fluxerjs/core');
|
|
const {messageHelper} = require("./helpers/messageHelper.js");
|
|
const {enums} = require("./enums.js");
|
|
const {commands} = require("./commands.js");
|
|
const {webhookHelper} = require("./helpers/webhookHelper.js");
|
|
const env = require('dotenv');
|
|
const {utils} = require("./helpers/utils.js");
|
|
const { AppDataSource } = require("../database/data-source");
|
|
|
|
env.config();
|
|
|
|
const token = process.env.FLUXER_BOT_TOKEN;
|
|
const debug = process.env.debug;
|
|
|
|
if (!token) {
|
|
console.error("Missing FLUXER_BOT_TOKEN environment variable.");
|
|
process.exit(1);
|
|
}
|
|
|
|
client = new Client({ intents: 0 });
|
|
|
|
module.exports.client = client;
|
|
|
|
client.on(Events.MessageCreate, async (message) => {
|
|
await module.exports.handleMessageCreate(message);
|
|
});
|
|
|
|
/**
|
|
* Calls functions based off the contents of a message object.
|
|
*
|
|
* @async
|
|
* @param {Message} message - The message object
|
|
*
|
|
**/
|
|
module.exports.handleMessageCreate = async function(message) {
|
|
try {
|
|
// Ignore bots
|
|
if (message.author.bot) return;
|
|
// Parse command and arguments
|
|
const content = message.content.trim();
|
|
// If message doesn't start with the bot prefix, it could still be a message with a proxy tag. If it's not, return.
|
|
if (!content.startsWith(messageHelper.prefix)) {
|
|
return await webhookHelper.sendMessageAsMember(client, message);
|
|
}
|
|
|
|
const commandName = content.slice(messageHelper.prefix.length).split(" ")[0];
|
|
|
|
// If there's no command name (ie just the prefix)
|
|
if (!commandName) return await message.reply(enums.help.SHORT_DESC_PLURALFLUX);
|
|
|
|
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 {
|
|
await message.reply(enums.err.COMMAND_NOT_RECOGNIZED);
|
|
}
|
|
}
|
|
catch(error) {
|
|
if(debug){console.error("An error occurred at unix timestamp " + Date.now() + "while processing the command: " + message + " with error:" + error);}
|
|
else{console.error(error);}
|
|
process.exit(2); //need this for now just to make sure the bot continues to restart on errors, since it would seem that fluxer.js doesn't define custom error types. TODO: map out some exit codes
|
|
}
|
|
}
|
|
|
|
client.on(Events.Ready, () => {
|
|
console.log(`Logged in as ${client.user?.username}`);
|
|
if(debug){console.log(Date.now() + `: Currently running in debug mode!`)}
|
|
});
|
|
|
|
let guildCount = 0;
|
|
client.on(Events.GuildCreate, () => {
|
|
guildCount++;
|
|
debouncePrintGuilds();
|
|
});
|
|
|
|
function printGuilds() {
|
|
console.log(`Serving ${client.guilds.size} guild(s)`);
|
|
}
|
|
|
|
const debouncePrintGuilds = utils.debounce(printGuilds, 2000);
|
|
// export const debounceLogin = utils.debounce(client.login, 60000);
|
|
|
|
module.exports.login = async function() {
|
|
try {
|
|
if (!AppDataSource.isInitialized) {
|
|
await AppDataSource.initialize();
|
|
}
|
|
await client.login(token);
|
|
} catch (err) {
|
|
console.error('Login failed:', err);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
function main()
|
|
{
|
|
exports.login();
|
|
}
|
|
|
|
main(); |