Simplify events

This commit is contained in:
Christer Warén 2023-06-06 03:41:04 +03:00
parent 348ac90cba
commit 87cf4b62c8
10 changed files with 232 additions and 257 deletions

View File

@ -6,7 +6,7 @@ import Streamer from "./client/classes/Streamer";
import Statistics from "./client/classes/Statistics"; import Statistics from "./client/classes/Statistics";
import { command } from "./client/commands"; import { command } from "./client/commands";
import config from "./config"; import config from "./config";
import { events } from "./client/events" import events from "./client/events"
import { funcs } from "./client/funcs"; import { funcs } from "./client/funcs";
import { messages } from "./client/messages"; import { messages } from "./client/messages";
@ -20,7 +20,6 @@ GatewayIntents.add(
export default class RadioClient extends Client { export default class RadioClient extends Client {
readonly commands: Collection<string, command>; readonly commands: Collection<string, command>;
readonly events = events;
readonly funcs = funcs; readonly funcs = funcs;
readonly config = config; readonly config = config;
readonly messages = messages; readonly messages = messages;
@ -52,47 +51,7 @@ export default class RadioClient extends Client {
this.funcs.logger("Maintenance Mode", "Enabled"); this.funcs.logger("Maintenance Mode", "Enabled");
this.config.maintenanceMode = true; this.config.maintenanceMode = true;
this.on("ready", () => { events(this);
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);
});
this.login(this.config.token).catch((err) => { this.login(this.config.token).catch((err) => {
this.funcs.logger("Discord Client", "Login Error"); this.funcs.logger("Discord Client", "Login Error");

View File

@ -1,3 +1,4 @@
import RadioClient from "../Client"
import interactionCreate from "./events/interactionCreate" import interactionCreate from "./events/interactionCreate"
import messageDelete from "./events/messageDelete" import messageDelete from "./events/messageDelete"
import ready from "./events/ready" import ready from "./events/ready"
@ -7,6 +8,46 @@ import uncaughtException from "./events/uncaughtException"
import voiceStateUpdate from "./events/voiceStateUpdate" import voiceStateUpdate from "./events/voiceStateUpdate"
import warning from "./events/warning" import warning from "./events/warning"
export const events = { export default function events(client: RadioClient) {
interactionCreate, messageDelete, ready, SIGINT, SIGTERM, uncaughtException, voiceStateUpdate, warning 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);
});
} }

View File

@ -1,8 +1,6 @@
import RadioClient from "../../Client"; import RadioClient from "../../Client";
export default { export default function SIGINT(client: RadioClient) {
name: 'SIGINT',
execute(client: RadioClient) {
client.user?.setStatus('dnd'); client.user?.setStatus('dnd');
client.streamer?.leave(client); client.streamer?.leave(client);
@ -13,5 +11,4 @@ export default {
process.exit(); process.exit();
} }
}, 500); }, 500);
}
} }

View File

@ -1,8 +1,5 @@
import RadioClient from "../../Client"; import RadioClient from "../../Client";
export default { export default function SIGTERM(client: RadioClient) {
name: 'SIGTERM',
execute(client: RadioClient) {
process.emit('SIGINT'); process.emit('SIGINT');
}
} }

View File

@ -1,9 +1,8 @@
import { PermissionFlagsBits } from "discord.js"; import { Interaction, PermissionFlagsBits } from "discord.js";
import RadioClient from "../../Client"; import RadioClient from "../../Client";
export default { export default function interactionCreate(client: RadioClient, interaction: Interaction) {
name: 'interactionCreate', if(!(interaction.isButton()) && !(interaction.isChatInputCommand()) && !(interaction.isStringSelectMenu())) return;
async execute(client: RadioClient, interaction: any) {
if(client.config.maintenanceMode){ if(client.config.maintenanceMode){
return interaction.reply({ return interaction.reply({
@ -12,7 +11,8 @@ export default {
}); });
} }
const permissions = interaction.channel.permissionsFor(interaction.client.user); //@ts-ignore
const permissions = interaction.channel?.permissionsFor(interaction.client.user);
if (!permissions.has(PermissionFlagsBits.ViewChannel)) return; if (!permissions.has(PermissionFlagsBits.ViewChannel)) return;
if (!permissions.has(PermissionFlagsBits.EmbedLinks)) return interaction.reply({ if (!permissions.has(PermissionFlagsBits.EmbedLinks)) return interaction.reply({
@ -49,5 +49,4 @@ export default {
console.error(error); console.error(error);
} }
} }
}
} }

View File

@ -1,14 +1,11 @@
import { Message, PartialMessage } from "discord.js"; import { Message, PartialMessage } from "discord.js";
import RadioClient from "../../Client"; import RadioClient from "../../Client";
export default { export default function messageDelete(client: RadioClient, msg: Message | PartialMessage){
name: 'messageDelete',
async execute(client: RadioClient, msg: Message | PartialMessage) {
if(!msg.author?.bot || !msg.guild) return; if(!msg.author?.bot || !msg.guild) return;
const radio = client.radio?.get(msg.guild.id); const radio = client.radio?.get(msg.guild.id);
if(!radio) return; if(!radio) return;
if(!radio.message) return; if(!radio.message) return;
if(msg.id != radio.message.id) return; if(msg.id != radio.message.id) return;
radio.message = null; radio.message = null;
}
} }

View File

@ -6,10 +6,7 @@ import Streamer from "../classes/Streamer";
import Statistics from "../classes/Statistics"; import Statistics from "../classes/Statistics";
import commands from "../commands"; import commands from "../commands";
export default { export default async function ready(client: RadioClient) {
name: 'ready',
async execute(client: RadioClient) {
client.funcs.logger("Bot", "Ready"); client.funcs.logger("Bot", "Ready");
/*DATASTORE*/ /*DATASTORE*/
@ -82,6 +79,4 @@ export default {
client.config.maintenanceMode = false; client.config.maintenanceMode = false;
} }
}, 10000); }, 10000);
}
} }

View File

@ -1,13 +1,10 @@
import RadioClient from "../../Client"; import RadioClient from "../../Client";
export default { export default function uncaughtException(client: RadioClient, error: Error) {
name: 'uncaughtException',
execute(client: RadioClient, error: any) {
client.funcs.logger("Error"); client.funcs.logger("Error");
console.log(error.stack); console.log(error.stack);
console.log(''); console.log('');
if(error.name == "DiscordAPIError" && error.message == "Unknown interaction") return; if(error.name == "DiscordAPIError" && error.message == "Unknown interaction") return;
process.emit('SIGINT'); process.emit('SIGINT');
}
} }

View File

@ -5,9 +5,7 @@ const {
joinVoiceChannel joinVoiceChannel
} = require("@discordjs/voice"); } = require("@discordjs/voice");
export default { export default async function voiceStateUpdate(client: RadioClient, oldState: VoiceState, newState: VoiceState) {
name: "voiceStateUpdate",
async execute(client: RadioClient, oldState: VoiceState, newState: VoiceState) {
if (oldState.channel === null) return; if (oldState.channel === null) return;
let change = false; let change = false;
const radio = client.radio?.get(newState.guild.id); const radio = client.radio?.get(newState.guild.id);
@ -33,7 +31,6 @@ export default {
guildId: oldState.channel?.guild.id, guildId: oldState.channel?.guild.id,
adapterCreator: oldState.channel?.guild.voiceAdapterCreator adapterCreator: oldState.channel?.guild.voiceAdapterCreator
}) })
//radio.connection = await oldState.channel.join()
), ),
1000 1000
); );
@ -50,7 +47,7 @@ export default {
change = true; change = true;
radio.voiceChannel = newState.channel; radio.voiceChannel = newState.channel;
radio.connection = getVoiceConnection(newState.channel.guild.id); 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) { if ((oldState.channel.members.filter(member => !member.user.bot).size === 0 && oldState.channel === radio.voiceChannel) || change) {
@ -65,5 +62,4 @@ export default {
} }
}, 5000); }, 5000);
} }
},
}; };

View File

@ -1,8 +1,6 @@
import RadioClient from "../../Client"; import RadioClient from "../../Client";
export default { export default function warning(client: RadioClient, warning: Error) {
name: 'warning',
execute(client: RadioClient, warning: Error) {
if(warning.name == "ExperimentalWarning" && warning.message.startsWith("stream/web")) return; if(warning.name == "ExperimentalWarning" && warning.message.startsWith("stream/web")) return;
client.funcs.logger("Warning"); client.funcs.logger("Warning");
@ -10,5 +8,4 @@ export default {
console.warn(warning.message); console.warn(warning.message);
console.warn(warning.stack); console.warn(warning.stack);
console.log(''); console.log('');
}
} }