-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
146 lines (117 loc) · 3.52 KB
/
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const Discord = require("discord.js");
const Keyv = require('keyv')
const db = require('./db.js')
const token = 'YOUR TOKEN HERE'
const bot = new Discord.Client()
const keyv = new Keyv('sqlite://test-db.sqlite')
keyv.on('error', err => console.log(err))
const statEmbed = async (username, rank) => {
let embed = new Discord.MessageEmbed()
embed.setTitle(`Ranking for ${username}`)
embed.setDescription(`Score is ${rank}`)
return embed
}
const prefix = '!'
bot.on('message', async (msg) => {
if(msg.content[0] !== prefix) {
console.log('no prefix')
return
}
const args = msg.content.slice(prefix.length).trim().split(' ')
const command = args.shift().toLowerCase()
if(command === 'stats') {
const user = msg.author
let rank = await keyv.get(user.username)
if(rank === undefined) {
await keyv.set(user.username, 1)
rank = await keyv.get(user.username)
}
console.log('rank: ', rank)
let embed = await statEmbed(user.username, rank)
msg.channel.send(embed)
}
if(command === 'promote') {
if(!msg.member.roles.cache.has('602665687051403274')) {
console.log('user is not moderator')
return
}
const user = msg.mentions.users.first()
if(!user) {
console.log('no user mentioned')
return
}
let rankVal = await keyv.get(user.username)
if(rankVal === undefined) {
await keyv.set(user.username, 1)
rankVal = await keyv.get(user.username)
}
let num = 1
if(args.length > 1) {
num = parseInt(args[0])
}
const newRank = rankVal + num
await keyv.set(user.username, newRank)
const checker = await keyv.get(user.username)
if(checker >= 10) {
let modeRole = msg.guild.roles.cache.find(r => r.name === 'moderator')
let member = msg.mentions.members.first()
member.roles.add(modeRole)
}
}
if(command === 'demote') {
if(!msg.member.roles.cache.has('602665687051403274')) {
console.log('user is not moderator')
return
}
const user = msg.mentions.users.first()
if(!user) {
console.log('no user mentioned')
return
}
let rankVal = await keyv.get(user.username)
if(rankVal === undefined) {
await keyv.set(user.username, 1)
rankVal = await keyv.get(user.username)
}
let num = 1
if(args.length > 1) {
num = parseInt(args[0])
}
const newRank = rankVal - num
await keyv.set(user.username, newRank)
const checker = await keyv.get(user.username)
if(checker < 10) {
let modRole = msg.guild.roles.cache.find(r => r.name === "moderator")
let member = msg.mentions.members.first()
member.roles.remove(modRole)
}
}
if(command === 'highscore') {
let rows = await db.select()
//console.log(rows)
let sortedData = []
sortedData = rows.sort((a,b) => {
let newa = JSON.parse(a.value)
let newb = JSON.parse(b.value)
a.value = newa
b.value = newb
let val = b['value']['value'] - a['value']['value']
return val
})
console.log(sortedData)
if(sortedData.length > 5) {
sortedData = sortedData.slice(0,5)
}
let embed = new Discord.MessageEmbed()
embed.setTitle('Highest ranked users')
embed.setDescription(`Top ${sortedData.length} users`)
sortedData.map(user => {
let username = user['key'].slice(5)
let score = user['value']['value']
embed.addField(`${username} : `, score)
})
embed.setFooter(`this embed made by ${bot.user.username}`)
msg.channel.send(embed)
}
})
bot.login(token)