More typings to classes, commands, events, funcs

This commit is contained in:
Christer Warén 2023-06-06 08:05:54 +03:00
parent 227e0bcaaa
commit 40cd3a9ec5
14 changed files with 84 additions and 39 deletions

View File

@ -1,6 +1,32 @@
import fs from 'fs';
import { Guild } from 'discord.js';
import fs, { NoParamCallback } from 'fs';
import path from 'path';
interface entry {
guild: {
id: string,
name?: string
},
statistics: {
[key: string]: {
"time": number,
"used": number
}
} | {},
state: {
channels: {
"text": string,
"voice": string
},
date: string,
station: {
name: string,
owner: string
}
} | {},
updated?: string
}
export default class {
map: Map<string, any>;
constructor() {
@ -40,11 +66,13 @@ export default class {
}
createEntry(id: string){
let newData: any = {};
newData.guild = {};
newData.guild.id = id;
newData.statistics = {};
newData.state = {};
let newData: entry = {
guild: {
id: id,
},
statistics: {},
state: {}
};
this.map.set(id, newData);
this.saveEntry(id, newData);
}
@ -61,7 +89,7 @@ export default class {
return this.map.get(id);
}
updateEntry(guild: any, newData: any) {
updateEntry(guild: Guild | { id: string, name: string }, newData: entry) {
newData.guild.name = guild.name;
let date = new Date();
@ -72,7 +100,7 @@ export default class {
//this.showEntry(this.getEntry(guild.id));
}
showEntry(data : any){
showEntry(data : entry){
console.log(data);
}
@ -96,12 +124,10 @@ export default class {
this.updateEntry(newData.guild, newData);
}
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: any) {
saveEntry(file: string, data: entry) {
fs.writeFile(path.join(path.dirname(__dirname), '../../datastore') + "/" + file + ".json", JSON.stringify(data, null, 4), 'utf8', function(err: any) {
if (err) {
//console.log(err);
}
});
}

View File

@ -1,6 +1,19 @@
import { getVoiceConnection, joinVoiceChannel } from "@discordjs/voice";
import { Guild, GuildMember, VoiceChannel } from "discord.js";
import { Guild, GuildMember, TextBasedChannel, VoiceBasedChannel, VoiceChannel } from "discord.js";
import { getVoiceConnection, joinVoiceChannel, VoiceConnection } from "@discordjs/voice";
import RadioClient from "../../Client";
import { station } from "./Stations";
export interface radio {
textChannel: TextBasedChannel | null,
voiceChannel: VoiceBasedChannel,
connection: VoiceConnection | null,
message: null,
station: station,
datastore?: any,
currentTime?: number,
startTime: number,
playTime?: number,
}
export default class Radio extends Map {

View File

@ -40,7 +40,7 @@ export default class Stations extends Array {
});
}
list.forEach(async (station: { stream: { [x: string]: any; default: string | number; }; }) => {
list.forEach(async (station: station) => {
try {
let stationTest = await fetch(station.stream[station.stream.default]);
if(stationTest.ok === true) return;
@ -83,7 +83,7 @@ export default class Stations extends Array {
return foundStation;
} else {
let foundStations : any[] = [];
let foundStations : { station: string, name: string, probability: number }[] = [];
if (key == "radio") return false;
this
@ -126,7 +126,7 @@ export default class Stations extends Array {
}
}
}
let highestProbabilityStation;
let highestProbabilityStation : any | { station: string, name: string, probability: number } | string | null = null;
for (let i = 0; i < foundStations.length; i++) {
if (
!highestProbabilityStation ||

View File

@ -1,14 +1,15 @@
import { Guild } from "discord.js";
import RadioClient from "../../Client";
import { radio } from "./Radio";
export default class Statistics {
map: any;
map: Map<any, any>;
constructor() {
this.map = new Map();
}
update(client: RadioClient, guild: Guild | null, radio: any) {
update(client: RadioClient, guild: Guild | null, radio: radio) {
if(!guild) return;
client.datastore?.checkEntry(guild.id);
@ -24,8 +25,8 @@ export default class Statistics {
let date = new Date();
radio.currentTime = date.getTime();
radio.playTime = parseInt(radio.currentTime)-parseInt(radio.startTime);
radio.datastore.statistics[radio.station.name].time = parseInt(radio.datastore.statistics[radio.station.name].time)+parseInt(radio.playTime);
radio.playTime = radio.currentTime - radio.startTime;
radio.datastore.statistics[radio.station.name].time = parseInt(radio.datastore.statistics[radio.station.name].time) + radio.playTime;
radio.datastore.statistics[radio.station.name].used = parseInt(radio.datastore.statistics[radio.station.name].used)+1;
client.datastore?.updateEntry(guild, radio.datastore);

View File

@ -5,12 +5,10 @@ import { station } from "./Stations";
export default class Streamer {
map: any;
mode: any | null;
logger: any;
mode: "auto" | "manual" = "manual";
constructor() {
this.map = new Map();
this.mode = null;
}
init(client: RadioClient){

View File

@ -1,5 +1,6 @@
import { ChatInputCommandInteraction, ColorResolvable, EmbedBuilder } from "discord.js";
import RadioClient from "../../Client";
import { command } from "../commands";
export default {
name: 'help',
@ -12,13 +13,13 @@ export default {
ephemeral: true
});
const categories : any= [];
const categories: string[] = [];
for (let i = 0; i < client.commands.size; i++) {
if (!categories.includes([...client.commands.values()][i].category)) categories.push([...client.commands.values()][i].category);
}
let commands = '';
for (let i = 0; i < categories.length; i++) {
commands += `**» ${categories[i].toUpperCase()}**\n${client.commands.filter(x => x.category === categories[i]).map((x: { name: any; }) => `\`${x.name}\``).join(', ')}\n`;
commands += `**» ${categories[i].toUpperCase()}**\n${client.commands.filter(x => x.category === categories[i]).map((x: command) => `\`${x.name}\``).join(', ')}\n`;
}
const embed = new EmbedBuilder()

View File

@ -1,11 +1,12 @@
import { ButtonInteraction, ChatInputCommandInteraction, ColorResolvable, EmbedBuilder, StringSelectMenuInteraction } from "discord.js";
import RadioClient from "../../Client";
import { command } from "../commands";
export default {
name: 'nowplaying',
description: 'Current Radio Station',
category: 'radio',
async execute(interaction: ButtonInteraction | ChatInputCommandInteraction | StringSelectMenuInteraction, client: RadioClient, command: any) {
async execute(interaction: ButtonInteraction | ChatInputCommandInteraction | StringSelectMenuInteraction, client: RadioClient, command: command) {
if(client.funcs.check(client, interaction, command)) {
const radio = client.radio?.get(interaction.guild?.id);

View File

@ -1,6 +1,7 @@
import { ApplicationCommandOptionType, ChatInputCommandInteraction, GuildMember, PermissionFlagsBits, StringSelectMenuInteraction } from "discord.js";
import { getVoiceConnection, joinVoiceChannel } from "@discordjs/voice";
import RadioClient from "../../Client";
import { radio } from "../classes/Radio"
export default {
name: "play",
@ -116,7 +117,7 @@ export default {
}
let date = new Date();
const construct: any = {
const construct: radio = {
textChannel: interaction.channel,
voiceChannel: voiceChannel,
connection: null,

View File

@ -1,11 +1,11 @@
import { ColorResolvable, EmbedBuilder } from "discord.js";
import { ChatInputCommandInteraction, ColorResolvable, EmbedBuilder } from "discord.js";
import RadioClient from "../../Client";
export default {
name: 'status',
description: 'Bot Status',
category: 'info',
async execute(interaction: any, client: RadioClient) {
async execute(interaction: ChatInputCommandInteraction, client: RadioClient) {
if(!client.user) return interaction.reply({
content: client.messages.emojis["error"] + client.messages.maintenance,

View File

@ -1,11 +1,12 @@
import { ButtonInteraction, ChatInputCommandInteraction, ColorResolvable, EmbedBuilder, StringSelectMenuInteraction } from "discord.js";
import RadioClient from "../../Client";
import { command } from "../commands";
export default {
name: 'stop',
description: 'Stop radio',
category: 'radio',
async execute(interaction: ButtonInteraction | ChatInputCommandInteraction | StringSelectMenuInteraction, client: RadioClient, command: any) {
async execute(interaction: ButtonInteraction | ChatInputCommandInteraction | StringSelectMenuInteraction, client: RadioClient, command: command) {
if (client.funcs.check(client, interaction, command)) {
const radio = client.radio?.get(interaction.guild?.id);
client.statistics?.update(client, interaction.guild, radio);

View File

@ -1,4 +1,4 @@
import { PermissionFlagsBits, VoiceState } from "discord.js";
import { GuildMember, PermissionFlagsBits, VoiceState } from "discord.js";
import RadioClient from "../../Client";
const {
getVoiceConnection,
@ -53,7 +53,7 @@ export default async function voiceStateUpdate(client: RadioClient, oldState: Vo
if ((oldState.channel.members.filter(member => !member.user.bot).size === 0 && oldState.channel === radio.voiceChannel) || change) {
setTimeout(() => {
if (!radio || !radio.connection || !radio.connection === null) return;
if (radio.voiceChannel.members.filter((member: { user: { bot: any; }; }) => !member.user.bot).size === 0) {
if (radio.voiceChannel.members.filter((member: GuildMember) => !member.user.bot).size === 0) {
client.statistics?.update(client, newState.guild, radio);
radio.connection?.destroy();
radio.message?.delete();

View File

@ -1,9 +1,10 @@
import { ButtonInteraction, ChatInputCommandInteraction, StringSelectMenuInteraction } from "discord.js";
import RadioClient from "../../Client";
import { command } from "../commands";
export default function check(client: RadioClient, interaction: any, command: command) {
export default function check(client: RadioClient, interaction: ButtonInteraction | ChatInputCommandInteraction | StringSelectMenuInteraction, command: command) {
const radio = client.radio?.get(interaction.guild.id);
const radio = client.radio?.get(interaction.guild?.id);
if(!client.stations) {
interaction.reply({
content: client.messages.emojis["error"] + client.messages.replace(client.messages.errorToGetPlaylist, {
@ -20,6 +21,7 @@ export default function check(client: RadioClient, interaction: any, command: co
});
return false;
}
//@ts-ignore
if (interaction.member.voice.channel !== radio.voiceChannel) {
interaction.reply({
content: client.messages.emojis["error"] + client.messages.wrongVoiceChannel,

View File

@ -1,6 +1,6 @@
import { Snowflake } from "discord.js";
export default function isDev(devIDs : any[], authorID : Snowflake){
export default function isDev(devIDs : string[], authorID : Snowflake){
for (const devID of devIDs){
if(authorID == devID){
return true;

View File

@ -1,7 +1,8 @@
import { Guild } from "discord.js";
import RadioClient from "../../Client";
import { radio } from "../classes/Radio";
export default function saveState(client: RadioClient, guild: Guild, radio: any){
export default function saveState(client: RadioClient, guild: Guild, radio: radio){
if(!client.datastore) return;
client.datastore.checkEntry(guild.id);
@ -11,7 +12,7 @@ export default function saveState(client: RadioClient, guild: Guild, radio: any)
data.state = {};
data.state.channels = {};
data.state.channels.text = radio.textChannel.id;
data.state.channels.text = radio.textChannel?.id;
data.state.channels.voice = radio.voiceChannel.id;
data.state.date = date.toISOString();
data.state.station = {};