forked from PluralFlux/PluralFlux
refactoring to use fluxer.js
This commit is contained in:
106
bot.js
106
bot.js
@@ -1,11 +1,7 @@
|
|||||||
import { Client, GatewayDispatchEvents } from "@discordjs/core";
|
import { Client, Events, GatewayOpcodes } from '@fluxerjs/core';
|
||||||
import { REST } from "@discordjs/rest";
|
|
||||||
import { WebSocketManager } from "@discordjs/ws";
|
|
||||||
import { db } from './sequelize.js';
|
|
||||||
import { webhookHelper } from "./helpers/webhookHelper.js";
|
|
||||||
import { messageHelper } from "./helpers/messageHelper.js";
|
import { messageHelper } from "./helpers/messageHelper.js";
|
||||||
import { memberHelper } from "./helpers/memberHelper.js";
|
|
||||||
import {enums} from "./enums.js";
|
import {enums} from "./enums.js";
|
||||||
|
import {commands} from "./commands.js";
|
||||||
|
|
||||||
const token = process.env.FLUXER_BOT_TOKEN;
|
const token = process.env.FLUXER_BOT_TOKEN;
|
||||||
|
|
||||||
@@ -14,65 +10,61 @@ if (!token) {
|
|||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
const rest = new REST({
|
const BOT_STATUS = process.env.FLUXER_BOT_STATUS ?? 'online';
|
||||||
api: "https://api.fluxer.app",
|
const client = new Client({ intents: 0 });
|
||||||
version: "1",
|
|
||||||
}).setToken(token);
|
|
||||||
|
|
||||||
const gateway = new WebSocketManager({
|
client.on(Events.MessageCreate, async (message) => {
|
||||||
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 }) => {
|
|
||||||
try {
|
try {
|
||||||
if (data.webhook_id) {
|
// Ignore bots and messages without content
|
||||||
return;
|
if (message.author.bot || !message.content) return;
|
||||||
} else if (data.author.username === pluralFluxName && data.author.discriminator === pluralFluxDiscriminator) {
|
|
||||||
return;
|
|
||||||
} else if (data.content.startsWith(messageHelper.prefix)) {
|
|
||||||
|
|
||||||
const commandName = data.content.slice(messageHelper.prefix.length).split(" ")[0];
|
// Parse command and arguments
|
||||||
const args = messageHelper.parseCommandArgs(data.content, commandName);
|
const content = message.content.trim();
|
||||||
if (!commandName) {
|
|
||||||
return await api.channels.createMessage(data.channel_id, {content: enums.help.PLURALFLUX});
|
// If message doesn't start with the bot prefix, it could still be a message with a proxy tag
|
||||||
}
|
if (!content.startsWith(messageHelper.prefix)) {
|
||||||
switch (commandName) {
|
// wait until webhooks exist lol
|
||||||
case 'm':
|
//const successful = await webhookHelper.sendMessageAsMember(message, content);
|
||||||
case 'member':
|
// if (!successful) return;
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
catch(error) {
|
||||||
return await api.channels.createMessage(data.channel_id, {content: error});
|
return await message.reply(error);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on(GatewayDispatchEvents.Ready, async ({data}) => {
|
client.on(Events.Ready, () => {
|
||||||
console.log(`Logged in as ${data.user.username}#${data.user.discriminator}`);
|
console.log(`Logged in as ${client.user?.username}`);
|
||||||
pluralFluxName = data.user.username;
|
console.log(`Serving ${client.guilds.size} guild(s)`);
|
||||||
pluralFluxDiscriminator = data.user.discriminator;
|
const status = ['online', 'idle', 'dnd', 'invisible'].includes(BOT_STATUS) ? BOT_STATUS : 'online';
|
||||||
await db.check_connection();
|
client.sendToGateway(0, {
|
||||||
|
op: GatewayOpcodes.PresenceUpdate,
|
||||||
|
d: { status },
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
await gateway.connect();
|
try {
|
||||||
|
await client.login(token);
|
||||||
|
console.log('Gateway connected');
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Login failed:', err);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user