161 lines
4.9 KiB
TypeScript
161 lines
4.9 KiB
TypeScript
import { AudioPlayerStatus, AudioResource, createAudioPlayer, createAudioResource, entersState, getVoiceConnection, joinVoiceChannel, PlayerSubscription, VoiceConnection, VoiceConnectionStatus } from "@discordjs/voice";
|
|
import { Guild, StageChannel, VoiceChannel } from "discord.js";
|
|
import { once } from "events";
|
|
import { MuzikaFace } from "./types";
|
|
import { existsSync } from "fs";
|
|
|
|
const pripojeni: Record<string, PlayerSubscription> = {};
|
|
|
|
export const oddiakritikovat = (a: string) => {
|
|
return a
|
|
.replace(/[àáâãäåæāăą]/g, "a")
|
|
.replace(/[çćĉċč]/g, "c")
|
|
.replace(/[ďđ]/g, "d")
|
|
.replace(/[èéêëðēĕėęě]/g, "e")
|
|
.replace(/[ĝğġģ]/g, "g")
|
|
.replace(/[ĥħ]/g, "h")
|
|
.replace(/[ìíîïĩīĭįı]/g, "i")
|
|
.replace(/[ĵ]/g, "j")
|
|
.replace(/[ķĸ]/g, "k")
|
|
.replace(/[ĺļľŀł]/g, "l")
|
|
.replace(/[ñńņňʼnŋ]/g, "n")
|
|
.replace(/[òóôõöøōŏőœ]/g, "o")
|
|
.replace(/[þ]/g, "p")
|
|
.replace(/[ŕŗř]/g, "r")
|
|
.replace(/[ßśŝşšſ]/g, "s")
|
|
.replace(/[ţťŧ]/g, "t")
|
|
.replace(/[ùúûüũūŭůűų]/g, "u")
|
|
.replace(/[ŵ]/g, "w")
|
|
.replace(/[ÿýŷ]/g, "y")
|
|
.replace(/[źżž]/g, "z")
|
|
.replace(/[ÀÁÂÃÄÅÆĀĂĄ]/g, "A")
|
|
.replace(/[ÇĆĈĊČ]/g, "C")
|
|
.replace(/[Ď]/g, "D")
|
|
.replace(/[ÈÉÊËÐĒĔĖĘĚ]/g, "E")
|
|
.replace(/[ĜĞĠĢ]/g, "G")
|
|
.replace(/[Ĵ]/g, "J")
|
|
.replace(/[Ķ]/g, "K")
|
|
.replace(/[ĤĦ]/g, "H")
|
|
.replace(/[ÌÍÎÏĨĪĬĮİ]/g, "I")
|
|
.replace(/[ĹĻĽĿŁ]/g, "L")
|
|
.replace(/[ÑŃŅŇŊ]/g, "N")
|
|
.replace(/[ÒÓÔÕÖØŌŎŐŒ]/g, "O")
|
|
.replace(/[Þ]/g, "P")
|
|
.replace(/[ŔŖŘ]/g, "R")
|
|
.replace(/[ŚŜŞŠ]/g, "S")
|
|
.replace(/[ŢŤŦ]/g, "T")
|
|
.replace(/[ÙÚÛÜŨŪŬŮŰŲ]/g, "U")
|
|
.replace(/[Ŵ]/g, "W")
|
|
.replace(/[ÝŶŸ]/g, "Y")
|
|
.replace(/[ŹŻŽ]/g, "Z");
|
|
};
|
|
|
|
export const formatCas = (cas: number) => {
|
|
const h = Math.floor(cas / 3600);
|
|
const m = Math.floor(cas % 3600 / 60);
|
|
const s = Math.floor(cas % 3600 % 60);
|
|
|
|
return `${h} hodin ${m} mynut a ${s} se kund`;
|
|
};
|
|
|
|
export const joinVoice = async (channel: VoiceChannel | StageChannel | string, guild?: Guild) => {
|
|
const channelId = typeof channel === "string" ? channel : channel.id;
|
|
const guildId = typeof channel === "string" ? guild!.id : channel.guildId;
|
|
const guilda = typeof channel === "string" ? guild! : channel.guild;
|
|
|
|
let conn = getVoiceConnection(guildId);
|
|
let prev: boolean | string = false;
|
|
if (conn) {
|
|
if (conn.joinConfig.channelId === channelId) {
|
|
await entersState(conn, VoiceConnectionStatus.Ready, 3e4)
|
|
.catch(err => {
|
|
conn!.destroy();
|
|
throw err;
|
|
});
|
|
return { conn, prev: true };
|
|
}
|
|
prev = conn.joinConfig.channelId!;
|
|
}
|
|
|
|
conn = joinVoiceChannel({
|
|
channelId,
|
|
guildId,
|
|
adapterCreator: guilda.voiceAdapterCreator
|
|
});
|
|
|
|
await entersState(conn, VoiceConnectionStatus.Ready, 3e4)
|
|
.catch((err: Error) => {
|
|
conn!.destroy();
|
|
throw err;
|
|
});
|
|
|
|
if (!prev) {
|
|
const player = createAudioPlayer();
|
|
pripojeni[guildId] = conn.subscribe(player)!;
|
|
}
|
|
|
|
return { conn, prev };
|
|
};
|
|
|
|
const makeAudioPlayer = (co: string | MuzikaFace) => {
|
|
if (typeof co === "string") return createAudioResource(co);
|
|
const res = createAudioResource(co.name, { inlineVolume: true });
|
|
res.volume?.setVolume(co.volume);
|
|
return res;
|
|
};
|
|
|
|
export const play = (conn: VoiceConnection, co: string | string[] | MuzikaFace | MuzikaFace[]) => new Promise<void>(async (res, rej) => {
|
|
if (conn.state.status !== VoiceConnectionStatus.Ready) return rej("conn není ready");
|
|
|
|
const player = pripojeni[conn.joinConfig.guildId].player;
|
|
if (!Array.isArray(co)) {
|
|
let aud = makeAudioPlayer(co);
|
|
player.play(aud);
|
|
const funkce = () => {
|
|
res();
|
|
player.removeListener(AudioPlayerStatus.Idle, funkce);
|
|
};
|
|
player.on(AudioPlayerStatus.Idle, funkce);
|
|
return;
|
|
}
|
|
|
|
const resources: AudioResource[] = [];
|
|
co.forEach(c => {
|
|
const res = makeAudioPlayer(c);
|
|
resources.push(res);
|
|
});
|
|
for (const res of resources) {
|
|
await once(res.playStream, "readable");
|
|
}
|
|
player.play(resources[0]);
|
|
|
|
let i = 0;
|
|
const funkce = () => {
|
|
i++;
|
|
if (i === resources.length) {
|
|
res();
|
|
player.removeListener(AudioPlayerStatus.Idle, funkce);
|
|
}
|
|
else player.play(resources[i]);
|
|
};
|
|
player.on(AudioPlayerStatus.Idle, funkce);
|
|
});
|
|
|
|
const leave = (guildId: string) => {
|
|
const conn = getVoiceConnection(guildId);
|
|
if (!conn) return;
|
|
conn.destroy();
|
|
delete pripojeni[guildId];
|
|
};
|
|
|
|
export const handlePrevVoice = (guild: Guild, prev: boolean | string) => {
|
|
if (prev === true) return; // Byl jsem v tomdle vojsu
|
|
if (prev === false) return leave(guild.id); // Nebyl jsem ve vojsu vůbec
|
|
joinVoice(prev, guild); // Byl jsem jinde
|
|
};
|
|
|
|
export function loadEnv() {
|
|
if (!existsSync("config.json")) throw new Error("config.json neexistuje");
|
|
process.env = { ...process.env, ...require("../../config.json") };
|
|
}
|