Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const discord = require("discord.js"),
- mysql = require("mysql"),
- colors = require("colors");
- const config = {
- sql: {
- host: "localhost",
- user: "root",
- password: "",
- database: "bot"
- },
- token: "MzY4MDk0ODUxMzM1NDU0NzMx.DME-ww.kLVHR5Qz4dlSRybAsBNQV0A994A",
- prefix: "!"
- };
- var bot = new discord.Client(),
- con;
- function log(text, color) {
- let d = new Date(),
- h = d.getHours(),
- m = d.getMinutes(),
- ap = "AM";
- if (h > 12) { h -= 12; ap = "PM"; }
- if (m < 10) { m = "0" + m; }
- time = h + ":" + m + " " + ap;
- if (typeof(color) == "undefined") { console.log(colors.grey(time) + ": " + text); }
- if (typeof(color) != "undefined") { console.log(colors.grey(time) + ": " + colors[color](text)); }
- }
- function handleConnection() {
- con = mysql.createConnection(config.sql);
- con.connect(function(err) {
- if (err) {
- log("[ERROR] An error has occurred while connection: " + err, "red");
- log("[INFO] Attempting to establish connection with SQL database.", "yellow");
- setTimeout(handleConnection, 2000);
- } else {
- log("[SUCCESS] SQL database connection established successfully.", "green");
- }
- });
- con.on("error", function(err) {
- console.log("Error: " + err);
- if (err.code === "PROTOCOL_CONNECTION_LOST") {
- handleConnection();
- } else {
- throw err;
- }
- });
- }
- function Item(name, amount, data) {
- this.name = name;
- this.amount = amount;
- this.data = data;
- this.setName = function(name) {
- this.name = name;
- };
- this.addAmount = function(amount) {
- if (this.data.maxStack !== undefined) {
- if (this.amount + amount <= this.data.maxStack) {
- this.amount += amount;
- return true;
- } else {
- return false;
- }
- } else {
- this.amount += amount;
- return true;
- }
- return false;
- };
- this.removeAmount = function(amount) {
- if (this.amount - amount < 0) {
- return false;
- } else {
- this.amount -= amount;
- return true;
- }
- return false;
- };
- }
- function Inventory(maxWeight) {
- this.contents = [];
- this.maxWeight = maxWeight;
- this.weight = 0;
- this.addItem = function(message, item) {
- if (this.weight + (item.amount * item.data.weight) <= this.maxWeight) {
- this.weight += item.amount * item.data.weight;
- for (let i = 0; i < this.contents.length; i++) {
- if (JSON.stringify(this.contents[i].data) === JSON.stringify(item.data)) {
- this.contents[i].amount += item.amount;
- message.author.send("Added x" + item.amount + " of " + item.name + " to your inventory.");
- return true;
- }
- }
- this.contents.push(item);
- message.author.send("Added x" + item.amount + " of " + item.name + " to your inventory.");
- return true;
- } else {
- message.channel.send("You are overburdened, try reducing your bag's weight.");
- return false;
- }
- return false;
- };
- this.removeItem = function(message, item) {
- for (let i = 0; i < this.contents.length; i++) {
- if (JSON.stringify(this.contents[i].data) === JSON.stringify(item.data)) {
- if (this.contents[i].amount - item.amount >= 0) {
- this.contents[i].amount -= item.amount;
- message.author.send("Removed x" + item.amount + " of " + item.name + " from your inventory.");
- if (this.contents[i].amount === 0) this.contents.pop(item);
- return true;
- } else {
- message.author.send("You don't have enough of " + item.name + " to drop x" + item.amount + ".");
- return false;
- }
- return true;
- }
- }
- return false;
- };
- }
- var users = {};
- bot.on("ready", () => {
- let members = bot.guilds.array()[0].members.array();
- for (let i = 0; i < members.length; i++) {
- users[members[i].user.username] = {
- inventory: new Inventory(10)
- };
- log("[INFO] " + members[i].user.username + " added to users list.", "cyan");
- }
- log("[SUCCESS] Discord bot is now ready.", "green");
- });
- bot.on("message", (message) => {
- if (message.author.equals(bot.user)) return;
- if (!message.content.startsWith(config.prefix)) return;
- let args = message.content.substring(config.prefix.length).split(" ");
- switch (args[0]) {
- case "ping":
- message.channel.send((Date.now() - message.createdTimestamp) + "ms");
- break;
- case "additem":
- if (args.length >= 2) users[message.author].inventory.addItem(message, new Item("Elemental Rune", args[1], {weight: 0.1, type: "Rune", description: "A wonderful little piece of stone containing great power."}));
- break;
- case "removeitem":
- if (args.length >= 2) users[message.author].inventory.removeItem(message, new Item("Elemental Rune", args[1], {weight: 0.1, type: "Rune", description: "A wonderful little piece of stone containing great power."}))
- break;
- case "inventory":
- let inv = users[message.author].inventory,
- msg = "```Inventory [Weight:" + inv.weight + "] [Max Weight: " + inv.maxWeight + "]\n";
- for (let i = 0; i < inv.contents.length; i++) {
- if (i !== inv.contents.length - 1) {
- msg += "[" + i + "] " + inv.contents[i].name + " x" + inv.contents[i].amount + "\n";
- } else {
- msg += "[" + i + "] " + inv.contents[i].name + " x" + inv.contents[i].amount + "```";
- }
- }
- if (msg.substring(msg.length - 1) == "`") msg += "Empty```";
- message.channel.send(msg);
- break;
- }
- });
- handleConnection();
- bot.login(config.token);
Add Comment
Please, Sign In to add comment