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 { 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<string, command>;
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");

View File

@ -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);
});
}

View File

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

View File

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

View File

@ -1,9 +1,8 @@
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({
@ -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.EmbedLinks)) return interaction.reply({
@ -50,4 +50,3 @@ export default {
}
}
}
}

View File

@ -1,9 +1,7 @@
import { Message, PartialMessage } from "discord.js";
import RadioClient from "../../Client";
export default {
name: 'messageDelete',
async execute(client: RadioClient, msg: Message | PartialMessage) {
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;
@ -11,4 +9,3 @@ export default {
if(msg.id != radio.message.id) return;
radio.message = null;
}
}

View File

@ -6,10 +6,7 @@ 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");
/*DATASTORE*/
@ -82,6 +79,4 @@ export default {
client.config.maintenanceMode = false;
}
}, 10000);
}
}

View File

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

View File

@ -5,9 +5,7 @@ const {
joinVoiceChannel
} = require("@discordjs/voice");
export default {
name: "voiceStateUpdate",
async execute(client: RadioClient, oldState: VoiceState, newState: VoiceState) {
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);
@ -33,7 +31,6 @@ export default {
guildId: oldState.channel?.guild.id,
adapterCreator: oldState.channel?.guild.voiceAdapterCreator
})
//radio.connection = await oldState.channel.join()
),
1000
);
@ -50,7 +47,7 @@ export default {
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) {
@ -65,5 +62,4 @@ export default {
}
}, 5000);
}
},
};

View File

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