LinkCloudDev/server/Utility/discordClient.js

87 lines
3.4 KiB
JavaScript
Raw Normal View History

2024-08-02 22:55:55 +00:00
import { Client, Collection, Events, GatewayIntentBits, Partials } from 'discord.js'
import infoCommand from '../commands/utility/info.js'
import connectLinkshell from '../commands/utility/connectLinkshell.js'
import joinlinkcloud from '../commands/utility/joinlinkcloud.js'
import { getLSFromGuildId, getLSModel } from "../Utility/lsModel.js"
import linkcloudstatus from '../commands/utility/linkcloudstatus.js'
import { event } from "./eventHandler.js";
import { Err, Log, Debug, Warn } from '../Utility/loggerUtility.js'
import createEcho from '../commands/utility/createEcho.js'
import fsConfig from '../config.json' assert { type: "json" };
const token = fsConfig.token
const servers = {
retail: fsConfig.retail_servers,
private: fsConfig.private_servers
}
export const client = new Client({ intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
partials: [
Partials.Channel,
Partials.Message
]
});
const outboundQueue = []
export const botSetup = () => {
client.commands = new Collection();
client.commands.set(infoCommand.data.name, infoCommand);
client.commands.set(connectLinkshell.data.name, connectLinkshell);
client.commands.set(joinlinkcloud.data.name, joinlinkcloud)
client.commands.set(linkcloudstatus.data.name, linkcloudstatus)
client.commands.set(createEcho.data.name, createEcho)
client.once(Events.ClientReady, readyClient => {
Log(`Discord Bot Ready! Logged in as ${readyClient.user.tag}`);
});
client.login(token);
}
client.on(Events.MessageCreate, async message => {
if(!message.guildId) return;
const linkshell = getLSFromGuildId(message.guildId)
const lsChannels = linkshell.channels
if (linkshell) {
for(const channel of lsChannels) {
if(channel.channelId == message.channelId && !message?.author?.bot) {
const guild = client.guilds.cache.get(message.guildId)
let nickname = false
guild.members.cache.forEach((member) => {
if (member.user.id == message.author.id) {
nickname = member.nickname
}
})
const authorName = nickname ? nickname : message?.author?.globalName
//TODO Fix emojis
const messagePacket = {platform: linkshell.ffxiver, server: linkshell.server, from: authorName, message: message.content, lsName: linkshell.name}
event.emit('NEW_DISCORD_ECHO_RECEIVED', messagePacket)
}
}
} else {
Warn('Unable to find linkshell counterpart for this guild.')
}
});
client.on(Events.InteractionCreate, async interaction => {
if (interaction.isModalSubmit()) {
event.emit('on_' + interaction.customId, interaction)
}
if (!interaction.isChatInputCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
Err(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
} else {
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
}
});