@L11K/

starbot

Nodejs

I help people thank each other ⭐

fork
loading
Files
  • index.js
  • db.json
  • index.html
  • package-lock.json
  • web.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const Discord = require('discord.js');
const client = new Discord.Client();
const token = process.env.SECRET;
const keep_alive = require('./web.js')

const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')


const adapter = new FileSync('db.json')
const db = low(adapter)

db.defaults({
	 users:{
	 }	 
	 })
  .write()


client.on('ready', () => {
  console.log("I'm in");
  console.log(client.user.username);
});

client.on('message', msg => {
    if (~msg.content.indexOf("⭐") && msg.author.id != client.user.id) {
			star(msg);
    }
});

client.login(token);

client.on('ready', () => {
    client.user.setStatus('available')
		client.user.setActivity("Help 👉 https://bot.4ty2.fun", {
			type: "STREAMING",
			url: "https://starbot.4ty2.fun"
		});
});

function search(nameKey, myArray){
    for (var i=0; i < myArray.length; i++) {
        if (myArray[i].name === nameKey) {
            return myArray[i];
        }
    }
}

function star(message){
	console.log("Mentions.users.array():" + message.mentions.users.array());

	if(db.has(message.guild.id).value() == false){
				db.set(message.guild.id, {}).write();			
	}
	// Check author stars
	if(!db.has(message.guild.id +".<@"+message.author.id+">").value()){
			db.set(message.guild.id +".<@"+message.author.id+">", 20).write();
			
	}
if(db.get(message.guild.id +".<@"+message.author.id+">").value()<1 || message.author.bot){
	message.react("❌");
	return;
}

	// Add Stars
	for(var i = 0; i<message.mentions.users.array().length; i++){

		if(db.has(message.guild.id +"."+message.mentions.users.array()[i]).value()){
			console.log("1up");
			if(`<@${message.author.id}>`!=message.mentions.users.array()[i]){
				db.update(message.guild.id +"." + message.mentions.users.array()[i], n => n + (message.content.match(/⭐/g) || []).length).write();
				db.update(message.guild.id +".<@"+message.author.id+">", n => n - (message.content.match(/⭐/g) || []).length).write();
				message.react("🌟")
			}
		}
		else{
			if(`<@${message.author.id}>`!=message.mentions.users.array()[i]){
				db.set(message.guild.id +"." + message.mentions.users.array()[i], 20 + (message.content.match(/⭐/g) || []).length).write();
				db.update(message.guild.id +".<@"+message.author.id+">", n => n - (message.content.match(/⭐/g) || []).length).write();
				message.react("🌟")
			}
			else{
				message.react("❌")
			}	
		}
		return;
	}

	// Show Stats
	if(message.content.replace(/\s/g,'') === "⭐"){
		leaderboard(message);
	}
	// Show API
	else if(message.content.replace(/\s/g,'').toLowerCase() === "⭐api"){
		api(message)
	}
	// Show Help
	else if(message.content.replace(/\s/g,'').toLowerCase() === "⭐help"){
		help(message)
	}
	else {
		message.react("❓");
	}

}

function leaderboard(message){
		var users = db.get(message.guild.id).value();
		console.log(users);
		var leaderboard = "**Star Count** 🏆\n\n";

		var sortedUsers = Object.keys(users).sort(function(a,b){return users[b]-users[a]});
		console.log(sortedUsers);;
		for(var i = 0; i<5; i++){
			leaderboard += `${i+1}. ${sortedUsers[i]} has **${users[sortedUsers[i]]}** ⭐` + "\n";
		}
		message.channel.send(`<@${message.author.id}>, you have **${users["<@"+message.author.id+">"]}** ⭐`);
		message.channel.send({
			"embed": {
  			"color": 16763739,
  			"description": leaderboard
			}
			});
}

function api(message){
	message.channel.send("You can find the star count in JSON at this endpoint 👉 `https://starbot.4ty2.fun/count/" + message.guild.id + "`\n\nIf a user ID is not present, assume it has 20 stars.");
}

function help(message){
	message.channel.send("Starbot lets you appreciate and acknowledge members in a guild.\n\n You can tag users in a message like this to appreciate them ✨\n`Thanks @userX and @userY for adding Starbot ⭐`\n\nYou can access the star count of users by just sending a `⭐`, and view help with `⭐ Help`. Additionally, learn about an API through sending `⭐ API`\n\nTo learn more 🌐 https://starbot.4ty2.fun")
}
node v10.16.0