Online Sequencer Forums

Full Version: Simple OS Chatbot
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Want to make your own chatbot? Void already posted chatbot code, so I assume it is okay for me to post mine.
Code:
// ==UserScript==
// @name         Frank's Chatbot
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  try to take over the world! maybe some musical stuff, too
// @author       Frank
// @match        https://onlinesequencer.net/chat/
// @grant        none
// @run-at       context-menu
// ==/UserScript==

(function() {
   'use strict';
   var botKeyword = '!do ';
   var lastChatId;
   var lastUser;
   function Command(name, desc, code){
       this.name=name;
       this.desc=desc;
       this.code=code;
       this.toString = () => this.name
   }
   var commands = {
       me: new Command('me', '!do me --> You are ...', function(command){
           post('You are ' + lastUser)
       }),
       random: new Command('random', '!do random --> ##random (in key)', function(command){
           $.get('https://onlinesequencer.net/ajax/random.php', function(r){
               post('##'+r.slice(r.search(/="\/\d/)+3,r.search(/\d" o/)+1))
           })
       }),
       stop: new Command('stop', '!do stop --> stops bot', function(command){
           clearInterval(clearId);
           post('Stopped.')
       }),
       help: new Command('help', '!do help | [command] --> gives desc. of [command]', function(command){
           var helped = commands[command[1]]
           if(typeof command[1] == 'undefined') post('!do [command] | [arg1] | [arg2] ... "!do help | help" for help on help. All Commands: ' + Object.keys(commands).join(', '))
           else post(helped?helped.desc:'Unknown command. Cannot help.')
       })
}

   function tickBot() {
       //get latest chat message
       var lastChat = document.getElementById('chat_table').lastElementChild;

       //check if it is new
       if (lastChat.id == lastChatId) {
           lastChatId = lastChat.id;
           return
       } else {
           lastChatId = lastChat.id
       }

       //parse the chat message
       lastUser = lastChat.children[0].children[0].innerText;
       var lastMessage = lastChat.children[1].innerText;

       //logic and send
       if (lastMessage.substr(0, botKeyword.length) == botKeyword) {
           var command = lastMessage.slice(botKeyword.length).split(' | ');
           if(commands[command[0]]) commands[command[0]].code(command, lastUser)
           else post('Invalid command. "!do help" for help.')
       }
   }

   function post(message) {
       var message1 = '​' + message
       if (message1.length > 256) return post('Tried to post; message too long.')
       $.post('xmlhttp.php?action=ajaxchat_send', {
           message: message1
       })
   }

   if($.post) {post('Started. "!do help" for help.');
               var clearId = setInterval(tickBot, 500)
               }
   else console.log('Not in IFrame')
})()
You can also comment suggestions for me or your own tweaks to my code.
What commands do you have planned for the future? I'm not sure how much a bot can help with making or listening to music. Maybe some quick references for scales and chords, motif generation etc?
Maybe you could add a command that selects and displays a sequence from the profile of the person who uses the command, based on how many plays or notes it has, or just randomly. That would be lit
Both suggestions so far are surprisingly applicable and amazing.
Current changelog:
Added Eandrew's suggestion.
Code:
// ==UserScript==
// @name         Frank's Chatbot
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  try to take over the world! maybe some musical stuff, too
// @author       Frank
// @match        https://onlinesequencer.net/chat/
// @grant        none
// @run-at       context-menu
// ==/UserScript==

(function() {
    'use strict';
    var botKeyword = '!do ';
    var lastChatId;
    var lastUser;
    function Command(name, desc, code){
        this.name=name;
        this.desc=desc;
        this.code=code;
        this.toString = () => this.name
    }
    var commands = {
        me: new Command('me', '!do me --> You are ...', function(command){
            post('You are ' + lastUser)
        }),
        random: new Command('random', '!do random [user id] --> ##random (in key / featured from [user id])', function(command){
            if(typeof command[1] == 'undefined') {
                $.get('https://onlinesequencer.net/ajax/random.php', function(r){
                    post('##'+r.slice(r.search(/="\/\d/)+3,r.search(/\d" o/)+1))
                })
            }
            else {
                $.get('https://onlinesequencer.net/ajax/random.php?user='+command[1], function(r){
                    var userArg = r.slice(r.search('More by'),r.search('</a>\n'));
                    if (userArg) post(userArg+': ##'+r.slice(r.search(/="\/\d/)+3,r.search(/\d" o/)+1))
                    else post('Unknown User Id')
                })
            }
        }),
        stop: new Command('stop', '!do stop --> stops bot', function(command){
            clearInterval(clearId);
            post('Stopped.')
        }),
        help: new Command('help', '!do help | [command] --> gives desc. of [command]', function(command){
            var helped = commands[command[1]]
            if(typeof command[1] == 'undefined') post('!do [command] | [arg1] | [arg2] ... "!do help | help" for help on help. All Commands: ' + Object.keys(commands).join(', '))
            else post(helped?helped.desc:'Unknown command. Cannot help.')
        })
    }

    function tickBot() {
        //get latest chat message
        var lastChat = document.getElementById('chat_table').lastElementChild;

        //check if it is new
        if (lastChat.id == lastChatId) {
            lastChatId = lastChat.id;
            return
        } else {
            lastChatId = lastChat.id
        }

        //parse the chat message
        lastUser = lastChat.children[0].children[0].innerText;
        var lastMessage = lastChat.children[1].innerText;

        //logic and send
        if (lastMessage.substr(0, botKeyword.length) == botKeyword) {
            var command = lastMessage.slice(botKeyword.length).split(' | ');
            if(commands[command[0]]) commands[command[0]].code(command, lastUser)
            else post('Invalid command. "!do help" for help.')
        }
    }

    function post(message) {
        var message1 = '​' + message
        if (message1.length > 256) return post('Tried to post; message too long.')
        $.post('xmlhttp.php?action=ajaxchat_send', {
            message: message1
        })
    }

    if($.post) {post('Started. "!do help" for help.');
                var clearId = setInterval(tickBot, 500)
                }
    else console.log('Not in IFrame')
})()
0.4 - added markov chains! Copy and paste them for ideas!
Code:
// ==UserScript==
// @name         Frank's Chatbot
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  try to take over the world! maybe some musical stuff, too
// @author       Frank
// @match        https://onlinesequencer.net/chat/
// @grant        none
// @run-at       context-menu
// ==/UserScript==

(function() {
    'use strict';
    var botKeyword = '!do ';
    var lastChatId;
    var lastUser;
    function Command(name, desc, code){
        this.name=name;
        this.desc=desc;
        this.code=code;
        this.toString = () => this.name
    }
    var commands = {
        me: new Command('me', 'me --> You are ...', function(command){
            post('You are ' + lastUser)
        }),
        random: new Command('random', 'random | [user id] --> ##random (in key / featured from [user id])', function(command){
            if(typeof command[1] == 'undefined') {
                $.get('https://onlinesequencer.net/ajax/random.php', function(r){
                    post('##'+r.slice(r.search(/="\/\d/)+3,r.search(/\d" o/)+1))
                })
            }
            else {
                $.get('https://onlinesequencer.net/ajax/random.php?user='+command[1], function(r){
                    var userArg = r.slice(r.search('More by'),r.search('</a>\n'));
                    if (userArg) post(userArg+': ##'+r.slice(r.search(/="\/\d/)+3,r.search(/\d" o/)+1))
                    else post('Unknown User Id')
                })
            }
        }),
        markov: new Command('markov', 'markov | [notes] --> generates a markov chain of [notes] notes which you can copy', function(command){
            function txtNote(time, type) {
                this.time = time;
                this.type = type;
                this.toString = () => time + ' ' + types[type] + ' 1 0;'
            }
            var notes = [new txtNote(0, 0), new txtNote(1, Math.floor(3 * Math.random()))];
            var types = ['A5', 'D5', 'G5'];
            var chainData = {
                '00': [0.18, 0.6, 0.22],
                '01': [0.5, 0.5, 0],
                '02': [0.15, 0.75, 0.1],
                '10': [0, 0, 1],
                '11': [0.25, 0, 0.75],
                '12': [0.9, 0.1, 0],
                '20': [0.4, 0.4, 0.2],
                '21': [0.5, 0.25, 0.25],
                '22': [1, 0, 0]
            }
            var loop = 16;
            if(!isNaN(parseInt(command[1]))) loop = parseInt(command[1]);
            for (var time = 2; time < loop; time++) {
                var past = notes.slice(-2).map(x => x.type).join('');
                var rand = Math.random();
                var type = 0;
                while (rand >= chainData[past][type]) {
                    rand -= chainData[past][type];
                    type++
                }
                notes.push(new txtNote(time, type))
            }
            post('Online Sequencer:1:' + notes.join(''))
        }),
        stop: new Command('stop', 'stop --> stops bot', function(command){
            clearInterval(clearId);
            post('Stopped.')
        }),
        help: new Command('help', 'help | [command] --> gives desc. of [command]', function(command){
            var helped = commands[command[1]]
            if(typeof command[1] == 'undefined') post(botKeyword+'[command] | [arg1] | [arg2] ... "'+botKeyword+'help | help" for help on help. All Commands: ' + Object.keys(commands).join(', '))
            else post(helped?botKeyword+helped.desc:'Unknown command. Cannot help.')
        })
    }

    function tickBot() {
        //get latest chat message
        var lastChat = document.getElementById('chat_table').lastElementChild;

        //check if it is new
        if (lastChat.id == lastChatId) {
            lastChatId = lastChat.id;
            return
        } else {
            lastChatId = lastChat.id
        }

        //parse the chat message
        lastUser = lastChat.children[0].children[0].innerText;
        var lastMessage = lastChat.children[1].innerText;

        //logic and send
        if (lastMessage.substr(0, botKeyword.length) == botKeyword) {
            var command = lastMessage.slice(botKeyword.length).split(' | ');
            if(commands[command[0]]) setTimeout(commands[command[0]].code,0,command)
            else post('Invalid command. "'+botKeyword+'help" for help.')
        }
    }

    function post(message) {
        var message1 = '​' + message
        if (message1.length > 256) return post('Tried to post; message too long.')
        $.post('xmlhttp.php?action=ajaxchat_send', {
            message: message1
        })
    }

    if($.post) {post('Started. "'+botKeyword+'help" for help.');
                var clearId = setInterval(tickBot, 500)
                }
    else console.log('Not in IFrame')
})()
Now use a neural network, and train it on the chat logs so you can generate entire conversations! http://karpathy.github.io/2015/05/21/rnn-effectiveness/
how do you use this?