Files
PluralFlux-infra/src/bot.js
pieartsy df80eca0ec refactor: Removing then/catch from async/await calls (#22)
* refactored async/await for import helper to not also use then/catch

* added enum

* refactor webhookHelper and tests to not use then/catch

* changed docstring

* refactoring bot and tests to not use then/catch

* refactoring commands.js and tests to not use then/catch

* refactoring memberHelper.js and tests to not use then/catch

* removing then/catch from messageHelper.test.js

* fixed set up for commands tests

* edited bot to have top level main function

* one more test in commands.js, and removed console.error

* fixed typo in webhookHelper

* forgot to switch over some tests in bot.test and commands.test

* removed console.log from import helper

* put console.error in commands

* converted utils.js to not use then/catch

* tested utils checkImageFormatValidity

* removed jest-fetch-mock since it turns out I was just manually mocking it anyway

* refactored database to not use then/catch

* added dash to commands.js and test to pass

* added the remaining webhook tests

* changed utils to check for 10MB size not 1MB

* removed unnecessary try/catch from utils

* Simplify getWebhook to use .find() instead of foreach logic

* make memberCommand exit when error occurs with parseMemberCommand

* changed commands.js to not have user interaction within the catch

* updated console.error message in database.js

* made importHelper mock throw error instead of "resolve" error

* replaced "pk;" with "pf;" in test

* Got rid of unnecessary check for empty message from user (Fluxer doesn't allow this to happen)

Removed export of token

* getAllMembersInfo checks for fields.length

* added default case to memberCommandHandler to throw error if command is not recognized

* reversed check for valid proxy (was returning valid if the proxy existed and invalid if it didn't)

* pushes e.message instead of full error object to errors array in importHelper

* adjusted tests to properly use mockRejectedValue for async rejections

* changed getAllMembersInfo map to say `index` not `name` as it actually gets the index of a member and then the member object

* adjusted importHelper to properly test throwing of aggregate error

* revamped setting of expiration warning (moved to utils and changed logic, wrote tests)

---------

Co-authored-by: Aster Fialla <asterfialla@gmail.com>
2026-02-25 19:30:39 -05:00

99 lines
2.7 KiB
JavaScript

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";
import env from 'dotenv';
import {utils} from "./helpers/utils.js";
env.config({path: './.env'});
const token = process.env.FLUXER_BOT_TOKEN;
if (!token) {
console.error("Missing FLUXER_BOT_TOKEN environment variable.");
process.exit(1);
}
export const client = new Client({ intents: 0 });
client.on(Events.MessageCreate, async (message) => {
await handleMessageCreate(message);
});
/**
* Calls functions based off the contents of a message object.
*
* @async
* @param {Message} message - The message object
*
**/
export const 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) {
console.error(error);
}
}
client.on(Events.Ready, () => {
console.log(`Logged in as ${client.user?.username}`);
});
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);
export const login = async function() {
try {
await client.login(token);
// await db.check_connection();
} catch (err) {
console.error('Login failed:', err);
process.exit(1);
}
}
function main()
{
login();
}
main();