diff --git a/bot.js b/bot.js index 97e4f27..592bb6f 100644 --- a/bot.js +++ b/bot.js @@ -1,11 +1,7 @@ -import { Client, GatewayDispatchEvents } from "@discordjs/core"; -import { REST } from "@discordjs/rest"; -import { WebSocketManager } from "@discordjs/ws"; -import { db } from './sequelize.js'; -import { webhookHelper } from "./helpers/webhookHelper.js"; +import { Client, Events, GatewayOpcodes } from '@fluxerjs/core'; import { messageHelper } from "./helpers/messageHelper.js"; -import { memberHelper } from "./helpers/memberHelper.js"; import {enums} from "./enums.js"; +import {commands} from "./commands.js"; const token = process.env.FLUXER_BOT_TOKEN; @@ -14,65 +10,61 @@ if (!token) { process.exit(1); } -const rest = new REST({ - api: "https://api.fluxer.app", - version: "1", -}).setToken(token); +const BOT_STATUS = process.env.FLUXER_BOT_STATUS ?? 'online'; +const client = new Client({ intents: 0 }); -const gateway = new WebSocketManager({ - token, - intents: 0, // Fluxer has no intents yet - rest, - version: "1", -}); - -export const client = new Client({ rest, gateway }); - -let pluralFluxName = "PluralFlux"; -let pluralFluxDiscriminator = "8677"; - -client.on(GatewayDispatchEvents.MessageCreate, async ({ api, data }) => { +client.on(Events.MessageCreate, async (message) => { try { - if (data.webhook_id) { - return; - } else if (data.author.username === pluralFluxName && data.author.discriminator === pluralFluxDiscriminator) { - return; - } else if (data.content.startsWith(messageHelper.prefix)) { + // Ignore bots and messages without content + if (message.author.bot || !message.content) return; - const commandName = data.content.slice(messageHelper.prefix.length).split(" ")[0]; - const args = messageHelper.parseCommandArgs(data.content, commandName); - if (!commandName) { - return await api.channels.createMessage(data.channel_id, {content: enums.help.PLURALFLUX}); - } - switch (commandName) { - case 'm': - case 'member': - const attachment = data.attachments[0] ?? null; - const reply = await memberHelper.parseMemberCommand(data.author.id, args, attachment); - return await api.channels.createMessage(data.channel_id, {content: reply}); - case 'help': - return await api.channels.createMessage(data.channel_id, {content: enums.help.PLURALFLUX}); - default: - return await api.channels.createMessage(data.channel_id, {content: enums.err.NO_SUCH_COMMAND}); - } - const proxyMatch = await messageHelper.parseProxyTags(data.author.id, data.content); - if (!proxyMatch.proxy) { - return; - } - const member = await memberHelper.getMemberByProxy(data.author.id, proxyMatch.proxy); - await webhookHelper.replaceMessage(api, data, proxyMatch.message, member); + // 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 (!content.startsWith(messageHelper.prefix)) { + // wait until webhooks exist lol + //const successful = await webhookHelper.sendMessageAsMember(message, content); + // if (!successful) return; } + + 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); + + const command = commands.get(commandName); + if (command || command.alias) { + try { + await command.execute(message, client, args); + } catch (err) { + console.error(`Error executing ${commandName}:`, err); + await message.reply('An error occurred while running that command.').catch(() => { + }); + } + } + } catch(error) { - return await api.channels.createMessage(data.channel_id, {content: error}); + return await message.reply(error); } }); -client.on(GatewayDispatchEvents.Ready, async ({data}) => { - console.log(`Logged in as ${data.user.username}#${data.user.discriminator}`); - pluralFluxName = data.user.username; - pluralFluxDiscriminator = data.user.discriminator; - await db.check_connection(); +client.on(Events.Ready, () => { + console.log(`Logged in as ${client.user?.username}`); + console.log(`Serving ${client.guilds.size} guild(s)`); + const status = ['online', 'idle', 'dnd', 'invisible'].includes(BOT_STATUS) ? BOT_STATUS : 'online'; + client.sendToGateway(0, { + op: GatewayOpcodes.PresenceUpdate, + d: { status }, + }); }); -await gateway.connect(); \ No newline at end of file +try { + await client.login(token); + console.log('Gateway connected'); +} catch (err) { + console.error('Login failed:', err); + process.exit(1); +} \ No newline at end of file