Denim-Bot/src/modules/muzika.ts

172 lines
4.9 KiB
TypeScript

// Tady bude muzika, vole
import { AudioPlayerStatus, VoiceConnectionStatus } from "@discordjs/voice";
import { Client } from "discord.js";
import { search, soundcloud, stream, validate, video_basic_info } from "play-dl";
import { emouty } from "../utils/emotes";
import { Modul } from "../utils/types";
import { adminLog, log } from "../utils/utils";
import { getConn, novejJoin, novejPlay, Priority, stopPlayer } from "../utils/voice";
const guildy = new Map<string, { kju: { name: string; url: string; }[]; loop: boolean; }>();
async function playNext(guildId: string, seek?: number) {
try {
const guilda = guildy.get(guildId)!;
const kju = guilda.kju;
const item = kju[0];
const src = await stream(item.url, { seek });
novejPlay(guildId, { src: src.stream, volume: 1, type: src.type }, Priority.Music)
.then(state => {
if (state.status != AudioPlayerStatus.Idle) return;
if (!guilda.loop) kju.shift();
if (kju.length)
return playNext(guildId);
})
.catch(e => {
log("chyba v muziki", e);
});
} catch (e) {
if (e instanceof Error && e.message.startsWith("Seeking beyond limit"))
return stopPlayer(guildId, Priority.Music);
log(e);
const client: Client = module.exports.client;
adminLog(client, "error v hani muziky");
}
}
const exp: Modul = {
more_komandy: {
zahraj: {
DMUnsafe: true,
run: async (mes, txt) => {
const kanel = mes.member?.voice.channelId;
if (!kanel) return "nejsi ve vojsu ty kkt";
const guildId = mes.guildId!;
const ajtem = { name: "", url: "" };
const druh = await validate(txt);
if (druh && druh != "search") {
if (druh != "yt_video" && druh != "so_track") return "tuto neumim zahrat";
ajtem.url = txt;
if (druh == "yt_video") {
try {
const video = await video_basic_info(txt);
ajtem.name = video.video_details.title!;
} catch (e) {
return "sorka bracho ael tuto je ajdzreztrigtnuti";
}
if (typeof ajtem.name == "undefined") {
const client: Client = module.exports.client;
adminLog(client, "video nemá název");
}
mes.channel.send(`zahraju \`${ajtem.name}\``);
} else {
const sound = await soundcloud(txt);
ajtem.name = sound.name;
mes.channel.send(`zahraju \`${ajtem.name}\``);
}
} else {
const msg = mes.channel.send("hledam");
const hledani = await search(txt, { limit: 1 });
if (!hledani[0]) return "nic sem nenašel";
ajtem.url = hledani[0].url;
ajtem.name = hledani[0].title!;
msg.then(m => void m.edit(`zahraju \`${ajtem.name}\``));
}
if (!guildy.has(guildId)) {
guildy.set(guildId, { kju: [], loop: false });
}
const kju = guildy.get(guildId)!.kju;
kju.push(ajtem);
if (kju.length != 1) return;
await novejJoin(mes.guild!, kanel);
getConn(guildId)?.once(VoiceConnectionStatus.Destroyed, () => {
kju.length = 0;
});
playNext(guildId);
}
},
queue: {
als: ["q", "kju"],
DMUnsafe: true,
run: mes => {
const kju = guildy.get(mes.guildId!)?.kju;
if (!kju?.length) return "nehraje nic";
return `tuto je kurentni repertoar:\n${kju.reduce((acc, cur, i) => {
const jmeno = `${cur.name} (<${cur.url}>)`;
return `${acc}\n${i ? `${i}. ${jmeno}` : `-- *${jmeno}* --`}`;
}, "")}`;
}
},
skip: {
DMUnsafe: true,
run: mes => {
const kju = guildy.get(mes.guildId!)?.kju;
if (!kju?.length) return "nehraje nic";
stopPlayer(mes.guildId!, Priority.Music);
mes.react(emouty.d3k);
}
},
remove: {
DMUnsafe: true,
als: ["odebrat"],
run: (mes, arg) => {
const numero = Number(arg);
if (isNaN(numero)) return "cokundo?";
const kju = guildy.get(mes.guildId!)?.kju;
if (!kju?.length) return "nehraje nic";
if (kju.length <= numero) return "tolik toho nehrae";
kju.splice(numero, 1);
mes.react(emouty.d3k);
}
},
seek: {
DMUnsafe: true,
run: (mes, arg) => {
const numero = Number(arg);
if (isNaN(numero)) return "huh?";
const kju = guildy.get(mes.guildId!)?.kju;
if (!kju?.length) return "nehraje nic";
playNext(mes.guildId!, numero);
mes.react(emouty.d3k);
}
},
loop: {
DMUnsafe: true,
run: mes => {
if (!guildy.has(mes.guildId!)) {
guildy.set(mes.guildId!, { kju: [], loop: false });
}
const guilda = guildy.get(mes.guildId!)!;
guilda.loop = !guilda.loop;
return `lup je ${guilda.loop ? "zapnutej" : "ukoncen"}`;
}
}
}
};
module.exports = exp;