Guest User

Mail script SB

a guest
Oct 16th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. registerPlugin({
  2.     name: 'Mail',
  3.     version: '1.0',
  4.     description: 'Allows you to leave mail for offline users!',
  5.     author: 'Mashiro-chan',
  6.     vars: {
  7.         adminUids: {
  8.             title: 'Comma-separated list of client-uids that the bot should accept admin commands from',
  9.             type: 'string'
  10.         }
  11.     }
  12. }, function (sinusbot, config) {
  13.    
  14.     function toId(string) {
  15.         return string.toLowerCase().replace(/[^a-z0-9]/g, '');
  16.     }
  17.    
  18.     function checkTime(i) {
  19.         if (i < 10) {
  20.             i = "0" + i;
  21.         }
  22.         return i;
  23.     }
  24.  
  25.     var store = require('store');
  26.     var engine = require('engine');
  27.     var event = require('event');
  28.     var backend = require('backend');
  29.     var dateFormat = require('dateformat');
  30.    
  31.     var mail = {};
  32.    
  33.     if (store.get('mail') != undefined)
  34.         mail = store.get('mail');
  35.    
  36.     engine.log("Loaded mail: " + JSON.stringify(mail));
  37.    
  38.     var uids = [];
  39.     if (config && config.adminUids) uids = config.adminUids.split(",");
  40.  
  41.     event.on('clientMove', function (ev) {
  42.         var nick = toId(ev.clientNick);
  43.         if (mail[nick] == undefined) {
  44.             mail[nick] = [];
  45.             store.set('mail', mail);
  46.         }
  47.     });
  48.     event.on('chat', function (ev) {
  49.         var parts = ev.msg.split(" ");
  50.         var cmd = parts.shift();
  51.         switch (cmd) {
  52.             case '!mhelp':
  53.             case '!mailhelp':
  54.             case '!helpmail':
  55.                 sendMessage(ev.clientId, "[u]Commands[/u]:\n[b]!nicks[/b] - get a list of users you can mail\n[b]!read[/b] - read your mail if you have it, deletes immediately\n[b]!send <nick> <msg>[/b] - sends mail to a user in the mail system");
  56.                 break;
  57.             case '!msg':
  58.             case '!mail':
  59.             case '!send':
  60.                 if (parts.length < 2) {
  61.                     sendMessage(ev.clientId, 'Usage: !send <nick|boxId> <message>');
  62.                     break;
  63.                 }
  64.                 var name = parts.shift();
  65.                 var id = toId(name);
  66.                
  67.                 /*
  68.                 Put aliases here!
  69.                
  70.                 For example, if their TeamSpeak username was "Infiniti | ??????"
  71.                 and you want users to be able to type simply "infin" or "bob":
  72.                
  73.                 if (id == "infin" || id == "bob") id = "infiniti";
  74.                 */
  75.                
  76.                 var msg = parts.join(' ');
  77.                 if (mail[id] == undefined) {
  78.                     sendMessage(ev.clientId, name + " either doesn't have an account with me or you spelled it wrong.\nUse !nicks to see the nicks that are registered with me.");
  79.                 } else if (mail[id].length >= 10) {
  80.                     sendMessage(ev.clientId, '[i][color=red]ERROR![/color] [color=gray]' + name + ' has no more room in their mailbox! ;~;[/color][/i]');
  81.                 } else {
  82.                     var today = new Date();
  83.                     var h = today.getHours();
  84.                     var m = today.getMinutes();
  85.                     var s = today.getSeconds();
  86.                     h = checkTime(h);
  87.                     m = checkTime(m);
  88.                     s = checkTime(s);
  89.                    
  90.                     msg = '[' + h + ':' + m + ':' + s + '] ' + ev.clientNick + ': ' + msg;
  91.                     mail[id].push(msg);
  92.                     sendMessage(ev.clientId, '[i][color=gray]Message sent![/color][/i]');
  93.                 }
  94.                 store.set('mail', mail);
  95.                 break;
  96.             case '!nicks':
  97.                 if (mail != undefined && Object.keys(mail).length) {
  98.                     var sMsg = 'Registered users are:\n';
  99.                     for (var name in mail) {
  100.                         sMsg += name + '\n';
  101.                     }
  102.                     sendMessage(ev.clientId, sMsg);
  103.                 } else {
  104.                     sendMessage(ev.clientId, '[i][color=red]ERROR![/color] [color=gray]There are no users in the mail system! ;~;[/color][/i]');
  105.                 }
  106.                 break;
  107.             case '!read':
  108.             case '!checkmail':
  109.             case '!mailbox':
  110.                 var nick = toId(ev.clientNick);
  111.                 if (mail[nick].length < 1) {
  112.                     sendMessage(ev.clientId, 'You have no messages.');
  113.                 } else {
  114.                     var mailTemp = 'You have mail!\n';
  115.                     while (msg = mail[nick].pop()) {
  116.                         mailTemp = mailTemp + msg + '\n';
  117.                     }
  118.                     sendMessage(ev.clientId, mailTemp);
  119.                 }
  120.                 store.set('mail', mail);
  121.                 break;
  122.             case '!deleteall':
  123.                 if (uids.indexOf(ev.clientUid) < 0) return;
  124.                 mail = {};
  125.                 store.unset('mail');
  126.                 sendMessage("[color=gray][i]The mail system has been reset![/i][/color]");
  127.                 break;
  128.             case '!viewmail':
  129.                 if (uids.indexOf(ev.clientUid) < 0) return;
  130.                 engine.log("Loaded mail: " + JSON.stringify(mail));
  131.                 break;
  132.         }
  133.     });
  134. });
Add Comment
Please, Sign In to add comment