TypeScript types

This commit is contained in:
Christer Warén
2023-06-04 05:48:42 +03:00
parent 9303c4fcc9
commit 3fc7337d0f
36 changed files with 131 additions and 119 deletions

View File

@ -2,6 +2,7 @@ const fs = require('fs');
const path = require('path');
export default class {
map: Map<any, any>;
constructor() {
this.map = new Map();
this.loadData();
@ -27,7 +28,7 @@ export default class {
//console.log("");
}
checkEntry(id){
checkEntry(id: string){
this.loadEntry(id);
if(!this.map.has(id)){
this.createEntry(id);
@ -37,8 +38,8 @@ export default class {
}
}
createEntry(id){
let newData = {};
createEntry(id: string){
let newData: any = {};
newData.guild = {};
newData.guild.id = id;
newData.statistics = {};
@ -47,7 +48,7 @@ export default class {
this.saveEntry(id, newData);
}
loadEntry(id){
loadEntry(id: any){
try {
const json = require(`../../../datastore/` + id + '.json');
this.map.set(id, json);
@ -55,11 +56,11 @@ export default class {
}
}
getEntry(id){
getEntry(id: string){
return this.map.get(id);
}
updateEntry(guild, newData) {
updateEntry(guild: any, newData: any) {
newData.guild.name = guild.name;
let date = new Date();
@ -70,7 +71,7 @@ export default class {
//this.showEntry(this.getEntry(guild.id));
}
showEntry(data){
showEntry(data : any){
console.log(data);
}
@ -94,10 +95,10 @@ export default class {
this.updateEntry(newData.guild, newData);
}
saveEntry(file, data) {
saveEntry(file: string, data: any) {
data = JSON.stringify(data, null, 4);
fs.writeFile(path.join(path.dirname(__dirname), '../../datastore') + "/" + file + ".json", data, 'utf8', function(err) {
fs.writeFile(path.join(path.dirname(__dirname), '../../datastore') + "/" + file + ".json", data, 'utf8', function(err: any) {
if (err) {
//console.log(err);
}

View File

@ -4,11 +4,12 @@ const {
} = require("@discordjs/voice");
export default class Radio extends Map {
constructor() {
super();
}
save(client) {
save(client: any) {
let currentRadios = this.keys();
let radio = currentRadios.next();
@ -29,16 +30,16 @@ export default class Radio extends Map {
}
}
restore(client, guilds) {
restore(client: any, guilds: any) {
if(!client.stations) return;
guilds.forEach(async guild => {
guilds.forEach(async (guild: { id: any; }) => {
let state = client.funcs.loadState(client, guild);
if(!state) return;
if(!state.station || !state.channels.voice || !state.channels.text) return;
let voiceChannel = client.channels.cache.get(state.channels.voice);
if(!voiceChannel) return;
if(voiceChannel.members.filter(member => !member.user.bot).size === 0) return;
if(voiceChannel.members.filter((member: { user: { bot: any; }; }) => !member.user.bot).size === 0) return;
const sstation = await client.stations.search(state.station.name, "direct");

View File

@ -1,22 +1,24 @@
const _importDynamic = new Function('modulePath', 'return import(modulePath)');
const fetch = (...args) => _importDynamic('node-fetch').then(({default: fetch}) => fetch(...args));
const fetch = (...args: any) => _importDynamic('node-fetch').then(({default: fetch}) => fetch(...args));
export default class Stations extends Array {
logger: any;
constructor() {
super();
this.logger = require("../funcs/logger.js");
}
async fetch(options){
async fetch(options: any){
try {
this.logger('Stations', 'Started fetching list ' + options.url);
let list = await fetch(options.url)
.then(this.checkFetchStatus)
.then(response => response.json());
.then((response: { json: () => any; }) => response.json());
if(list){
this.length = 0;
list.forEach(station => {
list.forEach((station: any) => {
try {
this.push(station);
} catch (error) {
@ -25,12 +27,12 @@ export default class Stations extends Array {
});
if(options.show){
list.forEach(station => {
list.forEach((station: { name: any; }) => {
this.logger('Stations', station.name);
});
}
list.forEach(async station => {
list.forEach(async (station: { stream: { [x: string]: any; default: string | number; }; }) => {
try {
let stationTest = await fetch(station.stream[station.stream.default]);
if(stationTest.ok === true) return;
@ -50,7 +52,7 @@ export default class Stations extends Array {
}
}
checkFetchStatus(response) {
checkFetchStatus(response: any) {
if (response.ok) { // res.status >= 200 && res.status < 300
return response;
} else {
@ -58,7 +60,7 @@ export default class Stations extends Array {
}
}
search(key, type) {
search(key: string, type: string) {
if (this === null) return false;
if (!key) return false;
if (!type) return false;
@ -73,7 +75,7 @@ export default class Stations extends Array {
return foundStation;
} else {
let foundStations = [];
let foundStations : any[] = [];
if (key == "radio") return false;
this

View File

@ -1,9 +1,13 @@
import { Guild } from "discord.js";
export default class Statistics {
map: any;
constructor() {
this.map = new Map();
}
update(client, guild, radio) {
update(client: any, guild: Guild, radio: any) {
client.datastore.checkEntry(guild.id);
@ -26,13 +30,13 @@ export default class Statistics {
this.calculateGlobal(client);
}
calculateGlobal(client){
calculateGlobal(client: any){
if(!client.stations) return;
if(!client.datastore.map) return;
let guilds = client.datastore.map.keys();
let stations = client.stations;
let statistics = {};
let statistics : any = {};
if(!client.stations) return;
@ -59,7 +63,7 @@ export default class Statistics {
calculation = guilds.next();
}
let newData = {};
let newData : any = {};
newData.guild = {};
newData.guild.id = "global";
newData.guild.name = "global";

View File

@ -1,18 +1,21 @@
const {
createAudioPlayer,
createAudioResource,
AudioPlayerStatus,
NoSubscriberBehavior
} = require("@discordjs/voice");
export default class Streamer {
map: any;
mode: any | null;
logger: any;
constructor() {
this.map = new Map();
this.mode = null;
this.logger = require("../funcs/logger");
}
init(client){
init(client: any){
if(!client.config.streamerMode) return;
switch(client.config.streamerMode){
@ -29,24 +32,24 @@ export default class Streamer {
if(this.mode == "auto"){
if(!client.stations) return;
client.stations.forEach(station => {
client.stations.forEach((station: any) => {
this.play(station);
});
}
}
refresh(client){
refresh(client: any){
this.init(client);
let streamers = this.map.keys();
streamers.forEach(streamer => {
if(client.stations.findIndex(station => station.name == streamer) == -1){
streamers.forEach((streamer: any) => {
if(client.stations.findIndex((station: { name: any; }) => station.name == streamer) == -1){
this.stop(streamer);
}
});
}
play(station) {
play(station: any) {
let audioPlayer = this.map.get(station.name);
if(!audioPlayer) {
if(this.mode == "auto"){
@ -89,13 +92,13 @@ export default class Streamer {
.on('autopaused', () => {
this.logger('Streamer', station.name + " / " + "AutoPaused");
})
.on('error', error => {
.on('error', (error: string) => {
this.logger('Streamer', station.name + " / " + "Error" + "\n" + error);
});
return audioPlayer;
}
stop(station){
stop(station: any){
let audioPlayer = this.map.get(station.name);
if(audioPlayer){
this.logger('Streamer', station.name + " / " + "Stop");
@ -105,15 +108,15 @@ export default class Streamer {
this.map.delete(station.name);
}
listen(station) {
listen(station: any) {
let audioPlayer = this.map.get(station.name);
if(!audioPlayer || this.mode == "manual" && audioPlayer.subscribers.length == 0) audioPlayer = this.play(station);
return audioPlayer;
}
leave(client) {
leave(client: any) {
if(!client.stations) return;
client.stations.forEach(station => {
client.stations.forEach((station: any) => {
this.stop(station);
});
}