0

Simple levelling script for Sporks

Free lines JavaScript 2 revisions 46 21 days ago 21 days ago
discord-sporks
// You can edit these levels or add more
var levels = [
  {"name":"Beginner", "msg":0, "rank": "1"},
  {"name":"Newbie", "msg":5, "rank": "2"},
  {"name":"Rookie", "msg":100, "rank": "3"},
  {"name":"Greenhorn", "msg":1000, "rank": "4"},
  {"name":"Apprentice", "msg":10000, "rank": "5"},
  {"name":"Journeyman", "msg":100000, "rank": "6"},
  {"name":"Master", "msg":1000000, "rank": "7"},
  {"name":"Awesomesauce!", "msg":10000000, "rank": "8"},
];

// Actual script code starts here
getLevel = function(current) {
  for (i = levels.length - 1; i >= 0; --i) {
    if (current >= levels[i].msg) {
      return levels[i];
    }
  }
  return;
};

passingLevel = function(current) {
  for (i = levels.length - 1; i >= 0; --i) {
    if (levels[i].msg == current) {
      return levels[i];
    }
  }
  return;
};

var u = find_user(author.id);
var user_id = u.nickname;
if (user_id == "") {
  user_id = author.username;
}
var current = load("counter_" + user_id);
if (current == undefined) {
  current = "1";
}
current = parseInt(current) + 1;
save("counter_" + user_id, current.toString());
var passing = passingLevel(parseInt(current));
if (passing != undefined) {
  create_embed(CHANNEL_ID, {
    "color": 0xFFFF00, 
    "description": "<@" + u.id + "> has levelled up to rank#" + passing.rank + " **" + passing.name + "**!",
    "footer": {
        "link": "https:\/\/sporks.gg\/",
        "text": "Powered by Sporks simple level script",
        "icon_url": "https:\/\/www.sporks.gg\/images\/sporks_2019.png"
    },
    "thumbnail": {
        "url": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT_1d4LpcuOFhWbNcIC6qTOiTN3W3fQQ3du8Uw4nCKcoqXW0btF&s"
    }    
  });  
}

var m = message.content.split(' ');
if (m[0] == "!rank") {
  if (m.length == 1) {
    search_user = user_id;
  } else {
    search_user = m[1];
  }
  var current = load("counter_" + search_user);
  if (current == undefined) {
    create_message(CHANNEL_ID, "The username " + search_user + " is unknown to me :cry:");
  } else {
    var level = getLevel(parseInt(current));
    if (level != undefined) {
      create_embed(CHANNEL_ID, {
        "color": 0xFFFF00, 
        "description": search_user + " is currently rank #" + level.rank + ": **" + level.name + "** with " + current + " total messages.",
        "footer": {
            "link": "https:\/\/sporks.gg\/",
            "text": "Powered by Sporks simple level script",
            "icon_url": "https:\/\/www.sporks.gg\/images\/sporks_2019.png"
        },
        "thumbnail": {
            "url": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT_1d4LpcuOFhWbNcIC6qTOiTN3W3fQQ3du8Uw4nCKcoqXW0btF&s"
        }    
      });  
    }
  }
}

This script provides a basic levelling system for Sporks. It will store a level for each person on your server. You can use this script as-is, ready to go, or you can modify and enhance it. There is lots of space left to add many more functions and possibilities. Type !rank to see your own rank, and !rank <other user> to see another user's rank. The bot will send a nice message to the channel when someone levels up. There are 8 levels as standard which can be found at the top of the script, feel free to edit them or even add more.