SHOW:
|
|
- or go back to the newest paste.
1 | const discord = require("discord.js"), | |
2 | mysql = require("mysql"), | |
3 | colors = require("colors"); | |
4 | ||
5 | const config = { | |
6 | sql: { | |
7 | host: "localhost", | |
8 | user: "root", | |
9 | password: "", | |
10 | database: "bot" | |
11 | }, | |
12 | token: "MzY4MDk0ODUxMzM1NDU0NzMx.DME-ww.kLVHR5Qz4dlSRybAsBNQV0A994A", | |
13 | prefix: "!" | |
14 | }; | |
15 | ||
16 | var bot = new discord.Client(), | |
17 | con; | |
18 | ||
19 | function log(text, color) { | |
20 | let d = new Date(), | |
21 | h = d.getHours(), | |
22 | m = d.getMinutes(), | |
23 | ap = "AM"; | |
24 | ||
25 | if (h > 12) { h -= 12; ap = "PM"; } | |
26 | if (m < 10) { m = "0" + m; } | |
27 | time = h + ":" + m + " " + ap; | |
28 | ||
29 | if (typeof(color) == "undefined") { console.log(colors.grey(time) + ": " + text); } | |
30 | if (typeof(color) != "undefined") { console.log(colors.grey(time) + ": " + colors[color](text)); } | |
31 | } | |
32 | ||
33 | function handleConnection() { | |
34 | con = mysql.createConnection(config.sql); | |
35 | ||
36 | con.connect(function(err) { | |
37 | if (err) { | |
38 | log("[ERROR] An error has occurred while connection: " + err, "red"); | |
39 | log("[INFO] Attempting to establish connection with SQL database.", "yellow"); | |
40 | setTimeout(handleConnection, 2000); | |
41 | } else { | |
42 | log("[SUCCESS] SQL database connection established successfully.", "green"); | |
43 | } | |
44 | }); | |
45 | ||
46 | con.on("error", function(err) { | |
47 | console.log("Error: " + err); | |
48 | if (err.code === "PROTOCOL_CONNECTION_LOST") { | |
49 | handleConnection(); | |
50 | } else { | |
51 | throw err; | |
52 | } | |
53 | }); | |
54 | } | |
55 | ||
56 | function Item(name, amount, data) { | |
57 | this.name = name; | |
58 | this.amount = amount; | |
59 | this.data = data; | |
60 | ||
61 | this.setName = function(name) { | |
62 | this.name = name; | |
63 | }; | |
64 | ||
65 | this.addAmount = function(amount) { | |
66 | if (this.data.maxStack !== undefined) { | |
67 | if (this.amount + amount <= this.data.maxStack) { | |
68 | this.amount += amount; | |
69 | return true; | |
70 | } else { | |
71 | return false; | |
72 | } | |
73 | } else { | |
74 | this.amount += amount; | |
75 | return true; | |
76 | } | |
77 | ||
78 | return false; | |
79 | }; | |
80 | ||
81 | this.removeAmount = function(amount) { | |
82 | if (this.amount - amount < 0) { | |
83 | return false; | |
84 | } else { | |
85 | this.amount -= amount; | |
86 | return true; | |
87 | } | |
88 | ||
89 | return false; | |
90 | }; | |
91 | } | |
92 | ||
93 | function Inventory(maxWeight) { | |
94 | this.contents = []; | |
95 | this.maxWeight = maxWeight; | |
96 | this.weight = 0; | |
97 | ||
98 | this.addItem = function(message, item) { | |
99 | if (this.weight + (item.amount * item.data.weight) <= this.maxWeight) { | |
100 | this.weight += item.amount * item.data.weight; | |
101 | ||
102 | for (let i = 0; i < this.contents.length; i++) { | |
103 | if (JSON.stringify(this.contents[i].data) === JSON.stringify(item.data)) { | |
104 | this.contents[i].amount += item.amount; | |
105 | message.author.send("Added x" + item.amount + " of " + item.name + " to your inventory."); | |
106 | return true; | |
107 | } | |
108 | } | |
109 | ||
110 | this.contents.push(item); | |
111 | message.author.send("Added x" + item.amount + " of " + item.name + " to your inventory."); | |
112 | return true; | |
113 | } else { | |
114 | message.channel.send("You are overburdened, try reducing your bag's weight."); | |
115 | return false; | |
116 | } | |
117 | ||
118 | return false; | |
119 | }; | |
120 | ||
121 | this.removeItem = function(message, item) { | |
122 | for (let i = 0; i < this.contents.length; i++) { | |
123 | if (JSON.stringify(this.contents[i].data) === JSON.stringify(item.data)) { | |
124 | if (this.contents[i].amount - item.amount >= 0) { | |
125 | this.contents[i].amount -= item.amount; | |
126 | message.author.send("Removed x" + item.amount + " of " + item.name + " from your inventory."); | |
127 | ||
128 | if (this.contents[i].amount === 0) this.contents.pop(item); | |
129 | return true; | |
130 | } else { | |
131 | message.author.send("You don't have enough of " + item.name + " to drop x" + item.amount + "."); | |
132 | return false; | |
133 | } | |
134 | ||
135 | return true; | |
136 | } | |
137 | } | |
138 | ||
139 | return false; | |
140 | }; | |
141 | } | |
142 | ||
143 | var users = {}; | |
144 | ||
145 | bot.on("ready", () => { | |
146 | ||
147 | let members = bot.guilds.array()[0].members.array(); | |
148 | for (let i = 0; i < members.length; i++) { | |
149 | users[members[i].user.username] = { | |
150 | inventory: new Inventory(10) | |
151 | }; | |
152 | log("[INFO] " + members[i].user.username + " added to users list.", "cyan"); | |
153 | } | |
154 | ||
155 | log("[SUCCESS] Discord bot is now ready.", "green"); | |
156 | }); | |
157 | ||
158 | bot.on("message", (message) => { | |
159 | if (message.author.equals(bot.user)) return; | |
160 | if (!message.content.startsWith(config.prefix)) return; | |
161 | ||
162 | let args = message.content.substring(config.prefix.length).split(" "); | |
163 | ||
164 | switch (args[0]) { | |
165 | case "ping": | |
166 | message.channel.send((Date.now() - message.createdTimestamp) + "ms"); | |
167 | break; | |
168 | case "additem": | |
169 | 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."})); | |
170 | break; | |
171 | case "removeitem": | |
172 | 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."})) | |
173 | break; | |
174 | case "inventory": | |
175 | let inv = users[message.author].inventory, | |
176 | msg = "```Inventory [Weight:" + inv.weight + "] [Max Weight: " + inv.maxWeight + "]\n"; | |
177 | ||
178 | for (let i = 0; i < inv.contents.length; i++) { | |
179 | if (i !== inv.contents.length - 1) { | |
180 | msg += "[" + i + "] " + inv.contents[i].name + " x" + inv.contents[i].amount + "\n"; | |
181 | } else { | |
182 | msg += "[" + i + "] " + inv.contents[i].name + " x" + inv.contents[i].amount + "```"; | |
183 | } | |
184 | } | |
185 | ||
186 | if (msg.substring(msg.length - 1) == "`") msg += "Empty```"; | |
187 | ||
188 | message.channel.send(msg); | |
189 | break; | |
190 | ||
191 | } | |
192 | }); | |
193 | ||
194 | handleConnection(); | |
195 | bot.login(config.token); |