• If you need help or want to discuss things, you now can also join us on our Discord Server!

EN Random command

guillaumearnx

New Member
Hello, I want to create a random command with SinusBot
I've tried differents methods but none works
JavaScript:
 command.createCommand('random')
        .help('Play a random track')
        .manual('Play a random track.')
        .checkPermission(requirePrivileges(PLAYBACK))
        .exec((client, args, reply, ev) => {
            var musicos = media.getTracks();   
            var indexation = require('helpers').getRandom(musicos.length);
            let track = media.getTrackByID(musicos[indexation].uuid).play();
            reply(`Playing ${formatTrack(track)}`);
            successReaction(ev, reply);
        });

        command.createCommand('random2')
        .help('Play a random track')
        .manual('Play a random track.')
        .checkPermission(requirePrivileges(PLAYBACK))
        .exec((client, args, reply, ev) => {
            var tracks = media.getTracks();   
            var track = require('helpers').getRandom(tracks.length);
            /*reply(`Playing ${formatTrack(track)}`);*/
            successReaction(ev, reply);
        });
Error for random1 is :
2020-05-16T21:16:04+02:00 [ command:41:14] TypeError: Cannot read property 'play' of undefined at command.createCommand.help.manual.checkPermission.exec (sinusbot-commands:408:69)
2020-05-16T21:16:04+02:00 [ command:41:14] This is _probably_ an Error with a Script which is using command.js!
2020-05-16T21:16:04+02:00 [ command:41:14] guillaumearnx (689576146765086830/629786143906594828) used $random

Error for random2 is :
2020-05-16T21:16:08+02:00 [ command:41:14] guillaumearnx (689576146765086830/629786143906594828) used $random2
 

TwentyFour

BinusSot Junkie
V.I.P.
Contributor
Insider
Error for random1 is :
2020-05-16T21:16:04+02:00 [ command:41:14] TypeError: Cannot read property 'play' of undefined at command.createCommand.help.manual.checkPermission.exec (sinusbot-commands:408:69)
2020-05-16T21:16:04+02:00 [ command:41:14] This is _probably_ an Error with a Script which is using command.js!
2020-05-16T21:16:04+02:00 [ command:41:14] guillaumearnx (689576146765086830/629786143906594828) used $random
media.getTrackByID(musicos[indexation].uuid) is undefined, so you're prolly giving a wrong ID to get the track.
Error for random2 is :
2020-05-16T21:16:08+02:00 [ command:41:14] guillaumearnx (689576146765086830/629786143906594828) used $random2
That's not an error, it's just the output of your successReaction(ev, reply); function.
 

guillaumearnx

New Member
media.getTrackByID(musicos[indexation].uuid) is undefined, so you're prolly giving a wrong ID to get the track.

That's not an error, it's just the output of your successReaction(ev, reply); function.
I know for random2 but i dont have any output, and for random 1, ive defined musicos and indexation no ?
 

TwentyFour

BinusSot Junkie
V.I.P.
Contributor
Insider
It doesn't matter if you define them, it matter what their value is ... it must be a valid string of a track-id and ofc the track must be existing. Otherwise you return null.

It feels, you have absolutely no clue what your doing there. Looking at your "random2", what do you expect?
JavaScript:
var tracks = media.getTracks();
var track = require('helpers').getRandom(tracks.length);
/*reply(`Playing ${formatTrack(track)}`);*/
successReaction(ev, reply);
1. variable tracks is an array of track-objects
2. variable track is a number between 0 and the amount of entrys in tracks
3. is commented out anyways
4. is the reason you get the single line of output

Only in line 4 there is any output or reaction to be found ... everything else is just storing variables.

You prolly look for sth as:
media.playURL(tracks[randomID].url())
with var randomID = Math.floor(Math.random() * tracks.length);.
Dunno if getRandom returns only integers, that's why I solve it without it anyways.
 
Last edited:

guillaumearnx

New Member
JavaScript:
command.createCommand('random')
        .help('Play a random track')
        .manual('Play a random track.')
        .checkPermission(requirePrivileges(PLAYBACK))
        .exec((client, args, reply, ev) => {
            var tracks = media.getTracks();   
            var indexation = require('helpers').getRandom(tracks.length);
            let track = media.getTrackByID(tracks[indexation].uuid);
            track.play();
            reply(`Playing ${formatTrack(track)}`);
            successReaction(ev, reply);
        });
Here, "track" is a song selected randomly in the tracks.length ?
 

TwentyFour

BinusSot Junkie
V.I.P.
Contributor
Insider
Looks better, but take care:
If your amount of tracks is e.g. 13. Then length will be 13, but your indexation may not exceed 0-12. That's why I suggested you the way with the randomID.

Also you don't need to get the track with getTrackByID, since you already have your tracks in the tracks array.
JavaScript:
var t1 = media.getTrackByID(tracks[indexation].uuid);
// is 100% equal to
var t2 = tracks[indexation];
// meaning
(t1 === t2) // TRUE
 

guillaumearnx

New Member
It works with
JavaScript:
command.createCommand('random')
        .help('Play a random track')
        .manual('Play a random track.')
        .checkPermission(requirePrivileges(PLAYBACK))
        .exec((client, args, reply, ev) => {
            var tracks = media.getTracks();   
            var randomID = Math.floor(Math.random() * tracks.length);
            media.playURL(tracks[randomID].url())
            successReaction(ev, reply);
        });
With the other commands, the bot responds thanks to :
JavaScript:
reply(`Playing ${formatTrack(track)}`);
But here, "track" is not defined. How to get track metadata ?
 

Similar threads

Top