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

View File

@ -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<any, any>;

View File

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

View File

@ -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){

View File

@ -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){

View File

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

View File

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

View File

@ -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: "<song name>",
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,

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 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();

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){
let stations: any = new Array();

View File

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

View File

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