• If you need help or want to discuss things, you now can also join us on our Discord Server!
  • A first preview of the unlimited version of SinusBot can be found in the Upcoming Changes thread. A version for Windows will follow, but we don't have a release date, yet.

EN Problem with track

Status
Not open for further replies.

omano

Member
I'm writing a script to ba able to set a command that will play set track.

Here is where I am:

PHP:
/*
*/
registerPlugin({
    name: 'Commandes custom track',
    version: '0',
    description: 'Script supposed to assign a comand/string msg to a track',
    author: 'omano',
    vars: {
                track_1: {
            title: 'Track 1',
            type: 'track',
            placeholder: 'Search for track...'
        },
        track_2: {
            title: 'Track 2',
            type: 'track',
            placeholder: 'Search for track...'
        },
        commande_1: {
            title: 'Command for Track 1',
            type: 'string',
            placeholder: '!com1'
        },
        commande_2: {
            title: 'Command for Track 2',
            type: 'string',
            placeholder: '!com2'
        }
    }
}, function(sinusbot, config, info){
    // -- Load messages --
    log('');
    log('Loading...');
    log('');
    var author = info.author.split(',');
    if(author.length == 1){
        author = author[0];
        author = author.replace(/<.*>/gi, '').trim();
    } else {
        author = author.map(function(e){
            return e.replace(/<.*>/gi, '').trim();
        });
        author = author.join(' & ');
    }
    log(info.name + ' v' + info.version + ' by ' + author + ' for SinusBot v0.9.9-a4f6453 (and above)');
 
 
    //Loading commande 1
    if(typeof config.commande_1 == 'undefined' || config.commande_1 == '') {
        log('No commande 1 set, using default !commande1');
        var commande_1 = "!commande1";
    }else{
        var commande_1 = config.commande_1;
        log('Commande 1: ' + commande_1);
    }
    //Loading commande 2
    if(typeof config.commande_2 == 'undefined' || config.commande_2 == '') {
        log('No commande 2 set, using default !commande2');
        var commande_2 = "!commande2";
    }else{
        var commande_2 = config.commande_2;
        log('Commande 2: ' + commande_2);
    }
    //Loading track 1
    if(typeof config.track_1 == 'undefined' || config.track_1 == '') {
        log('No track 1 set');
    }else{
        var track_1 = config.track_1;
        log('track 1: ' + track_1);
    } 
    //Loading track 2
    if(typeof config.track_2 == 'undefined' || config.track_2 == '') {
        log('No track 2 set');
    }else{
        var track_2 = config.track_2;
        log('track 2: ' + track_2);
    } 

 
    // -- Recreating "startsWith()" function which isn't included in ECMAScript 5 --  from pchannel.js
    if (!String.prototype.startsWith) {
            String.prototype.startsWith = function(searchString, position) {
                position = position || 0;
                return this.indexOf(searchString, position) === position;
            };
    }
 
 
    // -- Commandes persos
    sinusbot.on('chat', function(ev) {
        if (ev.msg.startsWith(commande_1)) {
                play("'" + track_1 + "'");
                log('lecture de ' + track_1);
                }
        else if (ev.msg.startsWith(commande_2)) {
                play(track_2);
                log('lecture de ' + track_2);
                }
    });
 

    log('Loaded all!');
    log('');
 
});


So I have two main problems:

- The track you configure from web interface does not appear to be loaded in webinterface, but I see that it is saved to the database
- The track saved to database is like "artist - title" instead of useable "track://f609f7bf-3022-4f8d-b7f9-02fe6defbeb5" so I can not use the play('track://f609f7bf-3022-4f8d-b7f9-02fe6defbeb5') thing like in my two tries at bottom of script..

Is there a reason why my tracks do not save in the web interface config page (actually they save but does not show up). I had it to work only once after I installed the beta 0.99 bot, but it is no more showing properly.

Is there a way to use track and play to achieve what I want?



//Edit: I just noticed that if you do not click the proposed music track it does not work properly but saves the Artist - Tile to DB... I'mm investigating it will may work in the end..
 
Last edited:

flyth

is reticulating splines
Staff member
Developer
Contributor
Hi,
type 'track' is currently buggy in some browsers, especially firefox. I'm not yet sure why that happens but I need to look into it sometime soon.
It works for me in chrome, but sometimes I need to choose a track, click into a white area somewhere on the page, wait a few seconds and THEN hit save. It's really a mess right now, sorry for that. It will always show the artist / title but save the track:// url once you get it to actually save anything.

Also,
don't use: play("'" + track_1 + "'");
use: play(track_2);
 

omano

Member
I just noticed that if you do not click the proposed music track it does not work properly but saves the Artist - Tile to DB... I'mm investigating it will may work in the end..
 

omano

Member
Ok if you click the proposed track you search in the webinterface, wait a little, click on blank space then, the track field most of time stays green instead of red, and saves correctly to the db and in the webinterface. in the db it saves like that:
Code:
"track_1":{"url":"track://f609f7bf-3022-4f8d-b7f9-02fe6defbeb5/?title=ArtistNameForThisTrack%20-%20TrackTitle","title":"Artist - Title"},

How can I set my var Track_1 to the url track://f609f7bf-3022-4f8d-b7f9-02fe6defbeb5/ stored in settings "url":"track://f609f7bf-3022-4f8d-b7f9-02fe6defbeb5/?title=ArtistNameForThisTrack%20-%20TrackTitle". If I log what contains Track1 it says [object Object], how can I manipulate this?


Thanks a lot for your answers, I want to say that I am not a coder and start learning sinusbot scripts.
 
Last edited:

omano

Member
I just got it working, so basically:

PHP:
/*
*/
registerPlugin({
  name: 'Custom Commands for Tracks/Sounds',
  version: '0.1',
  description: 'Script to assign a command to play a track/sound',
  author: 'omano',
  vars: {
  track_1: {
  title: 'Track/Sound 1',
  type: 'track',
  placeholder: 'Search for track...'
  },
  track_2: {
  title: 'Track/Sound 2',
  type: 'track',
  placeholder: 'Search for track...'
  },
  command_1: {
  title: 'Command users have to type for Track/Sound 1',
  type: 'string',
  placeholder: '!com1'
  },
  command_2: {
  title: 'Command users have to type for Track/Sound 2',
  type: 'string',
  placeholder: '!com2'
  }
  }
}, function(sinusbot, config, info){
  // -- Load messages --
  log('');
  log('Loading...');
  log('');
  var author = info.author.split(',');
  if(author.length == 1){
  author = author[0];
  author = author.replace(/<.*>/gi, '').trim();
  } else {
  author = author.map(function(e){
  return e.replace(/<.*>/gi, '').trim();
  });
  author = author.join(' & ');
  }
  log(info.name + ' v' + info.version + ' by ' + author + ' for SinusBot v0.9.9-a4f6453 (and above)');
   
   
  //Loading command 1
  if(typeof config.command_1 == 'undefined' || config.command_1 == '') {
  log('No command 1 set, using default !com1');
  var command_1 = "!com1";
  }else{
  var command_1 = config.command_1;
  log('Command 1: ' + command_1);
  }
  //Loading command 2
  if(typeof config.command_2 == 'undefined' || config.command_2 == '') {
  log('No command 2 set, using default !com2');
  var command_2 = "!com2";
  }else{
  var command_2 = config.command_2;
  log('Command 2: ' + command_2);
  }
  //Loading track 1
  if(typeof config.track_1 == 'undefined' || config.track_1 == '') {
  log('No Track/Sound 1 set');
  }else{
  var track_1_url = config.track_1.url;
     var track_1_title = config.track_1.title;
  log('track 1: ' + track_1_title + ' -- ' + track_1_url);
  }   
  //Loading track 2
  if(typeof config.track_2 == 'undefined' || config.track_2 == '') {
  log('No Track/Sound 2 set');
  }else{
  var track_2_url = config.track_2.url;
     var track_2_title = config.track_2.title;
  log('track 2: ' + track_2_title + ' -- ' + track_2_url);
  }
   
   
  // -- Recreating "startsWith()" function which isn't included in ECMAScript 5 --  from pchannel.js
  if (!String.prototype.startsWith) {
  String.prototype.startsWith = function(searchString, position) {
  position = position || 0;
  return this.indexOf(searchString, position) === position;
  };
  }
   
   
  // -- Custom Commands
  sinusbot.on('chat', function(ev) {
  if (ev.msg.startsWith(command_1)) {
  play(track_1_url);
  log('playing: ' + track_1_title + ' -- ' + track_1_url);
  }
  else if (ev.msg.startsWith(command_2)) {
  play(track_2_url);
  log('playing: ' + track_2_title + ' -- ' + track_2_url);
  }
  });
   

  log('Loaded all!');
  log('');
   
});


It works :D I finaly fixed it by myself, now it is time to make the script better (with more functionality, and stuff, what I will do, or not, in the next months) but it can be a starting point for someone like me so feel free to use it.
 
Last edited:

Raider51

Member
It doesn't work for me, I made it a javascript via notepad and added it to the scriptfolder. Can someone help me? Latest updates of sinusbot and yt-dl. the bot is running from my computer.
 

omano

Member
I really recommend to use better program than notepad, like Notepad++ (download it here) it will make life easier to better view source code ;) with syntax hilighting for many languages. I made an updated version of the script, this one is on the old API. If you prefer to have my script updated version here it is, I made quick translation in english for the text (original was in French) and it has 5 custom commands you can create, and another one to seek in current track, enjoy:

direct download: https://www.dropbox.com/s/c4lq67o0f0t374i/customcommands.js?dl=1 (open this file in notepad++ and see why it is important to stop using basic notepad ;))


here is direct code in case my dropbox link does not exist anymore in the future:

PHP:
/*
*/
registerPlugin({
    name: 'Custom Commands for Tracks/Sounds',
    version: '2.0',
    description: 'Script to assign a command to play a track/sound',
    author: 'omano',
    vars: {
        command_seek: {
            title: 'Chat command to seek in current track',
            type: 'string',
            placeholder: '!seek'
        }, track1: {
            title: 'Track / Sound 1',
            type: 'track',
            placeholder: 'Search for track...'
        }, track2: {
            title: 'Track / Sound 2',
            type: 'track',
            placeholder: 'Search for track...'
        }, track3: {
            title: 'Track / Sound 3',
            type: 'track',
            placeholder: 'Search for track...'
        }, track4: {
            title: 'Track / Sound 4',
            type: 'track',
            placeholder: 'Search for track...'
        }, track5: {
            title: 'Track / Sound 5',
            type: 'track',
            placeholder: 'Search for track...'
        }, command1: {
            title: 'Chat command to type for Track / Sound 1',
            type: 'string',
            placeholder: '!com1'
        }, command2: {
            title: 'Chat command to type for Track / Sound 2',
            type: 'string',
            placeholder: '!com2'
        }, command3: {
            title: 'Chat command to type for Track / Sound 3',
            type: 'string',
            placeholder: '!com3'
        }, command4: {
            title: 'Chat command to type for Track / Sound 4',
            type: 'string',
            placeholder: '!com4'
        }, command5: {
            title: 'Chat command to type for Track / Sound 5',
            type: 'string',
            placeholder: '!com5'
        }
    }
}, function(sinusbot, config, info){

    var engine = require('engine');
    var event = require('event');

    engine.log('-------------------');
    engine.log('Loading  ' + info.name + ' ...');
    engine.log('-------------------');

    //Loading seek command
    if(typeof config.command_seek == 'undefined' || config.command_seek == '') {
        engine.log('No seek command  set, using default !seek');
        var command_seek = "!seek";
    } else {
        var command_seek = config.command_seek;
        engine.log('Seek Command: ' + config.command_seek);
    }
    //Loading command 1
    if(typeof config.command1 == 'undefined' || config.command1 == '') {
        engine.log('No command 1 set, using default !com1');
        var command1 = "!com1";
    } else {
        var command1 = config.command1;
        engine.log('Command 1: ' + command1);
    }
    //Loading command 2
    if(typeof config.command2 == 'undefined' || config.command2 == '') {
        engine.log('No command 2 set, using default !com2');
        var command2 = "!com2";
    } else {
        var command2 = config.command2;
        engine.log('Command 2: ' + command2);
    }
    //Loading command 3
    if(typeof config.command3 == 'undefined' || config.command3 == '') {
        engine.log('No command 3 set, using default !com3');
        var command3 = "!com3";
    } else {
        var command3 = config.command3;
        engine.log('Command 3: ' + command3);
    }
    //Loading command 4
    if(typeof config.command4 == 'undefined' || config.command4 == '') {
        engine.log('No command 4 set, using default !com4');
        var command4 = "!com4";
    } else {
        var command4 = config.command4;
        engine.log('Command 4: ' + command4);
    }
    //Loading command 5
    if(typeof config.command5 == 'undefined' || config.command5 == '') {
        engine.log('No command 5 set, using default !com5');
    var command5 = "!com5";
    } else {
        var command5 = config.command5;
        engine.log('Command 5: ' + command5);
    }
    engine.log('-------------------');
    engine.log('TRACKS :');
    engine.log('-------------------');
    //Loading track 1
    if(typeof config.track1 == 'undefined' || config.track1 == '') {
        engine.log('No Track/Sound 1 set');
    } else {
        engine.log('Track/Sound 1: ' + config.track1.title + ' - ' + config.track1.url);
    }
    //Loading track 2
    if(typeof config.track2 == 'undefined' || config.track2 == '') {
        engine.log('No Track/Sound 2 set');
    } else {
        engine.log('Track/Sound 2: ' + config.track2.title + ' - ' + config.track2.url);
    }
    //Loading track 3
    if(typeof config.track3 == 'undefined' || config.track3 == '') {
        engine.log('No Track/Sound 3 set');
    } else {
        engine.log('Track/Sound 3: ' + config.track3.title + ' - ' + config.track3.url);
    }
    //Loading track 4
    if(typeof config.track4 == 'undefined' || config.track4 == '') {
        engine.log('No Track/Sound 4 set');
    } else {
        engine.log('Track/Sound 4: ' + config.track4.title + ' - ' + config.track4.url);
    }
    //Loading track 5
    if(typeof config.track5 == 'undefined' || config.track5 == '') {
        engine.log('No Track/Sound 5 set');
    } else {
        engine.log('Track/Sound 5: ' + config.track5.title + ' - ' + config.track5.url);
    }
    engine.log('-------------------');
    // -- Recreating "startsWith()" function which isn't included in ECMAScript 5 --  from pchannel.js
    if (!String.prototype.startsWith) {
            String.prototype.startsWith = function(searchString, position) {
                position = position || 0;
                return this.indexOf(searchString, position) === position;
            };
    }
    // -- Custom Commands
    event.on('chat', function(ev) {
   
        var media = require('media');
        var audio = require('audio');
    
// if your bot is currently playing a track before you type command, it will use this ephemeral parameter to play your custom track over the current one
        if (audio.isPlaying() == true) {
            var ephemeral = "?ephemeral=true&volume=-90"; // ...ephemeral parameter is bugged, it is in theory from -100 to 100 (volume level), but in reality for now -100=0 -90=10 -80=20 so if your bot sound level is 15 and you want the custom track to play at same level modify current -90 value accordingly (-90 is to play at same level if bot has sound level at 10)..
        } else {
            var ephemeral = "?ephemeral=false";
        }
        if(ev.text.startsWith(command_seek)) {
            var buffer = ev.text.replace(command_seek, '').trim();
            var buffer2 = parseInt(buffer, 10);
            if (isNaN(buffer2) == false) {
                var buffer3 = buffer2 * 1000;
                if(audio.isPlaying() == true) {
                audio.seek(buffer3);
                }
            } else {
                ev.client.chat('Usage of command  ' + command_seek + ': send to the bot the message \" ' + command_seek + ' xxx \" where \" xxx \" is the track time/position in seconds (example: \" ' + command_seek + ' 145 \" will seek current playing track at 145 seconds, which means 2 minutes and 25 seconds).');
                }
        }
        if (ev.text.startsWith(command1)) {
            media.playURL(config.track1.url + ephemeral);
            engine.log('playing: ' + track1_title + ' - ' + track1_url);
        } else if (ev.text.startsWith(command2)) {
            media.playURL(config.track2.url + ephemeral);
            engine.log('playing: ' + track2_title + ' - ' + track2_url);
        } else if (ev.text.startsWith(command3)) {
            media.playURL(config.track3.url + ephemeral);
            engine.log('playing: ' + track3_title + ' - ' + track3_url);
        } else if (ev.text.startsWith(command4)) {
            media.playURL(config.track4.url + ephemeral);
            engine.log('playing: ' + track4_title + ' - ' + track4_url);
        } else if (ev.text.startsWith(command5)) {
            media.playURL(config.track5.url + ephemeral);
            engine.log('playing: ' + track5_title + ' - ' + track5_url);
        }
    });
    engine.log('... Loaded ' + info.name);
    engine.log('-------------------');
});

edit: if you don't want the bot to play your custom tracks over the currently playing one (it is the default behavior I prefer it like that to just play my sound effects over the current track), delete line 157 to line 162, it should work and play your custom track only instead of having both playing at same time.
 
Last edited:

Raider51

Member
Thank you for your quick response. I'm glad I can use your script. (and I downloaded notepad++ as you advised me to do) :)
 
Status
Not open for further replies.
Top