Add music loop

This commit is contained in:
2022-03-03 23:57:30 +07:00
parent cde013f783
commit 11f5a3fd20
4 changed files with 144 additions and 6 deletions
+109
View File
@@ -0,0 +1,109 @@
import DiscordModule, { HybridInteractionMessage } from "../../utils/DiscordModule";
import { Message, CommandInteraction, Interaction, MessageActionRow, ButtonInteraction, MessageSelectMenu, MessageSelectOptionData } from "discord.js";
import { makeErrorEmbed, makeSuccessEmbed, sendHybridInteractionMessageResponse, makeInfoEmbed } from "../../utils/DiscordMessage";
import DiscordMusicPlayer, { DiscordMusicPlayerLoopMode } from "../../providers/DiscordMusicPlayer";
const EMBEDS = {
INVALID_LOOP_MODE: (data: Message | Interaction) => {
return makeErrorEmbed({
title: `Invalid loop mode`,
user: (data instanceof Interaction) ? data.user : data.author
});
},
LOOP_STATUS: (data: Message | Interaction, loopMode: DiscordMusicPlayerLoopMode) => {
let embed = makeInfoEmbed({
title: `Current loop mode is ${loopMode}`,
description: "Available loop mode: ``None`` ``Current``",
user: (data instanceof Interaction) ? data.user : data.author
});
return embed;
},
LOOP_SET: (data: Message | Interaction, loopMode: DiscordMusicPlayerLoopMode) => {
let embed = makeSuccessEmbed({
title: `Loop mode is now set to ${loopMode}`,
user: (data instanceof Interaction) ? data.user : data.author
});
return embed;
},
USER_NOT_IN_VOICECHANNEL: (data: Message | Interaction) => {
return makeErrorEmbed({
title: `You need to be in the voice channel first!`,
user: (data instanceof Interaction) ? data.user : data.author
});
},
USER_NOT_IN_SAME_VOICECHANNEL: (data: Message | Interaction) => {
return makeErrorEmbed({
title: `You are not in the same voice channel!`,
user: (data instanceof Interaction) ? data.user : data.author
});
},
NO_MUSIC_PLAYING: (data: Message | Interaction) => {
return makeErrorEmbed({
title: `There are no music playing`,
user: (data instanceof Interaction) ? data.user : data.author
});
}
}
export default class Loop extends DiscordModule {
public id = "Discord_MusicPlayer_Loop";
public commands = ["loop"];
public commandInteractionName = "loop";
async GuildOnModuleCommand(args: any, message: Message) {
await this.run(new HybridInteractionMessage(message), args);
}
async GuildModuleCommandInteractionCreate(interaction: CommandInteraction) {
await this.run(new HybridInteractionMessage(interaction), interaction.options);
}
async run(data: HybridInteractionMessage, args: any) {
const guild = data.getGuild();
const user = data.getUser();
const member = data.getMember();
const channel = data.getChannel();
if (!guild || !user || !member || !channel) return;
let query;
if (data.isMessage()) {
if (args.length !== 0)
query = args.join(' ');
}
else if (data.isSlashCommand())
query = args.getSubcommand();
const voiceChannel = member.voice.channel;
if (!voiceChannel)
return await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.USER_NOT_IN_VOICECHANNEL(data.getRaw())] });
const instance = DiscordMusicPlayer.getGuildInstance(guild.id);
if (!instance)
return await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.NO_MUSIC_PLAYING(data.getRaw())] });
if (instance.voiceChannel.id !== voiceChannel.id)
return await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.USER_NOT_IN_SAME_VOICECHANNEL(data.getRaw())] }, true);
if (instance.queue.track.length === 0)
return await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.NO_MUSIC_PLAYING(data.getRaw())] });
if (!query)
return await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.LOOP_STATUS(data.getRaw(), instance.getLoopMode())] });
let enumKey = Object.keys(DiscordMusicPlayerLoopMode)[Object.values(DiscordMusicPlayerLoopMode).indexOf(query.toLowerCase())];
if(enumKey) {
instance.setLoopMode(DiscordMusicPlayerLoopMode[enumKey as keyof typeof DiscordMusicPlayerLoopMode]);
return await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.LOOP_SET(data.getRaw(), instance.getLoopMode())] });
}
return await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.INVALID_LOOP_MODE(data.getRaw())] });
}
}
+9 -6
View File
@@ -3,10 +3,13 @@ import DiscordModule, { HybridInteractionMessage } from "../../utils/DiscordModu
import { Message, CommandInteraction, Interaction, TextChannel } from "discord.js";
import { makeErrorEmbed, sendHybridInteractionMessageResponse, makeInfoEmbed } from "../../utils/DiscordMessage";
import DiscordMusicPlayer, { Queue } from "../../providers/DiscordMusicPlayer";
import DiscordMusicPlayer, { DiscordMusicPlayerInstance, DiscordMusicPlayerLoopMode } from "../../providers/DiscordMusicPlayer";
const EMBEDS = {
QUEUE: (data: Message | Interaction, queue: Queue) => {
QUEUE: (data: Message | Interaction, instance: DiscordMusicPlayerInstance) => {
const queue = instance.queue;
if(!queue.track[0])
return makeInfoEmbed({
title: `Queue`,
@@ -18,7 +21,7 @@ const EMBEDS = {
let queueString = `1. [${queue.track[0].title}](${queue.track[0].url})`
return makeInfoEmbed({
title: `Queue`,
description: `Now playing: [${queue.track[0].title}](${queue.track[0].url})\n\nThere are ${queue.track.length} song in the queue!\n${queueString}`,
description: `Now playing${instance.getLoopMode() === DiscordMusicPlayerLoopMode.Current ? " (Looping current)" : ""}: [${queue.track[0].title}](${queue.track[0].url})\n\nThere are ${queue.track.length} song in the queue!\n${queueString}`,
user: (data instanceof Interaction) ? data.user : data.author
});
}
@@ -27,14 +30,14 @@ const EMBEDS = {
let queueString = first10.map((track, index) => `${index + 1}. [${track.title}](${track.url})`).join("\n");
return makeInfoEmbed({
title: `Queue`,
description: `Now playing: [${queue.track[0].title}](${queue.track[0].url})\nUpcoming song: [${queue.track[1].title}](${queue.track[1].url})\n\nThere are ${queue.track.length} songs in the queue!\n${queueString}\n${queue.track.length > 10 ? `...${queue.track.length - 10} more songs` : ""}`,
description: `Now playing${instance.getLoopMode() === DiscordMusicPlayerLoopMode.Current ? " (Looping current)" : ""}: [${queue.track[0].title}](${queue.track[0].url})\nUpcoming song: [${queue.track[1].title}](${queue.track[1].url})\n\nThere are ${queue.track.length} songs in the queue!\n${queueString}\n${queue.track.length > 10 ? `...${queue.track.length - 10} more songs` : ""}`,
user: (data instanceof Interaction) ? data.user : data.author
});
} else {
let queueString = queue.track.map((track, index) => `${index + 1}. [${track.title}](${track.url})`).join("\n");
return makeInfoEmbed({
title: `Queue`,
description: `Now playing: [${queue.track[0].title}](${queue.track[0].url})\nUpcoming song: [${queue.track[1].title}](${queue.track[1].url})\n\nThere are ${queue.track.length} songs in the queue!\n${queueString}`,
description: `Now playing${instance.getLoopMode() === DiscordMusicPlayerLoopMode.Current ? " (Looping current)" : ""}: [${queue.track[0].title}](${queue.track[0].url})\nUpcoming song: [${queue.track[1].title}](${queue.track[1].url})\n\nThere are ${queue.track.length} songs in the queue!\n${queueString}`,
user: (data instanceof Interaction) ? data.user : data.author
});
}
@@ -71,7 +74,7 @@ export default class QueueCommand extends DiscordModule {
const instance = DiscordMusicPlayer.getGuildInstance(guild.id);
if(!instance) return await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.NO_MUSIC_PLAYING(data.getRaw())] });
await sendHybridInteractionMessageResponse(data, { embeds:[EMBEDS.QUEUE(data.getRaw(), instance.queue)] });
await sendHybridInteractionMessageResponse(data, { embeds:[EMBEDS.QUEUE(data.getRaw(), instance)] });
return;
}