• 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 Help for converting to new script API

Status
Not open for further replies.

omano

Member
Hello there, so first I would like to say I'm not a coder. I took lot of time before to create a few scripts for my personal use, looking how other scripts were made, on the older API version. Most of my scripts still work, but the last one I created had weird bugs. Someone here (where I linked to my original script) https://forum.sinusbot.com/threads/ev-clientnick-problem-in-my-script.3512/#post-19139 adviced me to use the new API as I'm using the old one. I tried to understand how it worked but to me it seems more complicated than expected and I can not succeed in converting my old script.

Here is a simplified version of the script:

Code:
registerPlugin({
   name: 'Client Leave Music or Sentence',
   version: '1',
   description: 'Script to play track or say something on client leave',
   author: '®omano',
   vars: {   }
}, function(sinusbot, config, info){
   sinusbot.on('clientMove', function(ev){
     if (ev.clientUid == '4Wa3D3xeXWzeXLWvoAwZJLvTnBw=' && ev.oldChannel == sinusbot.getCurrentChannelId()) {
       play('track://e06d0578-f567-41f5-8503-40224fb33acb');
     } else { if (ev.oldChannel == sinusbot.getCurrentChannelId()) {
       say ('client ' + ev.clientNick + ' left the channel');}
     }
   });
});

I would like someone to help me convert this little piece of simplified script to the new API, from there I think I could get all my scripts to the new API. I tried to convert my script and so far I was only able to have the bot say the sentence when any client moves (not only from the bot channel) and I couldn't have the information if the bot is playing a track https://www.sinusbot.com/docs/scripting/#Audio.isPlaying or couldn't have playURL https://www.sinusbot.com/docs/scripting/#Media.playURL to work, couldn't retrieve the current bot channel https://www.sinusbot.com/docs/scripting/#Backend.getCurrentChannel and so on. I really have a lack of knowledge here, I know, and there is surely only a little something i didn't understand to be able to use the new API

Really appreciate anyone guiding me in the right direction :)
 
Last edited:

xBarkeeper

Member
I think this will work but im not sure ^^
Code:
registerPlugin({
   name: 'Client Leave Music or Sentence',
   version: '1',
   description: 'Script to play track or say something on client leave',
   author: '®omano',
   vars: {   }
}, function(){
   
    var backend = require('backend');
    var event = require('event');
   
    event.on('clientMove', function(ev){
       
        var uid = ev.client.uid();
       
        if (uid == '4Wa3D3xeXWzeXLWvoAwZJLvTnBw=' && ev.fromChannel == backend.getCurrentChannel())
        {
            playURL('track://e06d0578-f567-41f5-8503-40224fb33acb');
        }
        else
        {
            if (ev.fromChannel() == backend.getCurrentChannel()) {
                say ('client ' + ev.nick + ' left the channel');
            }
        }
   });
   
});
 

Tuetchen

Diesmon Dominator
is awesome!
Contributor
Insider
its ev.client.name() and i guess you have to compare ev.fromChannel.id() and backend.getCurrentChannel().id()
 

omano

Member
Hello guy! Thanks for all the replies, I will look into it as soon as possible but I recently had big internet issues and until everything is not sorted on this side I will not work on this. I think I can have it to work now with all the suggestion in your replies, it is really different than before and for someone who don't know shit about coding, transition is not easy :D
 

omano

Member
Here is the progress so far, all this is working, but it is not exactly what I need because there is still no comparision with the original channel the client is moving from:

Code:
registerPlugin({
   name: 'Client Leave Music or Sentence',
   version: '2',
   description: 'Script to play track or say something on client leave',
   author: '®omano',
   vars: {   }
}, function(){

    var engine = require('engine');
    var backend = require('backend');
    var event = require('event');
    var audio = require('audio');
    var media = require('media');

    event.on('clientMove', function(ev){
     
        var uid = ev.client.uid();
     
        if (uid == '4Wa3D3xeXWzeXLWvoAwZJLvTnBw=')
        {
            media.playURL('track://e06d0578-f567-41f5-8503-40224fb33acb');
        }
        else
        {
                audio.say('client ' + ev.client.name() + ' left the channel');
        }
        engine.log('client name: ' + ev.client.name());
        engine.log('bot current channel: ' + backend.getCurrentChannel().id());
        engine.log('client move from channel: ' + ev.fromChannel);
   });
 
});

What I can't have to work and this is why I removed in my script example above, is the MoveInfo thing, it is not working as the other things, documentation says:
MoveInfo
new MoveInfo()
Properties
fromChannel (Channel) : the old channel (or null if the client just got online / changed visibility)
toChannel (Channel) : the new channel (or null if the client just went offline / changed visibility)
client (Client) : the moving client
invoker (Client) : the client that invoked the move

I think it works completely different than the other things, because I see that: new MoveInfo(). Can someone show me a working example of this to use fromChannel? because all I get for now is 'undefined' or other no members or other errors in my different tries. What I would like is to retreive the channel id the client has moved from. In my example script it logs this:

Code:
client move from channel: [object Object]

So it seems there is something there but I don't know how to use it.
 

omano

Member
I'm getting closer, by modifying the logging, I got this:
Code:
engine.log(ev.fromChannel);
Channel{ ID: <45>, Name: <\o/-- ЪAℓTOMAИO --\o/> }


EDIT: woooohhoooooo!! I got it! ev.fromChannel.id() gives me the channel ID :)


Here is the working script with all I needed, now I will just expand it to match my old full script, thanks all for the help ;)

Code:
registerPlugin({
    name: 'Client Leave Music or Sentence',
    version: '2',
    description: 'Script to play track or say something on client leave',
    author: '®omano',
    vars: {   }
}, function(){

    var engine = require('engine');
    var backend = require('backend');
    var event = require('event');
    var audio = require('audio');
    var media = require('media');
  
    event.on('clientMove', function(ev){
      
        var uid = ev.client.uid();
        var uname = ev.client.name();
        var fromchan = ev.fromChannel.id();
        var botchan = backend.getCurrentChannel().id();
      
        if (uid == '4Wa3D3xeXWzeXLWvoAwZJLvTnBw=' && fromchan == botchan)
        {
            media.playURL('track://e06d0578-f567-41f5-8503-40224fb33acb');
        }
        else
        {
            if (fromchan == botchan)
            {
                audio.say('client ' + ev.client.name() + ' left the channel');
            }
        }
        engine.log('client name: ' + uname);
        engine.log('bot current channel: ' + botchan);
        engine.log('client moved from channel: ' + fromchan);
    });

});
 
Last edited:
Status
Not open for further replies.
Top