Skip to content

Commit

Permalink
Fix shuffle functionality in Shuffle.ts command
Browse files Browse the repository at this point in the history
- added auto node from api
  • Loading branch information
appujet committed Apr 13, 2024
1 parent febc573 commit 378c330
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 37 deletions.
10 changes: 6 additions & 4 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/commands/info/Help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <command>\` 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 <command>\` to get more info on a command.`
)
.setFooter({
text: `Use ${prefix.prefix}help <command> for more info on a command`,
Expand Down
2 changes: 1 addition & 1 deletion src/commands/music/Shuffle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`)],
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 2 additions & 12 deletions src/events/client/SetupButtons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 6 additions & 12 deletions src/structures/Dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>;
public autoplay: boolean;
Expand All @@ -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;
Expand Down Expand Up @@ -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<void> {
if (!this.player) return;
Expand Down
30 changes: 26 additions & 4 deletions src/structures/Lavamusic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any> = new Collection();
public aliases: Collection<string, any> = new Collection();
Expand All @@ -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<void> {
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();
Expand Down Expand Up @@ -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 });
Expand All @@ -129,7 +136,22 @@ export default class Lavamusic extends Client {
}
});
}
private async getNodes(): Promise<any> {
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 => {
Expand Down
6 changes: 3 additions & 3 deletions src/structures/Shoukaku.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit 378c330

Please sign in to comment.