diff --git a/src/Client.ts b/src/Client.ts index 7b5a4e1..4208f17 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -7,8 +7,8 @@ import Statistics from "./client/classes/Statistics"; import { command } from "./client/utils/typings"; import config from "./config"; import messages from "./client/messages"; - -const events = "./client/events/"; +import events from "./client/events" +import funcs from "./client/funcs"; const GatewayIntents = new IntentsBitField(); GatewayIntents.add( @@ -19,6 +19,7 @@ GatewayIntents.add( export default class RadioClient extends Client { readonly commands: Collection; + public events: any; public funcs: any; readonly config = config; readonly messages = messages; @@ -27,6 +28,7 @@ export default class RadioClient extends Client { public streamer: Streamer | null; public statistics: Statistics | null; public radio: Radio | null; + constructor() { super({ intents: GatewayIntents @@ -38,15 +40,8 @@ export default class RadioClient extends Client { this.statistics = null; this.radio = null; - this.funcs = {}; - this.funcs.check = require("./client/funcs/check"); - this.funcs.isDev = require("./client/funcs/isDev"); - this.funcs.logger = require("./client/funcs/logger"); - this.funcs.msToTime = require("./client/funcs/msToTime"); - this.funcs.saveState = require("./client/funcs/saveState"); - this.funcs.loadState = require("./client/funcs/loadState"); - this.funcs.play = require("./client/funcs/play"); - this.funcs.listStations = require("./client/funcs/listStations"); + this.events = events; + this.funcs = funcs; console.log('RadioX ' + this.config.version); console.log('Internet Radio to your Discord guild'); @@ -59,19 +54,19 @@ export default class RadioClient extends Client { this.config.maintenanceMode = true; this.on("ready", () => { - require(`${events}ready`).execute(this); + this.events.ready.execute(this); }); this.on("messageDelete", msg => { - require(`${events}messageDelete`).execute(this, msg); + this.events.messageDelete.execute(this, msg); }); this.on("interactionCreate", interaction => { - require(`${events}interactionCreate`).execute(this, interaction); + this.events.interactionCreate.execute(this, interaction); }); this.on("voiceStateUpdate", (oldState, newState) => { - require(`${events}voiceStateUpdate`).execute(this, oldState, newState); + this.events.voiceStateUpdate.execute(this, oldState, newState); }); this.on("error", error => { @@ -81,15 +76,15 @@ export default class RadioClient extends Client { }); process.on('SIGINT', () => { - require(`${events}SIGINT`).execute(this); + this.events.SIGINT.execute(this); }); process.on('SIGTERM', () => { - require(`${events}SIGTERM`).execute(this); + this.events.SIGTERM.execute(this); }); process.on('uncaughtException', (error) => { - require(`${events}uncaughtException`).execute(this, error); + this.events.uncaughtException.execute(this, error); }); process.on('exit', () => { @@ -97,7 +92,7 @@ export default class RadioClient extends Client { }); process.on('warning', (warning) => { - require(`${events}warning`).execute(this, warning); + this.events.warning.execute(this, warning); }); this.login(this.config.token).catch((err) => { diff --git a/src/client/classes/Datastore.ts b/src/client/classes/Datastore.ts index d7f2637..c8e0bbe 100644 --- a/src/client/classes/Datastore.ts +++ b/src/client/classes/Datastore.ts @@ -1,5 +1,5 @@ -const fs = require('fs'); -const path = require('path'); +import fs from 'fs'; +import path from 'path'; export default class { map: Map; diff --git a/src/client/classes/Radio.ts b/src/client/classes/Radio.ts index b215237..21d7f4d 100644 --- a/src/client/classes/Radio.ts +++ b/src/client/classes/Radio.ts @@ -1,7 +1,4 @@ -const { - getVoiceConnection, - joinVoiceChannel -} = require("@discordjs/voice"); +import { getVoiceConnection, joinVoiceChannel } from "@discordjs/voice"; export default class Radio extends Map { diff --git a/src/client/classes/Stations.ts b/src/client/classes/Stations.ts index e91343f..a827eed 100644 --- a/src/client/classes/Stations.ts +++ b/src/client/classes/Stations.ts @@ -1,12 +1,14 @@ const _importDynamic = new Function('modulePath', 'return import(modulePath)'); +// @ts-ignore const fetch = (...args: any) => _importDynamic('node-fetch').then(({default: fetch}) => fetch(...args)); +import logger from "../funcs/logger"; export default class Stations extends Array { logger: any; constructor() { super(); - this.logger = require("../funcs/logger.js"); + this.logger = logger; } async fetch(options: any){ diff --git a/src/client/classes/Streamer.ts b/src/client/classes/Streamer.ts index 4e5055a..a5b2896 100644 --- a/src/client/classes/Streamer.ts +++ b/src/client/classes/Streamer.ts @@ -1,8 +1,5 @@ -const { - createAudioPlayer, - createAudioResource, - NoSubscriberBehavior -} = require("@discordjs/voice"); +import logger from "../funcs/logger"; +import { createAudioPlayer, createAudioResource, NoSubscriberBehavior } from "@discordjs/voice"; export default class Streamer { map: any; @@ -12,7 +9,7 @@ export default class Streamer { constructor() { this.map = new Map(); this.mode = null; - this.logger = require("../funcs/logger"); + this.logger = logger; } init(client: any){ diff --git a/src/client/commands.ts b/src/client/commands.ts index b7a79f2..f53ba00 100644 --- a/src/client/commands.ts +++ b/src/client/commands.ts @@ -1,88 +1,56 @@ import { Snowflake } from "discord.js"; -const { SlashCommandBuilder } = require('@discordjs/builders'); -const { REST } = require('@discordjs/rest'); -const { Routes } = require('discord-api-types/v9'); -const fs = require('fs'); -const path = require ('path'); +import bug from "./commands/bug"; +import help from "./commands/help"; +import invite from "./commands/invite"; +import list from "./commands/list"; +import maintenance from "./commands/maintenance"; +import next from "./commands/next"; +import nowplaying from "./commands/nowplaying"; +import play from "./commands/play"; +import prev from "./commands/prev"; +import statistics from "./commands/statistics"; +import status from "./commands/status"; +import stop from "./commands/stop"; export default { async execute(client: any) { + const commands : any = [ bug, help, invite, list, maintenance, next, nowplaying, play, prev, statistics, status, stop ]; - const commands : any = []; - const commandFiles = fs.readdirSync(path.join("./src/client/commands")).filter((f: string) => f.endsWith(".ts")); - - for (const file of commandFiles) { - const command = require(`./commands/${file}`); + for(const command of commands){ client.commands.set(command.name, command); - - command.data = new SlashCommandBuilder() - .setName(command.name) - .setDescription(command.description); - - command.data = command.data.toJSON(); - if(command.options) { - command.options.forEach(function(option: { type: string | number; }) { - if(option.type == "STRING") option.type = 3; - if(option.type == "NUMBER") option.type = 10; - command.data.options.push(option); - }); - } - - commands.push(command.data); } - console.log(commands); -/* - const rest = new REST({ version: '9' }).setToken(client.config.token); - - (async () => { - try { - client.funcs.logger('Slash Commands', 'Started refreshing application (/) commands.'); - - if(client.config.devMode){ - await rest.put( - Routes.applicationCommands(client.user.id), - { body: [] }, - ); - - let guilds = await client.guilds.fetch(); - guilds.forEach(async (guild: { id: string; name: string; }) => { - try { - await rest.put( - Routes.applicationGuildCommands(client.user.id, guild.id), - { body: commands } - ); - client.funcs.logger('Slash Commands', 'Guild Applications – Successful' + "\n" + guild.id + " / " + guild.name); - } catch (DiscordAPIError: any) { - client.funcs.logger('Slash Commands', 'Guild Applications – Failed' + "\n" + guild.id + " / " + guild.name); - if(DiscordAPIError.name != "DiscordAPIError[50001]") console.error(DiscordAPIError.message + "\n\n"); - } - }); - } else { - await rest.put( - Routes.applicationCommands(client.user.id), - { body: commands } - ); - - let guilds = await client.guilds.fetch(); - guilds.forEach(async (guild: { id: Snowflake; }) => { - try { - await rest.put( - Routes.applicationGuildCommands(client.user.id, guild.id), - { body: [] } - ); - } catch (DiscordAPIError) { - } - }); - } - - client.funcs.logger('Slash Commands', 'Successfully reloaded application (/) commands.' + "\n"); - } catch (error) { - client.funcs.logger('Slash Commands', 'Reloading application (/) commands failed.' + "\n"); - console.error(error); + client.funcs.logger('Application Commands', 'Started refreshing application (/) commands.'); + if(client.config.devMode){ + client.application.commands.set([]); + for(const command of commands){ + let guilds = await client.guilds.fetch(); + guilds.forEach(async (guild: { id: Snowflake; name: string; }) => { + client.application.commands.create({ + name: command.name, + description: command.description, + options: command.options || [] + }, guild.id); + client.funcs.logger('Application Commands', 'Guild: ' + guild.id + " (" + guild.name + ") \n" + 'Command: ' + command.name); + }); } - })(); -*/ + } else { + for(const command of commands){ + client.application.commands.create({ + name: command.name, + description: command.description, + options: command.options || [] + }); + + client.funcs.logger('Application Commands', 'Command: ' + command.name); + } + + let guilds = await client.guilds.fetch(); + guilds.forEach(async (guild: { id: Snowflake; }) => { + client.application.commands.set([], guild.id); + }); + } + client.funcs.logger('Application Commands', 'Successfully reloaded application (/) commands.' + "\n"); } } diff --git a/src/client/commands/maintenance.ts b/src/client/commands/maintenance.ts index abdbb18..7d325c5 100644 --- a/src/client/commands/maintenance.ts +++ b/src/client/commands/maintenance.ts @@ -1,5 +1,6 @@ import { ActionRowBuilder, EmbedBuilder, StringSelectMenuBuilder } from "discord.js"; import Streamer from "../classes/Streamer"; +import commands from "../commands"; const _importDynamic = new Function('modulePath', 'return import(modulePath)'); // @ts-ignore const fetch = (...args) => _importDynamic('node-fetch').then(({default: fetch}) => fetch(...args)); @@ -120,7 +121,7 @@ export default { case "6": client.config.maintenanceMode = true; client.user.setStatus('idle'); - require(`../commands.js`).execute(client); + commands.execute(client); client.user.setStatus('online'); client.config.maintenanceMode = false; break; diff --git a/src/client/commands/play.ts b/src/client/commands/play.ts index 270364d..c56e98b 100644 --- a/src/client/commands/play.ts +++ b/src/client/commands/play.ts @@ -1,15 +1,12 @@ -import { PermissionFlagsBits } from "discord.js"; -const { - getVoiceConnection, - joinVoiceChannel -} = require("@discordjs/voice"); +import { ApplicationCommandOptionType, PermissionFlagsBits } from "discord.js"; +import { getVoiceConnection, joinVoiceChannel } from "@discordjs/voice"; export default { name: "play", usage: "", description: "Play radio", options: [ - { type: "STRING", name: "query", description: "Select station", required: false} + { type: ApplicationCommandOptionType.String, name: "query", description: "Select station", required: false} ], category: "radio", async execute(interaction: any, client: any) { @@ -111,7 +108,7 @@ export default { } let date = new Date(); - const construct = { + const construct: any = { textChannel: interaction.channel, voiceChannel: voiceChannel, connection: null, diff --git a/src/client/events.ts b/src/client/events.ts new file mode 100644 index 0000000..dc32b06 --- /dev/null +++ b/src/client/events.ts @@ -0,0 +1,12 @@ +import interactionCreate from "./events/interactionCreate" +import messageDelete from "./events/messageDelete" +import ready from "./events/ready" +import SIGINT from "./events/SIGINT" +import SIGTERM from "./events/SIGTERM" +import uncaughtException from "./events/uncaughtException" +import voiceStateUpdate from "./events/voiceStateUpdate" +import warning from "./events/warning" + +export default { + interactionCreate, messageDelete, ready, SIGINT, SIGTERM, uncaughtException, voiceStateUpdate, warning +} diff --git a/src/client/events/ready.ts b/src/client/events/ready.ts index 403ff64..e42e21a 100644 --- a/src/client/events/ready.ts +++ b/src/client/events/ready.ts @@ -3,6 +3,8 @@ import Radio from "../classes/Radio"; import Stations from "../classes/Stations"; import Streamer from "../classes/Streamer"; import Statistics from "../classes/Statistics"; +import emojis from "../emojis" +import commands from "../commands"; export default { name: 'ready', @@ -41,13 +43,6 @@ export default { show: true }); - /*setInterval(async () => { - await client.stations.fetch({ - url: client.config.stationslistUrl, - show: false - }); - }, 3600000);*/ - client.streamer = new Streamer(); client.streamer.init(client); @@ -70,10 +65,10 @@ export default { client.statistics.calculateGlobal(client); /*EMOJIS*/ - require(`../emojis.js`).execute(client); + emojis.execute(client); /*COMMANDS*/ - require(`../commands.js`).execute(client); + commands.execute(client); /*RADIO*/ client.radio = new Radio(); diff --git a/src/client/funcs.ts b/src/client/funcs.ts new file mode 100644 index 0000000..5a2c7ae --- /dev/null +++ b/src/client/funcs.ts @@ -0,0 +1,12 @@ +import check from "./funcs/check"; +import isDev from "./funcs/isDev"; +import listStations from "./funcs/listStations"; +import loadState from "./funcs/loadState"; +import logger from "./funcs/logger"; +import msToTime from "./funcs/msToTime"; +import play from "./funcs/play"; +import saveState from "./funcs/saveState"; + +export default { + check, isDev, listStations, loadState, logger, msToTime, play, saveState +} diff --git a/src/client/funcs/listStations.ts b/src/client/funcs/listStations.ts index 0a51045..e873b19 100644 --- a/src/client/funcs/listStations.ts +++ b/src/client/funcs/listStations.ts @@ -1,4 +1,4 @@ -import { ActionRowBuilder, Interaction, StringSelectMenuBuilder } from "discord.js"; +import { ActionRowBuilder, StringSelectMenuBuilder } from "discord.js"; export default function listStations(client: any, interaction: any){ let stations: any = new Array(); diff --git a/src/config.ts b/src/config.ts index 2bdc4ed..a00e8e9 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,5 +1,3 @@ -require('dotenv/config'); - export default { //credentials diff --git a/src/index.ts b/src/index.ts index a27b1ee..87b8477 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,3 @@ import RadioClient from "./Client"; -const client = new RadioClient(); +new RadioClient();