diff --git a/src/Client.ts b/src/Client.ts index 2bbd899..cb598d8 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -6,7 +6,7 @@ import Streamer from "./client/classes/Streamer"; import Statistics from "./client/classes/Statistics"; import { command } from "./client/commands"; import config from "./config"; -import { events } from "./client/events" +import events from "./client/events" import { funcs } from "./client/funcs"; import { messages } from "./client/messages"; @@ -20,7 +20,6 @@ GatewayIntents.add( export default class RadioClient extends Client { readonly commands: Collection; - readonly events = events; readonly funcs = funcs; readonly config = config; readonly messages = messages; @@ -52,47 +51,7 @@ export default class RadioClient extends Client { this.funcs.logger("Maintenance Mode", "Enabled"); this.config.maintenanceMode = true; - this.on("ready", () => { - this.events.ready.execute(this); - }); - - this.on("messageDelete", msg => { - this.events.messageDelete.execute(this, msg); - }); - - this.on("interactionCreate", interaction => { - this.events.interactionCreate.execute(this, interaction); - }); - - this.on("voiceStateUpdate", (oldState, newState) => { - this.events.voiceStateUpdate.execute(this, oldState, newState); - }); - - this.on("error", error => { - this.funcs.logger("Discord Client", "Error"); - console.error(error); - console.log(''); - }); - - process.on('SIGINT', () => { - this.events.SIGINT.execute(this); - }); - - process.on('SIGTERM', () => { - this.events.SIGTERM.execute(this); - }); - - process.on('uncaughtException', (error) => { - this.events.uncaughtException.execute(this, error); - }); - - process.on('exit', () => { - this.funcs.logger("Bot", "Stopping"); - }); - - process.on('warning', (warning) => { - this.events.warning.execute(this, warning); - }); + events(this); this.login(this.config.token).catch((err) => { this.funcs.logger("Discord Client", "Login Error"); diff --git a/src/client/events.ts b/src/client/events.ts index 811a92c..9da068b 100644 --- a/src/client/events.ts +++ b/src/client/events.ts @@ -1,3 +1,4 @@ +import RadioClient from "../Client" import interactionCreate from "./events/interactionCreate" import messageDelete from "./events/messageDelete" import ready from "./events/ready" @@ -7,6 +8,46 @@ import uncaughtException from "./events/uncaughtException" import voiceStateUpdate from "./events/voiceStateUpdate" import warning from "./events/warning" -export const events = { - interactionCreate, messageDelete, ready, SIGINT, SIGTERM, uncaughtException, voiceStateUpdate, warning +export default function events(client: RadioClient) { + client.on("ready", () => { + ready(client); + }); + + client.on("messageDelete", msg => { + messageDelete(client, msg); + }); + + client.on("interactionCreate", interaction => { + interactionCreate(client, interaction); + }); + + client.on("voiceStateUpdate", (oldState, newState) => { + voiceStateUpdate(client, oldState, newState); + }); + + client.on("error", error => { + client.funcs.logger("Discord Client", "Error"); + console.error(error); + console.log(''); + }); + + process.on('SIGINT', () => { + SIGINT(client); + }); + + process.on('SIGTERM', () => { + SIGTERM(client); + }); + + process.on('uncaughtException', (error) => { + uncaughtException(client, error); + }); + + process.on('exit', () => { + client.funcs.logger("Bot", "Stopping"); + }); + + process.on('warning', (error) => { + warning(client, error); + }); } diff --git a/src/client/events/SIGINT.ts b/src/client/events/SIGINT.ts index b3aa1cf..307ce12 100644 --- a/src/client/events/SIGINT.ts +++ b/src/client/events/SIGINT.ts @@ -1,17 +1,14 @@ import RadioClient from "../../Client"; -export default { - name: 'SIGINT', - execute(client: RadioClient) { - client.user?.setStatus('dnd'); +export default function SIGINT(client: RadioClient) { + client.user?.setStatus('dnd'); - client.streamer?.leave(client); - client.radio?.save(client); + client.streamer?.leave(client); + client.radio?.save(client); - setInterval(() => { - if(client.radio?.size == 0){ - process.exit(); - } - }, 500); - } + setInterval(() => { + if(client.radio?.size == 0){ + process.exit(); + } + }, 500); } diff --git a/src/client/events/SIGTERM.ts b/src/client/events/SIGTERM.ts index 7e03abb..c0e492d 100644 --- a/src/client/events/SIGTERM.ts +++ b/src/client/events/SIGTERM.ts @@ -1,8 +1,5 @@ import RadioClient from "../../Client"; -export default { - name: 'SIGTERM', - execute(client: RadioClient) { - process.emit('SIGINT'); - } +export default function SIGTERM(client: RadioClient) { + process.emit('SIGINT'); } diff --git a/src/client/events/interactionCreate.ts b/src/client/events/interactionCreate.ts index 2762e45..25387ca 100644 --- a/src/client/events/interactionCreate.ts +++ b/src/client/events/interactionCreate.ts @@ -1,53 +1,52 @@ -import { PermissionFlagsBits } from "discord.js"; +import { Interaction, PermissionFlagsBits } from "discord.js"; import RadioClient from "../../Client"; -export default { - name: 'interactionCreate', - async execute(client: RadioClient, interaction: any) { +export default function interactionCreate(client: RadioClient, interaction: Interaction) { + if(!(interaction.isButton()) && !(interaction.isChatInputCommand()) && !(interaction.isStringSelectMenu())) return; - if(client.config.maintenanceMode){ - return interaction.reply({ - content: client.messages.emojis["error"] + client.messages.maintenance, - ephemeral: true - }); - } - - const permissions = interaction.channel.permissionsFor(interaction.client.user); - if (!permissions.has(PermissionFlagsBits.ViewChannel)) return; - - if (!permissions.has(PermissionFlagsBits.EmbedLinks)) return interaction.reply({ - content: client.messages.emojis["error"] + client.messages.noPermsEmbed, + if(client.config.maintenanceMode){ + return interaction.reply({ + content: client.messages.emojis["error"] + client.messages.maintenance, ephemeral: true }); + } - if(interaction.isChatInputCommand()){ - const commandName = interaction.commandName; - const command = client.commands.get(commandName); - if (!command) return; + //@ts-ignore + const permissions = interaction.channel?.permissionsFor(interaction.client.user); + if (!permissions.has(PermissionFlagsBits.ViewChannel)) return; - try { - command.execute(interaction, client); - } catch (error) { - interaction.reply({ - content: client.messages.emojis["error"] + client.messages.runningCommandFailed, - ephemeral: true - }); - console.error(error); - } - } else if (interaction.isStringSelectMenu() || interaction.isButton()){ - const commandName = interaction.customId; - const command = client.commands.get(commandName); - if (!command) return; + if (!permissions.has(PermissionFlagsBits.EmbedLinks)) return interaction.reply({ + content: client.messages.emojis["error"] + client.messages.noPermsEmbed, + ephemeral: true + }); - try { - command.execute(interaction, client, command); - } catch (error) { - interaction.reply({ - content: client.messages.emojis["error"] + client.messages.runningCommandFailed, - ephemeral: true - }); - console.error(error); - } + if(interaction.isChatInputCommand()){ + const commandName = interaction.commandName; + const command = client.commands.get(commandName); + if (!command) return; + + try { + command.execute(interaction, client); + } catch (error) { + interaction.reply({ + content: client.messages.emojis["error"] + client.messages.runningCommandFailed, + ephemeral: true + }); + console.error(error); + } + } else if (interaction.isStringSelectMenu() || interaction.isButton()){ + const commandName = interaction.customId; + const command = client.commands.get(commandName); + if (!command) return; + + try { + command.execute(interaction, client, command); + } catch (error) { + interaction.reply({ + content: client.messages.emojis["error"] + client.messages.runningCommandFailed, + ephemeral: true + }); + console.error(error); } } } diff --git a/src/client/events/messageDelete.ts b/src/client/events/messageDelete.ts index 89584c6..3efbbfd 100644 --- a/src/client/events/messageDelete.ts +++ b/src/client/events/messageDelete.ts @@ -1,14 +1,11 @@ import { Message, PartialMessage } from "discord.js"; import RadioClient from "../../Client"; -export default { - name: 'messageDelete', - async execute(client: RadioClient, msg: Message | PartialMessage) { - if(!msg.author?.bot || !msg.guild) return; - const radio = client.radio?.get(msg.guild.id); - if(!radio) return; - if(!radio.message) return; - if(msg.id != radio.message.id) return; - radio.message = null; - } +export default function messageDelete(client: RadioClient, msg: Message | PartialMessage){ + if(!msg.author?.bot || !msg.guild) return; + const radio = client.radio?.get(msg.guild.id); + if(!radio) return; + if(!radio.message) return; + if(msg.id != radio.message.id) return; + radio.message = null; } diff --git a/src/client/events/ready.ts b/src/client/events/ready.ts index efd0683..47e6865 100644 --- a/src/client/events/ready.ts +++ b/src/client/events/ready.ts @@ -6,82 +6,77 @@ import Streamer from "../classes/Streamer"; import Statistics from "../classes/Statistics"; import commands from "../commands"; -export default { - name: 'ready', - async execute(client: RadioClient) { +export default async function ready(client: RadioClient) { + client.funcs.logger("Bot", "Ready"); - client.funcs.logger("Bot", "Ready"); + /*DATASTORE*/ + client.funcs.logger('Datastore', 'Initialize'); + client.datastore = new Datastore(); - /*DATASTORE*/ - client.funcs.logger('Datastore', 'Initialize'); - client.datastore = new Datastore(); + client.datastore.map.forEach((datastore: { guild: { id: string; name: string; }; }) => { + client.funcs.logger('Datastore', datastore.guild.id + " / " + datastore.guild.name); + }); - client.datastore.map.forEach((datastore: { guild: { id: string; name: string; }; }) => { - client.funcs.logger('Datastore', datastore.guild.id + " / " + datastore.guild.name); - }); + client.funcs.logger('Datastore', 'Ready'); - client.funcs.logger('Datastore', 'Ready'); - - /*DEVELOPERS*/ - client.developers = ""; - let user : any= ""; - for (let i = 0; i < client.config.devId.length; i++) { - user = await client.users.fetch(client.config.devId[i]); - client.funcs.logger('Developers', user.tag); - if (i == client.config.devId.length - 1) { - client.developers += user.tag; - } else { - client.developers += user.tag + " & "; - } + /*DEVELOPERS*/ + client.developers = ""; + let user : any= ""; + for (let i = 0; i < client.config.devId.length; i++) { + user = await client.users.fetch(client.config.devId[i]); + client.funcs.logger('Developers', user.tag); + if (i == client.config.devId.length - 1) { + client.developers += user.tag; + } else { + client.developers += user.tag + " & "; } - - /*STATIONS*/ - client.stations = new Stations(); - - await client.stations.fetch({ - url: client.config.stationslistUrl, - show: true - }); - - client.streamer = new Streamer(); - client.streamer.init(client); - - if(!client.stations) { - client.user?.setStatus('dnd'); - } - - /*GUILDS*/ - client.funcs.logger('Guilds', 'Started fetching list'); - - let guilds = await client.guilds.fetch(); - guilds.forEach((guild: { id: string; name: string; }) => { - client.funcs.logger('Guilds', guild.id + " / " + guild.name); - }); - - client.funcs.logger('Guilds', 'Successfully fetched list'); - - /*STATISTICS*/ - client.statistics = new Statistics(); - client.statistics.calculateGlobal(client); - - /*COMMANDS*/ - commands.execute(client); - - /*RADIO*/ - client.radio = new Radio(); - - setTimeout(function () { - /*RESTORE RADIOS*/ - client.radio?.restore(client, guilds); - }, 5000); - - setTimeout(function () { - if(client.stations) { - /*MAINTENANCE MODE*/ - client.funcs.logger("Maintenance Mode", "Disabled"); - client.config.maintenanceMode = false; - } - }, 10000); - } + + /*STATIONS*/ + client.stations = new Stations(); + + await client.stations.fetch({ + url: client.config.stationslistUrl, + show: true + }); + + client.streamer = new Streamer(); + client.streamer.init(client); + + if(!client.stations) { + client.user?.setStatus('dnd'); + } + + /*GUILDS*/ + client.funcs.logger('Guilds', 'Started fetching list'); + + let guilds = await client.guilds.fetch(); + guilds.forEach((guild: { id: string; name: string; }) => { + client.funcs.logger('Guilds', guild.id + " / " + guild.name); + }); + + client.funcs.logger('Guilds', 'Successfully fetched list'); + + /*STATISTICS*/ + client.statistics = new Statistics(); + client.statistics.calculateGlobal(client); + + /*COMMANDS*/ + commands.execute(client); + + /*RADIO*/ + client.radio = new Radio(); + + setTimeout(function () { + /*RESTORE RADIOS*/ + client.radio?.restore(client, guilds); + }, 5000); + + setTimeout(function () { + if(client.stations) { + /*MAINTENANCE MODE*/ + client.funcs.logger("Maintenance Mode", "Disabled"); + client.config.maintenanceMode = false; + } + }, 10000); } diff --git a/src/client/events/uncaughtException.ts b/src/client/events/uncaughtException.ts index 3f22396..b0d8536 100644 --- a/src/client/events/uncaughtException.ts +++ b/src/client/events/uncaughtException.ts @@ -1,13 +1,10 @@ import RadioClient from "../../Client"; -export default { - name: 'uncaughtException', - execute(client: RadioClient, error: any) { - client.funcs.logger("Error"); - console.log(error.stack); - console.log(''); +export default function uncaughtException(client: RadioClient, error: Error) { + client.funcs.logger("Error"); + console.log(error.stack); + console.log(''); - if(error.name == "DiscordAPIError" && error.message == "Unknown interaction") return; - process.emit('SIGINT'); - } + if(error.name == "DiscordAPIError" && error.message == "Unknown interaction") return; + process.emit('SIGINT'); } diff --git a/src/client/events/voiceStateUpdate.ts b/src/client/events/voiceStateUpdate.ts index 8c7600c..cf5fbb4 100644 --- a/src/client/events/voiceStateUpdate.ts +++ b/src/client/events/voiceStateUpdate.ts @@ -5,65 +5,61 @@ const { joinVoiceChannel } = require("@discordjs/voice"); -export default { - name: "voiceStateUpdate", - async execute(client: RadioClient, oldState: VoiceState, newState: VoiceState) { - if (oldState.channel === null) return; - let change = false; - const radio = client.radio?.get(newState.guild.id); - if (!radio) return; +export default async function voiceStateUpdate(client: RadioClient, oldState: VoiceState, newState: VoiceState) { + if (oldState.channel === null) return; + let change = false; + const radio = client.radio?.get(newState.guild.id); + if (!radio) return; - if (newState.member?.id === client.user?.id && oldState.member?.id === client.user?.id) { + if (newState.member?.id === client.user?.id && oldState.member?.id === client.user?.id) { - if (newState.channel === null) { + if (newState.channel === null) { + client.statistics?.update(client, newState.guild, radio); + radio.connection?.destroy(); + radio.message?.delete(); + client.funcs.logger('Radio', newState.guild.id + " / " + 'Stop'); + return client.radio?.delete(newState.guild.id); + } + + const newPermissions = newState.channel.permissionsFor(newState.client.user); + if (!newPermissions?.has(PermissionFlagsBits.Connect) || !newPermissions?.has(PermissionFlagsBits.Speak) || !newPermissions?.has(PermissionFlagsBits.ViewChannel)) { + try { + setTimeout( + async () => ( + radio.connection = joinVoiceChannel({ + channelId: oldState.channel?.id, + guildId: oldState.channel?.guild.id, + adapterCreator: oldState.channel?.guild.voiceAdapterCreator + }) + ), + 1000 + ); + } catch (error) { client.statistics?.update(client, newState.guild, radio); radio.connection?.destroy(); radio.message?.delete(); client.funcs.logger('Radio', newState.guild.id + " / " + 'Stop'); - return client.radio?.delete(newState.guild.id); + client.radio?.delete(oldState.guild.id); } + return; + } + if (newState.channel !== radio.voiceChannel) { + change = true; + radio.voiceChannel = newState.channel; + radio.connection = getVoiceConnection(newState.channel.guild.id); - const newPermissions = newState.channel.permissionsFor(newState.client.user); - if (!newPermissions?.has(PermissionFlagsBits.Connect) || !newPermissions?.has(PermissionFlagsBits.Speak) || !newPermissions?.has(PermissionFlagsBits.ViewChannel)) { - try { - setTimeout( - async () => ( - radio.connection = joinVoiceChannel({ - channelId: oldState.channel?.id, - guildId: oldState.channel?.guild.id, - adapterCreator: oldState.channel?.guild.voiceAdapterCreator - }) - //radio.connection = await oldState.channel.join() - ), - 1000 - ); - } catch (error) { - client.statistics?.update(client, newState.guild, radio); - radio.connection?.destroy(); - radio.message?.delete(); - client.funcs.logger('Radio', newState.guild.id + " / " + 'Stop'); - client.radio?.delete(oldState.guild.id); - } - return; - } - if (newState.channel !== radio.voiceChannel) { - change = true; - radio.voiceChannel = newState.channel; - radio.connection = getVoiceConnection(newState.channel.guild.id); - //radio.connection = await newState.channel.join(); - } } - if ((oldState.channel.members.filter(member => !member.user.bot).size === 0 && oldState.channel === radio.voiceChannel) || change) { - setTimeout(() => { - if (!radio || !radio.connection || !radio.connection === null) return; - if (radio.voiceChannel.members.filter((member: { user: { bot: any; }; }) => !member.user.bot).size === 0) { - client.statistics?.update(client, newState.guild, radio); - radio.connection?.destroy(); - radio.message?.delete(); - client.funcs.logger('Radio', newState.guild.id + " / " + 'Stop'); - client.radio?.delete(newState.guild.id); - } - }, 5000); - } - }, + } + if ((oldState.channel.members.filter(member => !member.user.bot).size === 0 && oldState.channel === radio.voiceChannel) || change) { + setTimeout(() => { + if (!radio || !radio.connection || !radio.connection === null) return; + if (radio.voiceChannel.members.filter((member: { user: { bot: any; }; }) => !member.user.bot).size === 0) { + client.statistics?.update(client, newState.guild, radio); + radio.connection?.destroy(); + radio.message?.delete(); + client.funcs.logger('Radio', newState.guild.id + " / " + 'Stop'); + client.radio?.delete(newState.guild.id); + } + }, 5000); + } }; diff --git a/src/client/events/warning.ts b/src/client/events/warning.ts index 4b8dd8e..431cf03 100644 --- a/src/client/events/warning.ts +++ b/src/client/events/warning.ts @@ -1,14 +1,11 @@ import RadioClient from "../../Client"; -export default { - name: 'warning', - execute(client: RadioClient, warning: Error) { - if(warning.name == "ExperimentalWarning" && warning.message.startsWith("stream/web")) return; +export default function warning(client: RadioClient, warning: Error) { + if(warning.name == "ExperimentalWarning" && warning.message.startsWith("stream/web")) return; - client.funcs.logger("Warning"); - console.warn(warning.name); - console.warn(warning.message); - console.warn(warning.stack); - console.log(''); - } + client.funcs.logger("Warning"); + console.warn(warning.name); + console.warn(warning.message); + console.warn(warning.stack); + console.log(''); }