support multiple servers
Bi-Rabittoh andronacomarco@gmail.com
Wed, 10 Jan 2024 14:58:12 +0100
7 files changed,
23 insertions(+),
9 deletions(-)
M
src/commands/clear.ts
→
src/commands/clear.ts
@@ -1,5 +1,5 @@
import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js'; -import { getChannel, queue } from '../functions/music'; +import { getChannel, getQueue } from '../functions/music'; module.exports = { data: new SlashCommandBuilder()@@ -11,6 +11,7 @@ const channel = await getChannel(interaction);
if (typeof channel == 'string') return await interaction.reply({ content: channel, ephemeral: true }); + const queue = getQueue(interaction.guildId); queue.clear() return await interaction.reply({ content: 'Queue cleared.' }); },
M
src/commands/outro.ts
→
src/commands/outro.ts
@@ -1,5 +1,5 @@
import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js'; -import { getChannel, queue } from '../functions/music'; +import { getChannel, getQueue } from '../functions/music'; import path from 'node:path'; const { outros } = require(path.join(process.cwd(), 'config.json'));@@ -32,6 +32,7 @@
const outro = interaction.options.getString('which'); const kick = interaction.options.getString('kick'); const outroUrl = getOutroUrl(outro); + const queue = getQueue(interaction.guildId); await queue.outro(outroUrl, channel); if (kick !== 'false') {
M
src/commands/play.ts
→
src/commands/play.ts
@@ -1,6 +1,6 @@
import { ChatInputCommandInteraction, SlashCommandBuilder } from 'discord.js'; import play, { YouTubeVideo } from 'play-dl'; -import { getChannel, formatTitle, queue } from '../functions/music'; +import { getChannel, formatTitle, getQueue } from '../functions/music'; async function handleUserInput(input: string): Promise<YouTubeVideo[]> { try {@@ -42,6 +42,7 @@ await interaction.deferReply();
const opt = interaction.options; const input = opt.getString('query'); const yt_videos = await handleUserInput(input); + const queue = getQueue(interaction.guildId); const added = await queue.addArray(yt_videos, channel); switch (added.length) {
M
src/commands/queue.ts
→
src/commands/queue.ts
@@ -1,5 +1,5 @@
import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js'; -import { formatTitle, getChannel, queue } from '../functions/music'; +import { formatTitle, getChannel, getQueue } from '../functions/music'; import { YouTubeVideo } from 'play-dl'; const CHARACTER_LIMIT_API = 2000;@@ -36,6 +36,7 @@ const channel = await getChannel(interaction);
if (typeof channel == 'string') return await interaction.reply({ content: channel, ephemeral: true }); + const queue = getQueue(interaction.guildId); const result = queue.queue; if (result) { const reply = getReply(result);
M
src/commands/skip.ts
→
src/commands/skip.ts
@@ -1,5 +1,5 @@
import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js'; -import { getChannel, queue } from '../functions/music'; +import { getChannel, getQueue } from '../functions/music'; module.exports = { data: new SlashCommandBuilder()@@ -11,8 +11,8 @@ const channel = await getChannel(interaction);
if (typeof channel == 'string') return await interaction.reply({ content: channel, ephemeral: true }); + const queue = getQueue(interaction.guildId); const result = await queue.next(); return await interaction.reply({ content: 'Skipped.' }); - //return await interaction.reply({ content: 'Error: couldn\'t skip.', ephemeral: true }); }, };
M
src/commands/stop.ts
→
src/commands/stop.ts
@@ -1,5 +1,5 @@
import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js'; -import { getChannel, queue } from '../functions/music'; +import { getChannel, getQueue } from '../functions/music'; module.exports = { data: new SlashCommandBuilder()@@ -11,6 +11,7 @@ const channel = await getChannel(interaction);
if (typeof channel == 'string') return await interaction.reply({ content: channel, ephemeral: true }); + const queue = getQueue(interaction.guildId); const r = queue.stop(); if (r) return await interaction.reply({ content: 'Stopped.', ephemeral: true });
M
src/functions/music.ts
→
src/functions/music.ts
@@ -2,10 +2,19 @@ import { ChatInputCommandInteraction, VoiceBasedChannel } from 'discord.js'
import { YouTubeVideo } from 'play-dl'; import MyQueue from './myqueue'; -export const queue = new MyQueue(); +const queues = new Map<string, MyQueue>(); + +export function getQueue(server: string): MyQueue { + if (queues.has(server)) + return queues.get(server); + + const q = new MyQueue(); + queues.set(server, q); + return q; +} export function formatTitle(video: YouTubeVideo): string { - return `**${video.title}** (\`${video.durationRaw}\`)` + return `**${video.title}** (\`${video.durationRaw}\`)`; } export async function getChannel(interaction: ChatInputCommandInteraction): Promise<string | VoiceBasedChannel>{