diff --git a/.env.example b/.env.example index 616f1784c..eeab8ec3a 100644 --- a/.env.example +++ b/.env.example @@ -18,13 +18,15 @@ BOT_STATUS= "online" # Your bot status BOT_ACTIVITY= "Lavamusic" # Your bot activity -LAVALINK_URL="lavalink:2333" # Your lavalink url +AUTO_NODE="true" # true for auto node it is gave from lavainfo-api "https://lavainfo-api.freedback-dip.workers.dev" -LAVALINK_AUTH="youshallnotpass" # Your lavalink password +LAVALINK_URL="lavalink:2333" # Your lavalink url (if auto node is true then you can leave it blank) -LAVALINK_NAME="Blacky" # Your lavalink name +LAVALINK_AUTH="youshallnotpass" # Your lavalink password (if auto node is true then you can leave it blank) -LAVALINK_SECURE= "false" # true for secure lavalink +LAVALINK_NAME="Blacky" # Your lavalink name (if auto node is true then you can leave it blank) + +LAVALINK_SECURE= "false" # true for secure lavalink (if auto node is true then you can leave it blank) KEEP_ALIVE= "false" # true for keep alive in https://replit.com diff --git a/src/commands/info/Help.ts b/src/commands/info/Help.ts index b18c58432..8eb903df5 100644 --- a/src/commands/info/Help.ts +++ b/src/commands/info/Help.ts @@ -59,7 +59,7 @@ export default class Help extends Command { .setColor(this.client.color.main) .setTitle('Help Menu') .setDescription( - `Hey there! I'm ${this.client.user.username}, a music bot made with [Lavamusic](https://github.com/appujet/lavamusic) and Discord. You can use \`${prefix}help \` to get more info on a command.` + `Hey there! I'm ${this.client.user.username}, a music bot made with [Lavamusic](https://github.com/appujet/lavamusic) and Discord. You can use \`${prefix.prefix}help \` to get more info on a command.` ) .setFooter({ text: `Use ${prefix.prefix}help for more info on a command`, diff --git a/src/commands/music/Shuffle.ts b/src/commands/music/Shuffle.ts index 2bcfdc878..33e0cc8f5 100644 --- a/src/commands/music/Shuffle.ts +++ b/src/commands/music/Shuffle.ts @@ -39,7 +39,7 @@ export default class Shuffle extends Command { .setDescription('There are no songs in the queue.'), ], }); - player.setShuffle(true); + player.setShuffle(); return await ctx.sendMessage({ embeds: [embed.setColor(this.client.color.main).setDescription(`Shuffled the queue`)], diff --git a/src/config.ts b/src/config.ts index 401ccc904..088ec30c9 100644 --- a/src/config.ts +++ b/src/config.ts @@ -14,6 +14,7 @@ export default { main: 0x2f3136, }, keepAlive: parseBoolean(process.env.KEEP_ALIVE) || false, // for https://replit.com keep alive bot 24/7 + autoNode: parseBoolean(process.env.AUTO_NODE) || true, // auto node for lavamusic bot it is gave from lavainfo-api "https://lavainfo-api.freedback-dip.workers.dev" searchEngine: process.env.SEARCH_ENGINE || (SearchEngine.YouTube as SearchEngine), maxPlaylistSize: parseInt(process.env.MAX_PLAYLIST_SIZE) || 100, botStatus: process.env.BOT_STATUS || 'online', // online, idle, dnd, invisible diff --git a/src/events/client/SetupButtons.ts b/src/events/client/SetupButtons.ts index 1a816ad22..481bcd447 100644 --- a/src/events/client/SetupButtons.ts +++ b/src/events/client/SetupButtons.ts @@ -191,22 +191,12 @@ export default class SetupButtons extends Event { break; } case 'SHUFFLE_BUT': - player.setShuffle(player.shuffle ? false : true); + player.setShuffle(); await buttonReply( interaction, - `Shuffle set to ${player.shuffle ? `on` : `off`}.`, + `Shuffled the queue.`, this.client.color.main ); - await message.edit({ - embeds: [ - embed.setFooter({ - text: `Shuffle set to ${player.shuffle ? `on` : `off`} by ${ - interaction.member.displayName - }`, - iconURL: interaction.member.displayAvatarURL({}), - }), - ], - }); break; case 'PREV_BUT': if (!player.previous) diff --git a/src/structures/Dispatcher.ts b/src/structures/Dispatcher.ts index 6a5a1a075..76d684cad 100644 --- a/src/structures/Dispatcher.ts +++ b/src/structures/Dispatcher.ts @@ -44,7 +44,6 @@ export default class Dispatcher { public requester: User; public repeat: number; public node: Node; - public shuffle: boolean; public paused: boolean; public filters: Array; public autoplay: boolean; @@ -63,7 +62,6 @@ export default class Dispatcher { this.loop = 'off'; this.repeat = 0; this.node = options.node; - this.shuffle = false; this.paused = false; this.filters = []; this.autoplay = false; @@ -133,18 +131,14 @@ export default class Dispatcher { if (this.stopped) return; this.client.shoukaku.emit('playerDestroy', this.player); } - public setShuffle(shuffle: boolean): void { + public setShuffle(): void { if (!this.player) return; - this.shuffle = shuffle; - if (shuffle) { - const current = this.queue.shift(); - this.queue = this.queue.sort(() => Math.random() - 0.5); - this.queue.unshift(current); - } else { - const current = this.queue.shift(); - this.queue = this.queue.sort((a: any, b: any) => a - b); - this.queue.unshift(current); + const queue = this.queue; + for (let i = queue.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [queue[i], queue[j]] = [queue[j], queue[i]]; } + this.queue = queue; } public async skip(skipto = 1): Promise { if (!this.player) return; diff --git a/src/structures/Lavamusic.ts b/src/structures/Lavamusic.ts index 26eb2b336..cf46cab41 100644 --- a/src/structures/Lavamusic.ts +++ b/src/structures/Lavamusic.ts @@ -23,6 +23,8 @@ import loadPlugins from '../plugin/index.js'; import { Utils } from '../utils/Utils.js'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); + + export default class Lavamusic extends Client { public commands: Collection = new Collection(); public aliases: Collection = new Collection(); @@ -37,12 +39,17 @@ export default class Lavamusic extends Client { public queue = new Queue(this); public constructor(options: ClientOptions) { super(options); - this.shoukaku = new ShoukakuClient(this); } public embed(): EmbedBuilder { return new EmbedBuilder(); } public async start(token: string): Promise { + if (this.config.autoNode) { + const nodes = await this.getNodes(); + this.shoukaku = new ShoukakuClient(this, nodes); + } else { + this.shoukaku = new ShoukakuClient(this, this.config.lavalink); + } this.loadCommands(); this.logger.info(`Successfully loaded commands!`); this.loadEvents(); @@ -117,9 +124,9 @@ export default class Lavamusic extends Client { this.config.production === true ? Routes.applicationCommands(this.user.id ?? '') : Routes.applicationGuildCommands( - this.user.id ?? '', - this.config.guildId ?? '' - ); + this.user.id ?? '', + this.config.guildId ?? '' + ); try { const rest = new REST({ version: '9' }).setToken(this.config.token ?? ''); await rest.put(applicationCommands, { body: this.body }); @@ -129,7 +136,22 @@ export default class Lavamusic extends Client { } }); } + private async getNodes(): Promise { + const params = new URLSearchParams({ + ssl: 'false', + version: 'v4', + format: 'shoukaku', + }); + const res = await fetch(`https://lavainfo-api.freedback-dip.workers.dev/nodes?${params.toString()}`, { + headers: { + 'Content-Type': 'application/json', + }, + }); + + const nodes = await res.json(); + return nodes; + } private loadEvents(): void { const eventsPath = fs.readdirSync(path.join(__dirname, '../events')); eventsPath.forEach(dir => { diff --git a/src/structures/Shoukaku.ts b/src/structures/Shoukaku.ts index 4836a68e5..834d91775 100644 --- a/src/structures/Shoukaku.ts +++ b/src/structures/Shoukaku.ts @@ -1,12 +1,12 @@ /* eslint-disable @typescript-eslint/no-unsafe-enum-comparison */ -import { Connectors, Shoukaku } from 'shoukaku'; +import { Connectors, NodeOption, Shoukaku } from 'shoukaku'; import { Lavamusic } from './index.js'; export default class ShoukakuClient extends Shoukaku { public client: Lavamusic; - constructor(client: Lavamusic) { - super(new Connectors.DiscordJS(client), client.config.lavalink, { + constructor(client: Lavamusic, nodes: NodeOption[]) { + super(new Connectors.DiscordJS(client), nodes, { moveOnDisconnect: false, resume: false, reconnectInterval: 30,