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'; 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 { export default class {
map: Map<string, any>; map: Map<string, any>;
constructor() { constructor() {
@ -40,11 +66,13 @@ export default class {
} }
createEntry(id: string){ createEntry(id: string){
let newData: any = {}; let newData: entry = {
newData.guild = {}; guild: {
newData.guild.id = id; id: id,
newData.statistics = {}; },
newData.state = {}; statistics: {},
state: {}
};
this.map.set(id, newData); this.map.set(id, newData);
this.saveEntry(id, newData); this.saveEntry(id, newData);
} }
@ -61,7 +89,7 @@ export default class {
return this.map.get(id); return this.map.get(id);
} }
updateEntry(guild: any, newData: any) { updateEntry(guild: Guild | { id: string, name: string }, newData: entry) {
newData.guild.name = guild.name; newData.guild.name = guild.name;
let date = new Date(); let date = new Date();
@ -72,7 +100,7 @@ export default class {
//this.showEntry(this.getEntry(guild.id)); //this.showEntry(this.getEntry(guild.id));
} }
showEntry(data : any){ showEntry(data : entry){
console.log(data); console.log(data);
} }
@ -96,12 +124,10 @@ export default class {
this.updateEntry(newData.guild, newData); this.updateEntry(newData.guild, newData);
} }
saveEntry(file: string, data: any) { saveEntry(file: string, data: entry) {
data = JSON.stringify(data, null, 4); fs.writeFile(path.join(path.dirname(__dirname), '../../datastore') + "/" + file + ".json", JSON.stringify(data, null, 4), 'utf8', function(err: any) {
fs.writeFile(path.join(path.dirname(__dirname), '../../datastore') + "/" + file + ".json", data, 'utf8', function(err: any) {
if (err) { if (err) {
//console.log(err);
} }
}); });
} }

View File

@ -1,6 +1,19 @@
import { getVoiceConnection, joinVoiceChannel } from "@discordjs/voice"; import { Guild, GuildMember, TextBasedChannel, VoiceBasedChannel, VoiceChannel } from "discord.js";
import { Guild, GuildMember, VoiceChannel } from "discord.js"; import { getVoiceConnection, joinVoiceChannel, VoiceConnection } from "@discordjs/voice";
import RadioClient from "../../Client"; 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 { 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 { try {
let stationTest = await fetch(station.stream[station.stream.default]); let stationTest = await fetch(station.stream[station.stream.default]);
if(stationTest.ok === true) return; if(stationTest.ok === true) return;
@ -83,7 +83,7 @@ export default class Stations extends Array {
return foundStation; return foundStation;
} else { } else {
let foundStations : any[] = []; let foundStations : { station: string, name: string, probability: number }[] = [];
if (key == "radio") return false; if (key == "radio") return false;
this 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++) { for (let i = 0; i < foundStations.length; i++) {
if ( if (
!highestProbabilityStation || !highestProbabilityStation ||

View File

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

View File

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

View File

@ -1,5 +1,6 @@
import { ChatInputCommandInteraction, ColorResolvable, EmbedBuilder } from "discord.js"; import { ChatInputCommandInteraction, ColorResolvable, EmbedBuilder } from "discord.js";
import RadioClient from "../../Client"; import RadioClient from "../../Client";
import { command } from "../commands";
export default { export default {
name: 'help', name: 'help',
@ -12,13 +13,13 @@ export default {
ephemeral: true ephemeral: true
}); });
const categories : any= []; const categories: string[] = [];
for (let i = 0; i < client.commands.size; i++) { for (let i = 0; i < client.commands.size; i++) {
if (!categories.includes([...client.commands.values()][i].category)) categories.push([...client.commands.values()][i].category); if (!categories.includes([...client.commands.values()][i].category)) categories.push([...client.commands.values()][i].category);
} }
let commands = ''; let commands = '';
for (let i = 0; i < categories.length; i++) { 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() const embed = new EmbedBuilder()

View File

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

View File

@ -1,6 +1,7 @@
import { ApplicationCommandOptionType, ChatInputCommandInteraction, GuildMember, PermissionFlagsBits, StringSelectMenuInteraction } from "discord.js"; import { ApplicationCommandOptionType, ChatInputCommandInteraction, GuildMember, PermissionFlagsBits, StringSelectMenuInteraction } from "discord.js";
import { getVoiceConnection, joinVoiceChannel } from "@discordjs/voice"; import { getVoiceConnection, joinVoiceChannel } from "@discordjs/voice";
import RadioClient from "../../Client"; import RadioClient from "../../Client";
import { radio } from "../classes/Radio"
export default { export default {
name: "play", name: "play",
@ -116,7 +117,7 @@ export default {
} }
let date = new Date(); let date = new Date();
const construct: any = { const construct: radio = {
textChannel: interaction.channel, textChannel: interaction.channel,
voiceChannel: voiceChannel, voiceChannel: voiceChannel,
connection: null, 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"; import RadioClient from "../../Client";
export default { export default {
name: 'status', name: 'status',
description: 'Bot Status', description: 'Bot Status',
category: 'info', category: 'info',
async execute(interaction: any, client: RadioClient) { async execute(interaction: ChatInputCommandInteraction, client: RadioClient) {
if(!client.user) return interaction.reply({ if(!client.user) return interaction.reply({
content: client.messages.emojis["error"] + client.messages.maintenance, content: client.messages.emojis["error"] + client.messages.maintenance,

View File

@ -1,11 +1,12 @@
import { ButtonInteraction, ChatInputCommandInteraction, ColorResolvable, EmbedBuilder, StringSelectMenuInteraction } from "discord.js"; import { ButtonInteraction, ChatInputCommandInteraction, ColorResolvable, EmbedBuilder, StringSelectMenuInteraction } from "discord.js";
import RadioClient from "../../Client"; import RadioClient from "../../Client";
import { command } from "../commands";
export default { export default {
name: 'stop', name: 'stop',
description: 'Stop radio', description: 'Stop radio',
category: '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)) { if (client.funcs.check(client, interaction, command)) {
const radio = client.radio?.get(interaction.guild?.id); const radio = client.radio?.get(interaction.guild?.id);
client.statistics?.update(client, interaction.guild, radio); 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"; import RadioClient from "../../Client";
const { const {
getVoiceConnection, 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) { if ((oldState.channel.members.filter(member => !member.user.bot).size === 0 && oldState.channel === radio.voiceChannel) || change) {
setTimeout(() => { setTimeout(() => {
if (!radio || !radio.connection || !radio.connection === null) return; 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); client.statistics?.update(client, newState.guild, radio);
radio.connection?.destroy(); radio.connection?.destroy();
radio.message?.delete(); radio.message?.delete();

View File

@ -1,9 +1,10 @@
import { ButtonInteraction, ChatInputCommandInteraction, StringSelectMenuInteraction } from "discord.js";
import RadioClient from "../../Client"; import RadioClient from "../../Client";
import { command } from "../commands"; 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) { if(!client.stations) {
interaction.reply({ interaction.reply({
content: client.messages.emojis["error"] + client.messages.replace(client.messages.errorToGetPlaylist, { 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; return false;
} }
//@ts-ignore
if (interaction.member.voice.channel !== radio.voiceChannel) { if (interaction.member.voice.channel !== radio.voiceChannel) {
interaction.reply({ interaction.reply({
content: client.messages.emojis["error"] + client.messages.wrongVoiceChannel, content: client.messages.emojis["error"] + client.messages.wrongVoiceChannel,

View File

@ -1,6 +1,6 @@
import { Snowflake } from "discord.js"; 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){ for (const devID of devIDs){
if(authorID == devID){ if(authorID == devID){
return true; return true;

View File

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