Typescript rework continue

This commit is contained in:
Christer Warén 2023-06-04 22:35:07 +03:00
parent 2d0b326721
commit 0e62861e33
14 changed files with 103 additions and 129 deletions

View File

@ -7,8 +7,8 @@ import Statistics from "./client/classes/Statistics";
import { command } from "./client/utils/typings"; import { command } from "./client/utils/typings";
import config from "./config"; import config from "./config";
import messages from "./client/messages"; import messages from "./client/messages";
import events from "./client/events"
const events = "./client/events/"; import funcs from "./client/funcs";
const GatewayIntents = new IntentsBitField(); const GatewayIntents = new IntentsBitField();
GatewayIntents.add( GatewayIntents.add(
@ -19,6 +19,7 @@ GatewayIntents.add(
export default class RadioClient extends Client { export default class RadioClient extends Client {
readonly commands: Collection<string, command>; readonly commands: Collection<string, command>;
public events: any;
public funcs: any; public funcs: any;
readonly config = config; readonly config = config;
readonly messages = messages; readonly messages = messages;
@ -27,6 +28,7 @@ export default class RadioClient extends Client {
public streamer: Streamer | null; public streamer: Streamer | null;
public statistics: Statistics | null; public statistics: Statistics | null;
public radio: Radio | null; public radio: Radio | null;
constructor() { constructor() {
super({ super({
intents: GatewayIntents intents: GatewayIntents
@ -38,15 +40,8 @@ export default class RadioClient extends Client {
this.statistics = null; this.statistics = null;
this.radio = null; this.radio = null;
this.funcs = {}; this.events = events;
this.funcs.check = require("./client/funcs/check"); this.funcs = funcs;
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");
console.log('RadioX ' + this.config.version); console.log('RadioX ' + this.config.version);
console.log('Internet Radio to your Discord guild'); console.log('Internet Radio to your Discord guild');
@ -59,19 +54,19 @@ export default class RadioClient extends Client {
this.config.maintenanceMode = true; this.config.maintenanceMode = true;
this.on("ready", () => { this.on("ready", () => {
require(`${events}ready`).execute(this); this.events.ready.execute(this);
}); });
this.on("messageDelete", msg => { this.on("messageDelete", msg => {
require(`${events}messageDelete`).execute(this, msg); this.events.messageDelete.execute(this, msg);
}); });
this.on("interactionCreate", interaction => { this.on("interactionCreate", interaction => {
require(`${events}interactionCreate`).execute(this, interaction); this.events.interactionCreate.execute(this, interaction);
}); });
this.on("voiceStateUpdate", (oldState, newState) => { this.on("voiceStateUpdate", (oldState, newState) => {
require(`${events}voiceStateUpdate`).execute(this, oldState, newState); this.events.voiceStateUpdate.execute(this, oldState, newState);
}); });
this.on("error", error => { this.on("error", error => {
@ -81,15 +76,15 @@ export default class RadioClient extends Client {
}); });
process.on('SIGINT', () => { process.on('SIGINT', () => {
require(`${events}SIGINT`).execute(this); this.events.SIGINT.execute(this);
}); });
process.on('SIGTERM', () => { process.on('SIGTERM', () => {
require(`${events}SIGTERM`).execute(this); this.events.SIGTERM.execute(this);
}); });
process.on('uncaughtException', (error) => { process.on('uncaughtException', (error) => {
require(`${events}uncaughtException`).execute(this, error); this.events.uncaughtException.execute(this, error);
}); });
process.on('exit', () => { process.on('exit', () => {
@ -97,7 +92,7 @@ export default class RadioClient extends Client {
}); });
process.on('warning', (warning) => { process.on('warning', (warning) => {
require(`${events}warning`).execute(this, warning); this.events.warning.execute(this, warning);
}); });
this.login(this.config.token).catch((err) => { this.login(this.config.token).catch((err) => {

View File

@ -1,5 +1,5 @@
const fs = require('fs'); import fs from 'fs';
const path = require('path'); import path from 'path';
export default class { export default class {
map: Map<any, any>; map: Map<any, any>;

View File

@ -1,7 +1,4 @@
const { import { getVoiceConnection, joinVoiceChannel } from "@discordjs/voice";
getVoiceConnection,
joinVoiceChannel
} = require("@discordjs/voice");
export default class Radio extends Map { export default class Radio extends Map {

View File

@ -1,12 +1,14 @@
const _importDynamic = new Function('modulePath', 'return import(modulePath)'); const _importDynamic = new Function('modulePath', 'return import(modulePath)');
// @ts-ignore
const fetch = (...args: any) => _importDynamic('node-fetch').then(({default: fetch}) => fetch(...args)); const fetch = (...args: any) => _importDynamic('node-fetch').then(({default: fetch}) => fetch(...args));
import logger from "../funcs/logger";
export default class Stations extends Array { export default class Stations extends Array {
logger: any; logger: any;
constructor() { constructor() {
super(); super();
this.logger = require("../funcs/logger.js"); this.logger = logger;
} }
async fetch(options: any){ async fetch(options: any){

View File

@ -1,8 +1,5 @@
const { import logger from "../funcs/logger";
createAudioPlayer, import { createAudioPlayer, createAudioResource, NoSubscriberBehavior } from "@discordjs/voice";
createAudioResource,
NoSubscriberBehavior
} = require("@discordjs/voice");
export default class Streamer { export default class Streamer {
map: any; map: any;
@ -12,7 +9,7 @@ export default class Streamer {
constructor() { constructor() {
this.map = new Map(); this.map = new Map();
this.mode = null; this.mode = null;
this.logger = require("../funcs/logger"); this.logger = logger;
} }
init(client: any){ init(client: any){

View File

@ -1,88 +1,56 @@
import { Snowflake } from "discord.js"; import { Snowflake } from "discord.js";
const { SlashCommandBuilder } = require('@discordjs/builders'); import bug from "./commands/bug";
const { REST } = require('@discordjs/rest'); import help from "./commands/help";
const { Routes } = require('discord-api-types/v9'); import invite from "./commands/invite";
const fs = require('fs'); import list from "./commands/list";
const path = require ('path'); 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 { export default {
async execute(client: any) { async execute(client: any) {
const commands : any = [ bug, help, invite, list, maintenance, next, nowplaying, play, prev, statistics, status, stop ];
const commands : any = []; for(const command of commands){
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}`);
client.commands.set(command.name, command); 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); client.funcs.logger('Application Commands', 'Started refreshing application (/) commands.');
}
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){ if(client.config.devMode){
await rest.put( client.application.commands.set([]);
Routes.applicationCommands(client.user.id), for(const command of commands){
{ body: [] },
);
let guilds = await client.guilds.fetch(); let guilds = await client.guilds.fetch();
guilds.forEach(async (guild: { id: string; name: string; }) => { guilds.forEach(async (guild: { id: Snowflake; name: string; }) => {
try { client.application.commands.create({
await rest.put( name: command.name,
Routes.applicationGuildCommands(client.user.id, guild.id), description: command.description,
{ body: commands } options: command.options || []
); }, guild.id);
client.funcs.logger('Slash Commands', 'Guild Applications Successful' + "\n" + guild.id + " / " + guild.name); client.funcs.logger('Application Commands', 'Guild: ' + guild.id + " (" + guild.name + ") \n" + 'Command: ' + command.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 { } else {
await rest.put( for(const command of commands){
Routes.applicationCommands(client.user.id), client.application.commands.create({
{ body: commands } name: command.name,
); description: command.description,
options: command.options || []
});
client.funcs.logger('Application Commands', 'Command: ' + command.name);
}
let guilds = await client.guilds.fetch(); let guilds = await client.guilds.fetch();
guilds.forEach(async (guild: { id: Snowflake; }) => { guilds.forEach(async (guild: { id: Snowflake; }) => {
try { client.application.commands.set([], guild.id);
await rest.put(
Routes.applicationGuildCommands(client.user.id, guild.id),
{ body: [] }
);
} catch (DiscordAPIError) {
}
}); });
} }
client.funcs.logger('Application Commands', 'Successfully reloaded application (/) commands.' + "\n");
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);
}
})();
*/
} }
} }

View File

@ -1,5 +1,6 @@
import { ActionRowBuilder, EmbedBuilder, StringSelectMenuBuilder } from "discord.js"; import { ActionRowBuilder, EmbedBuilder, StringSelectMenuBuilder } from "discord.js";
import Streamer from "../classes/Streamer"; import Streamer from "../classes/Streamer";
import commands from "../commands";
const _importDynamic = new Function('modulePath', 'return import(modulePath)'); const _importDynamic = new Function('modulePath', 'return import(modulePath)');
// @ts-ignore // @ts-ignore
const fetch = (...args) => _importDynamic('node-fetch').then(({default: fetch}) => fetch(...args)); const fetch = (...args) => _importDynamic('node-fetch').then(({default: fetch}) => fetch(...args));
@ -120,7 +121,7 @@ export default {
case "6": case "6":
client.config.maintenanceMode = true; client.config.maintenanceMode = true;
client.user.setStatus('idle'); client.user.setStatus('idle');
require(`../commands.js`).execute(client); commands.execute(client);
client.user.setStatus('online'); client.user.setStatus('online');
client.config.maintenanceMode = false; client.config.maintenanceMode = false;
break; break;

View File

@ -1,15 +1,12 @@
import { PermissionFlagsBits } from "discord.js"; import { ApplicationCommandOptionType, PermissionFlagsBits } from "discord.js";
const { import { getVoiceConnection, joinVoiceChannel } from "@discordjs/voice";
getVoiceConnection,
joinVoiceChannel
} = require("@discordjs/voice");
export default { export default {
name: "play", name: "play",
usage: "<song name>", usage: "<song name>",
description: "Play radio", description: "Play radio",
options: [ options: [
{ type: "STRING", name: "query", description: "Select station", required: false} { type: ApplicationCommandOptionType.String, name: "query", description: "Select station", required: false}
], ],
category: "radio", category: "radio",
async execute(interaction: any, client: any) { async execute(interaction: any, client: any) {
@ -111,7 +108,7 @@ export default {
} }
let date = new Date(); let date = new Date();
const construct = { const construct: any = {
textChannel: interaction.channel, textChannel: interaction.channel,
voiceChannel: voiceChannel, voiceChannel: voiceChannel,
connection: null, connection: null,

12
src/client/events.ts Normal file
View File

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

View File

@ -3,6 +3,8 @@ import Radio from "../classes/Radio";
import Stations from "../classes/Stations"; import Stations from "../classes/Stations";
import Streamer from "../classes/Streamer"; import Streamer from "../classes/Streamer";
import Statistics from "../classes/Statistics"; import Statistics from "../classes/Statistics";
import emojis from "../emojis"
import commands from "../commands";
export default { export default {
name: 'ready', name: 'ready',
@ -41,13 +43,6 @@ export default {
show: true show: true
}); });
/*setInterval(async () => {
await client.stations.fetch({
url: client.config.stationslistUrl,
show: false
});
}, 3600000);*/
client.streamer = new Streamer(); client.streamer = new Streamer();
client.streamer.init(client); client.streamer.init(client);
@ -70,10 +65,10 @@ export default {
client.statistics.calculateGlobal(client); client.statistics.calculateGlobal(client);
/*EMOJIS*/ /*EMOJIS*/
require(`../emojis.js`).execute(client); emojis.execute(client);
/*COMMANDS*/ /*COMMANDS*/
require(`../commands.js`).execute(client); commands.execute(client);
/*RADIO*/ /*RADIO*/
client.radio = new Radio(); client.radio = new Radio();

12
src/client/funcs.ts Normal file
View File

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

View File

@ -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){ export default function listStations(client: any, interaction: any){
let stations: any = new Array(); let stations: any = new Array();

View File

@ -1,5 +1,3 @@
require('dotenv/config');
export default { export default {
//credentials //credentials

View File

@ -1,3 +1,3 @@
import RadioClient from "./Client"; import RadioClient from "./Client";
const client = new RadioClient(); new RadioClient();