Jump to content
Admin

Code tutorials

Recommended Posts

Making your own command processor

 

In this function:

  function Receive(u, j) {

add:

  if(j.action == 'message')
  {
    doCommands(j.elements.t, u);
    return;
  }

then add something like this:

  doCommands(u, m)
  {
    let i,id
    if(!m) return; // ignore blank messages
    if(m.charAt(0) != '@') return; // first character must be @
    m = m.toLowerCase().substr(1).split(' '); // Split the command line into an array
    switch(m[0])
    {
      case 'start' :
        main(); // call game main to restart
        break;
      case "add":
      case 'invite' :
        if(m[1] == 'off') { delete this.bot.invited; return; } // turn off invite mode
        if(!this.bot.invited) this.bot.invited = {}; // invited mode on
        id = this.xInt(m[1]);
        if(id && this.bot.users[id]) { this.bot.invited[id] = true; return; }
        id = m[1].toLowerCase();
        for(i in this.bot.users)
          if(id == this.bot.users[i].regname) { this.bot.invited[i] = true; return; }
        this.bot.sendMessage(m[1].replace(/[^A-Za-z0-9_]/g, '')+' not in the chat');
        break;
      case "remove":
      case "uninvite":
        if(!this.bot.invited) return;
        id = this.xInt(m[1]);
        if(id && this.bot.invited[id]) { delete this.bot.invited[id]; return; }
        id = m[1].toLowerCase();
        for(i in this.bot.users)
          if(id == this.bot.users[i].regname) { delete this.bot.invited[i]; return; }
        break;
      case "list":
        let cMsg = '';
        if(!this.bot.invited)
          cMsg = "Invites are off";
        else
        {
          for(id in this.bot.invited)
            cMsg += (this.bot.users[id].regname ? this.bot.users[id].regname : id)+' ';
          if(cMsg)
            cMsg = "Invited users: "+cMsg;
          else
            cMsg = "No invited users";
        }
        this.bot.sendMessage(cMsg);
        break;
    }
  }

This is a basic command manager. You can add commands if you want or change the messages.

 

You could also add 

if(u != 42) // Only allow 42 to control this game
{
  bot.sendMessage("You need to know the answer to xat, the universe and everything to control this game!");
  return;
}

Note do not use

lib.Init({Commands:'on'});

or the library will handle commands for you

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.