· 6 years ago · Jun 08, 2019, 05:20 AM
1const Discord = require('discord.js')
2const client = new Discord.Client()
3const firebase = require("firebase");
4const config = require("./config.json");
5const AuthorID = '414622975598395398';
6const invites = {};
7const Canvas = require("canvas") // Contains the prefix, and token!
8const SQLite = require("better-sqlite3");
9const sql = new SQLite('./scores.sqlite');
10
11client.on("ready", () => {
12 // Check if the table "points" exists.
13 const table = sql.prepare("SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'scores';").get();
14 if (!table['count(*)']) {
15 // If the table isn't there, create it and setup the database correctly.
16 sql.prepare("CREATE TABLE scores (id TEXT PRIMARY KEY, user TEXT, guild TEXT, points INTEGER, level INTEGER);").run();
17 // Ensure that the "id" row is always unique and indexed.
18 sql.prepare("CREATE UNIQUE INDEX idx_scores_id ON scores (id);").run();
19 sql.pragma("synchronous = 1");
20 sql.pragma("journal_mode = wal");
21 }
22
23 // And then we have two prepared statements to get and set the score data.
24 client.getScore = sql.prepare("SELECT * FROM scores WHERE user = ? AND guild = ?");
25 client.setScore = sql.prepare("INSERT OR REPLACE INTO scores (id, user, guild, points, level) VALUES (@id, @user, @guild, @points, @level);");
26});
27
28
29client.on('message', async message => {
30 if (message.author.bot) return;
31 let score;
32 if (message.guild) {
33 score = client.getScore.get(message.author.id, message.guild.id);
34 if (!score) {
35 score = { id: `${message.guild.id}-${message.author.id}`, user: message.author.id, guild: message.guild.id, points: 0, level: 1 }
36 }
37 score.points++;
38 const curLevel = Math.floor(0.1 * Math.sqrt(score.points));
39 if(score.level < curLevel) {
40 score.level++;
41 message.reply(`You've leveled up to level **${curLevel}**! Ain't that dandy?`);
42 }
43 client.setScore.run(score);
44 }
45 if (message.content.indexOf(config.prefix) !== 0) return;
46
47 const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
48 const command = args.shift().toLowerCase();
49
50 if(command === "Idcard") {
51 const canvas = Canvas.createCanvas(1280, 364);
52 const ctx = canvas.getContext('2d');
53
54 const background = await Canvas.loadImage('https://cdn.glitch.com/3b1e98ec-ec80-4540-9202-74751f7a9f54%2Fwallpaper.jpg?v=1559732321126');
55 ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
56
57
58 ctx.strokeStyle = '#74037b';
59
60 ctx.strokeRect(0, 0, canvas.width, canvas.height);
61
62 const avatar = await Canvas.loadImage(message.author.displayAvatarURL);
63 // Move the image downwards vertically and constrain its height to 200, so it's a square
64 ctx.drawImage(avatar, 135, 53, 255, 260);
65
66 ctx.font = '50px Impact'
67 ctx.fillStyle = '#696969';
68 ctx.fillText(`${message.author.username}`, canvas.width / 1.8, canvas.height / 4.8);
69
70 ctx.font = applyText(canvas, `LEVEL`);
71 ctx.fillStyle = '#696969';
72 ctx.fillText(`LEVEL`, canvas.width / 2.8, canvas.height / 2.1);
73
74
75 const attachment = new Discord.Attachment(canvas.toBuffer(), 'welcome-image.png');
76
77 message.channel.send('your id card is here!', attachment);
78 }
79
80 if(command === "points") {
81 return message.reply(`You currently have ${score.points} points and are level ${score.level}!`);
82}
83
84 if(command === "give") {
85 // Limited to guild owner - adjust to your own preference!
86 if(!message.author.id === message.guild.owner) return message.reply("You're not the boss of me, you can't do that!");
87
88 const user = message.mentions.users.first() || client.users.get(args[0]);
89 if(!user) return message.reply("You must mention someone or give their ID!");
90
91 const pointsToAdd = parseInt(args[1], 10);
92 if(!pointsToAdd) return message.reply("You didn't tell me how many points to give...")
93
94 // Get their current points.
95 let userscore = client.getScore.get(user.id, message.guild.id);
96 // It's possible to give points to a user we haven't seen, so we need to initiate defaults here too!
97 if (!userscore) {
98 userscore = { id: `${message.guild.id}-${user.id}`, user: user.id, guild: message.guild.id, points: 0, level: 1 }
99 }
100 userscore.points += pointsToAdd;
101
102 // We also want to update their level (but we won't notify them if it changes)
103 let userLevel = Math.floor(0.1 * Math.sqrt(score.points));
104 userscore.level = userLevel;
105
106 // And we save it!
107 client.setScore.run(userscore);
108
109 return message.channel.send(`${user.tag} has received ${pointsToAdd} points and now stands at ${userscore.points} points.`);
110}
111
112if(command === "leaderboard") {
113 const top10 = sql.prepare("SELECT * FROM scores WHERE guild = ? ORDER BY points DESC LIMIT 10;").all(message.guild.id);
114
115 // Now shake it and show it! (as a nice embed, too!)
116 const embed = new Discord.RichEmbed()
117 .setTitle("Leaderboard")
118 .setAuthor(client.user.username, client.user.avatarURL)
119 .setDescription("Our top 10 points leaders!")
120 .setColor(0x00AE86);
121
122 for(const data of top10) {
123 embed.addField(client.users.get(data.user).tag, `${data.points} points (level ${data.level})`);
124 }
125 return message.channel.send({embed});
126}
127
128 // Command-specific code here!
129});
130
131
132// A pretty useful method to create a delay without blocking the whole script.
133const wait = require('util').promisify(setTimeout);
134
135client.on('ready', () => {
136 // "ready" isn't really ready. We need to wait a spell.
137 wait(1000);
138
139 // Load all invites for all guilds and save them to the cache.
140 client.guilds.forEach(g => {
141 g.fetchInvites().then(guildInvites => {
142 invites[g.id] = guildInvites;
143 });
144 });
145});
146
147firebase.initializeApp(config.firebaseConfig);
148
149client.on("ready", () => {
150 console.log(`Bot has started, with ${client.users.size} users, in ${client.channels.size} channels of ${client.guilds.size} servers.`);
151 client.user.setActivity('YouTube', { type: 'WATCHING' });
152
153})
154
155const applyText = (canvas, text) => {
156 const ctx = canvas.getContext('2d');
157
158 //LJ Declare a base size of the font
159 let fontSize = 70;
160
161 do {
162 // Assign the font to the context and decrement it so it can be measured again
163 ctx.font = `${fontSize -= 10}px sans-serif`;
164 // Compare pixel width of the text to the canvas minus the approximate avatar size
165 } while (ctx.measureText(text).width > canvas.width - 300);
166
167 // Return the result to use in the actual canvas
168 return ctx.font;
169};
170
171client.on('guildMemberAdd', async member => {
172 const channel = member.guild.channels.find(ch => ch.name === 'member-log');
173 if (!channel) return;
174
175 const canvas = Canvas.createCanvas(700, 250);
176 const ctx = canvas.getContext('2d');
177
178 const background = await Canvas.loadImage('https://cdn.glitch.com/3b1e98ec-ec80-4540-9202-74751f7a9f54%2Flololololol.png?v=1559824192569');
179 ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
180
181 ctx.strokeStyle = '#74037b';
182 ctx.strokeRect(0, 0, canvas.width, canvas.height);
183
184 // Slightly smaller text placed above the member's display name
185 ctx.font = '28px sans-serif';
186 ctx.fillStyle = '#ffffff';
187 ctx.fillText('Welcome to the server,', canvas.width / 2.5, canvas.height / 3.5);
188
189 // Add an exclamation point here and below
190 ctx.font = applyText(canvas, `${member.displayName}!`);
191 ctx.fillStyle = '#ffffff';
192 ctx.fillText(`${member.displayName}!`, canvas.width / 2.5, canvas.height / 1.8);
193
194 ctx.beginPath();
195 ctx.arc(125, 125, 100, 0, Math.PI * 2, true);
196 ctx.closePath();
197 ctx.clip();
198
199 const avatar = await Canvas.loadImage(member.user.displayAvatarURL);
200 ctx.drawImage(avatar, 25, 25, 200, 200);
201
202 const attachment = new Discord.Attachment(canvas.toBuffer(), 'welcome-image.png');
203
204 channel.send(`Welcome to the server, ${member}!`, attachment);
205});
206
207client.on('guildMemberAdd', member => { //when someone join
208});
209
210client.on("message", async message => {
211 if(message.author.bot) return; //bot don't repeate itself!!
212 return firebase.database().ref("data").once('value').then(function(snapshot) {
213 var serverdata = snapshot.val() || 'error';
214 if(serverdata == 'error'){
215 message.channel.send("something got fuckup with database");
216 return;
217 }
218
219
220
221 if(message.content.indexOf(serverdata.prefix)!==0) return;
222 const args = message.content.slice(serverdata.prefix.length).trim().split(/ +/g);
223 const command = args.shift().toLowerCase();
224
225 if(command=="account"){
226 return firebase.database().ref("admin").once('value').then(function(snapshot) {
227 var admin = snapshot.val() || 'error';
228 var id = message.mentions.users.first().id;
229 for(let i = 0; i < admin.length; i++){
230 if(admin[i]==message.author.id){
231 if(args[0]!="set"){
232 return firebase.database().ref("userdata").once('value').then(function(snapshot) {
233 var userdata = snapshot.val() || 'error';
234 if(userdata=='error'){
235 message.channel.send("pls set userdata first!");
236 }
237 message.channel.send("**Number of "+client.users.get(id)+" is** `"+ userdata[id].number+"`");
238 });
239 }
240 if(args[0]=="set"){
241 firebase.database().ref("userdata/"+id).update({number:args[2]});
242 message.channel.send("**Number have been set!!!**");
243 }
244 return;
245 }
246 }
247 message.channel.send("you're idiot be admin first");
248 return;
249 });
250 }
251
252 if(command=="test"){
253 message.channel.send('dont fear GOD is here..'+'**MIND IT**')
254 }
255
256
257 if(command == "setid"){
258 firebase.database().ref("/").update({membercountid : args[0]});
259 message.channel.send("update ->"+args[0]);
260 }
261
262
263 if(command == "update"){
264 return firebase.database().ref("membercountid").once('value').then(function(snapshot) {
265 var serverdata = snapshot.val() || 'error';
266 var guildmember = (message.guild.members.filter(member => !member.user.bot).size);
267 message.channel.send("update with"+serverdata);
268 var interval = setInterval (function () {
269 client.channels.get(serverdata).setName(`Member Count : ${guildmember}`, `voice`)
270 }, 1 * 5000);
271 message.channel.send("membercount have been update!! //"+serverdata);
272 return;
273
274 });
275 }
276
277 if(command=="eval"){
278 if (message.author.id !== AuthorID) return message.channel.send('Cette commandes est seulement pour le Créateur du bot !')
279
280 function clean(text) {
281 if (typeof (text) === "string")
282 return text.replace(/`/g, "`" + String.fromCharCode(8203)).replace(/@/g, "@" + String.fromCharCode(8203));
283 else
284 return text;
285 }
286
287 try {
288 message.delete(5)
289 const code = args.join(" ");
290 let evaled = eval(code);
291
292 if (typeof evaled !== "string")
293 evaled = require("util").inspect(evaled);
294
295 message.channel.send(clean(evaled), {
296 code: "xl"
297 }).then(msg => {
298 msg.react('❌')
299 });
300 } catch (err) {
301 message.channel.send(`\`ERROR\` \`\`\`xl\n${clean(err)}\n\`\`\``);
302 }
303
304 }
305
306
307 if(command=="invitelist"){
308 if(args[0]=="update"){
309 message.guild.fetchInvites()
310 .then(invites => {
311 let inviteArr = invites.array();
312 firebase.database().ref("data/"+"inviterank/invitelist/").set({});
313 for (let i = 0; i < inviteArr.length; i++) {
314 firebase.database().ref("data/"+"inviterank/invitelist/"+i).set({code : inviteArr[i].code,name : inviteArr[i].inviter.id, uses : inviteArr[i].uses});
315 }
316 message.channel.send("**Server's invite data have been updated!!!**");
317 })
318 .catch(console.error);
319 }
320 if(args[0]=="rank"){
321 var onlyusesarray = [];
322 var invitelistarray = serverdata.inviterank.invitelist;
323 for(let i = 0; i < invitelistarray.length; i++){
324 var listdata = { uses: invitelistarray[i].uses , number: i}
325 onlyusesarray.push(listdata);
326 }
327 onlyusesarray.sort(function(a, b){
328 return b.uses-a.uses
329 });
330 for(let i = 0; i < 10; i++){
331 let shownum = i + 1;
332 let numnumnum = onlyusesarray[i].number;
333 let whodafuck = client.users.get(invitelistarray[numnumnum].name);
334 message.channel.send(shownum+". "+invitelistarray[numnumnum].code + " By "+ whodafuck + " with "+invitelistarray[numnumnum].uses+" uses");
335 }
336 }
337
338 }
339
340 });
341});
342
343client.on('message', async message => {
344
345 if (message.content === 'ID'){
346 const canvas = Canvas.createCanvas(1280, 364);
347 const ctx = canvas.getContext('2d');
348
349 const background = await Canvas.loadImage('https://cdn.glitch.com/3b1e98ec-ec80-4540-9202-74751f7a9f54%2F20190607_150853.jpg?v=1559900405317');
350 ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
351
352
353 ctx.strokeStyle = '#74037b';
354
355 ctx.strokeRect(0, 0, canvas.width, canvas.height);
356
357 const avatar = await Canvas.loadImage(message.author.displayAvatarURL);
358 // Move the image downwards vertically and constrain its height to 200, so it's a square
359 ctx.drawImage(avatar, 135, 53, 255, 260);
360
361 ctx.font = '50px Impact'
362 ctx.fillStyle = '#696969';
363 ctx.fillText(`${message.author.username}`, canvas.width / 1.8, canvas.height / 4.8);
364
365 ctx.font = applyText(canvas, `LEVEL`);
366 ctx.fillStyle = '#696969';
367 ctx.fillText(`LEVEL`, canvas.width / 2.8, canvas.height / 2.1);
368
369
370 const attachment = new Discord.Attachment(canvas.toBuffer(), 'welcome-image.png');
371
372 message.channel.send('your id card is here!', attachment);
373 }
374});
375
376
377client.login(config.token);