Denim-Bot/src/modules/muzika.ts

152 lines
4.4 KiB
TypeScript

// Tady bude muzika, vole
import { AudioPlayerStatus, getVoiceConnection } from "@discordjs/voice";
import { Client, VoiceBasedChannel } from "discord.js";
import { search, soundcloud, stream, validate, video_basic_info } from "play-dl";
import { emouty } from "../utils/emotes";
import { Modul, SRecord } from "../utils/types";
import { adminLog, log } from "../utils/utils";
import { novejPlay, Priority, stopPlayer } from "../utils/voice";
const kjus: SRecord<{ name: string; url: string; }[]> = {};
async function playNext(channel: VoiceBasedChannel, seek?: number) {
try {
const kju = kjus[channel.guildId]!;
const item = kju[0];
const src = await stream(item.url, { seek });
novejPlay(channel, { src: src.stream, volume: 1, type: src.type }, Priority.Music)
.then(state => {
if (state.status != AudioPlayerStatus.Idle) return;
kju.shift();
if (kju.length)
return playNext(channel);
delete kjus[channel.guildId];
})
.catch(e => {
log("chyba v muziki", e);
});
} catch (e) {
if (e instanceof Error && e.message.startsWith("Seeking beyond limit"))
return stopPlayer(channel.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.channel;
if (!kanel) return "nejsi ve vojsu ty kkt";
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}\``));
}
const kju = (kjus[mes.guildId!] ??= []);
kju.push(ajtem);
if (kju.length != 1) return;
playNext(mes.member.voice.channel);
}
},
queue: {
als: ["q", "kju"],
DMUnsafe: true,
run: mes => {
const kju = kjus[mes.guildId!];
if (!kju) 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 = kjus[mes.guildId!];
if (!kju) return "nic nehraje";
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 = kjus[mes.guildId!];
if (!kju) return "nic nehraje";
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 = kjus[mes.guildId!];
if (!kju) return "nehraje nic";
const channel = mes.client.channels.cache.get(getVoiceConnection(mes.guildId!)?.joinConfig.channelId ?? "") as VoiceBasedChannel;
if (!channel) return "neco nefunguje";
playNext(channel, numero);
mes.react(emouty.d3k);
}
}
}
};
module.exports = exp;