forked from shikhir-arora/karma-simple
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkarma.js
311 lines (282 loc) · 13.6 KB
/
karma.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
if (process.version.slice(1).split('.')[0] < 11) throw new Error('Node must be v11+ - please upgrade to the latest version of Node!')
const Discord = require('discord.js')
const axios = require('axios')
const gist = require('snekgist')
const exec = require('child_process').exec
const os = require('os')
const moment = require('moment')
require('moment-duration-format')
const c = require('ansi-colors')
const config = require('./config.json')
const Ratelimiter = require('./Ratelimiter.js')
const rl = new Ratelimiter()
const randomColor = require('randomcolor')
const Enmap = require('enmap')
const EnmapMongo = require('enmap-mongo')
require('log-timestamp')(function () { return '[' + new Date().toISOString() + '] %s' })
const EVENTS_LIST = ['TYPING_START', 'MESSAGE_DELETE', 'MESSAGE_UPDATE', 'PRESENCE_UPDATE', 'VOICE_STATE_UPDATE', 'VOICE_SERVER_UPDATE', 'USER_NOTE_UPDATE', 'CHANNEL_PINS_UPDATE']
const client = new Discord.Client({ disabledEvents: EVENTS_LIST, messageCacheMaxSize: 100 })
client.karmaStore = new Enmap({
provider: new EnmapMongo({
name: 'karmaStore',
dbName: 'enmap',
url: ''
})
})
client.on('message', async (message) => {
if (message.author.bot) return
const check = await rl.check(message)
if (check === true) {
if (message.cleanContent.startsWith(config.prefix)) {
if (message.channel.type === 'dm') return
const keyword = message.cleanContent.replace(config.prefix, '').trim()
if (!client.karmaStore.has(keyword)) {
client.karmaStore.set(keyword, {
numKarma: 0
})
}
try {
await message.reply({
embed: {
color: randomColor(),
author: {
name: client.user.username,
icon_url: client.user.displayAvatarURL()
},
description: `${keyword} has **${client.karmaStore.getProp(keyword, 'numKarma') || 0}** Karma!`,
footer: {
text: 'KarmaBot by .vlexar#0001'
},
timestamp: new Date()
}
})
} catch (e) {
console.error(e)
}
} else if ((message.cleanContent.endsWith('--')) || message.cleanContent.endsWith('++')) {
if (message.channel.type === 'dm') return
if ((message.guild.roles.find(role => role.name === 'NoKarma')) && (message.member.roles.has(message.guild.roles.find(role => role.name === 'NoKarma').id))) {
message.reply('You are not allowed to add or subtract Karma at this time. Please contact a server mod/admin/staff member. Type `@KarmaBot help` for more info.')
return message.react('\uD83D\uDD34')
}
let type
if (message.cleanContent.endsWith('--')) {
type = 'minus'
} else if (message.cleanContent.endsWith('++')) {
type = 'plus'
} else {
return
}
const keyword = message.cleanContent.replace(/([+-]{2,})$/m, '').trim()
if (!client.karmaStore.has(keyword)) {
client.karmaStore.set(keyword, {
numKarma: 0
})
}
if (keyword === '') return
let currentKarma = client.karmaStore.getProp(keyword, 'numKarma')
if (type === 'minus') client.karmaStore.setProp(keyword, 'numKarma', --currentKarma)
else if (type === 'plus') client.karmaStore.setProp(keyword, 'numKarma', ++currentKarma)
console.log(c.bgWhite(`[KARMA] ${c.cyan.bold(keyword)} ${c.red.bold.underline(type)}`))
try {
await message.channel.send({
embed: {
color: randomColor(),
author: {
name: client.user.username,
icon_url: client.user.displayAvatarURL()
},
description: `[KARMA] **${keyword}** has **${client.karmaStore.getProp(keyword, 'numKarma') || 0}** Karma. To lookup later use **${config.prefix}** and type **${config.prefix} ${keyword}**`,
footer: {
text: 'KarmaBot by .vlexar#0001'
},
timestamp: new Date()
}
})
} catch (e) {
console.error(e)
}
}
}
if (message.content.match(new RegExp(`^<@!?${client.user.id}>( |)$`))) {
message.reply('Hi there! Please type `@KarmaBot help` for help using this bot or `@KarmaBot stats` to get bot statistics.')
return message.react('\u2705')
}
if ((message.content.startsWith(`<@!${client.user.id}>` + ' help')) || message.content.startsWith(`<@${client.user.id}>` + ' help')) {
if (message.channel.type === 'dm') return
try {
const embed = new Discord.MessageEmbed()
.setTitle('KarmaBot Help & Information')
.setThumbnail(message.guild.iconURL)
.setURL('https://discord.io/ec')
.setColor(randomColor())
.setDescription('**KarmaBot Help and Information (basic usage, invite URL, support)**')
.addField('**❯❯ Add Karma (++):**', 'To **add or increase** karma, type *any* keyword (can be a username, emoji, or any string of text) followed by two plus symbols **++** For example, typing **keyword++** will increase the karma of keyword by one.', true)
.addField('**❯❯ Subtract Karma (--):**', 'To **subtract or decrease** karma, type *any* keyword (can be a username, emoji, or any string of text) followed by two minus symbols **--** For example, typing **keyword--** will decrease the karma of keyword by one.', true)
.addField('**❯❯ Lookup Karma (>k):**', 'To **lookup** karma, type **>k** followed by the keyword to lookup. For example, typing **>k keyword** will return the karma of keyword. This is shared across all guilds using KarmaBot.', true)
.addField('**❯❯ Blacklist (Per Guild):**', 'To **blacklist** a user from being able to add or subtract Karma in a guild, create the role **NoKarma** and assign it to the users you wish to blacklist. Users can still lookup Karma, so this can act as a way for admins/mods to, for example, award points to users without the users all being able to add/remove Karma. By default this bot will take commands from any user, but messages [are internally rate-limited for spam protection](https://cdn.rawgit.com/shikhir-arora/karma-simple/3848016d/Ratelimiter.js).', true)
.addField('**❯❯ Stats:**', 'For **KarmaBot Stats,** type `@KarmaBot stats` - fun stuff!', true)
.addBlankField()
.addField('**❯❯ Invite KarmaBot:**', '**To Invite KarmaBot**, [click here (requires Manage Server permissions)](https://bot.discord.io/karmabot).', true)
.addField('**❯❯ Support:**', '**For support, visit:** [our Discord server](https://discord.io/ec) or [GitHub](https://github.com/shikhir-arora/karma-simple/issues).', true)
.setFooter('Project by .vlexar#0001 | KarmaBot Help')
.setTimestamp()
await message.reply({ embed })
} catch (e) {
console.error(e)
}
}
if ((message.content.startsWith(`<@!${client.user.id}>` + ' stats')) || message.content.startsWith(`<@${client.user.id}>` + ' stats')) {
try {
const embed = new Discord.MessageEmbed()
.setTitle('KarmaBot Stats')
.setURL('https://karmabot.vlexar.pw')
.setColor(randomColor())
.setDescription('**KarmaBot Stats/Info**')
.addField('**❯❯ Guilds:**', `${client.guilds.size.toLocaleString()}`, false)
.addField('**❯❯ Users:**', `${client.users.size.toLocaleString()}`, false)
.addField('**❯❯ Channels:**', `${client.channels.size.toLocaleString()}`, false)
.addField('**❯❯ Shards:**', `${client.ws.shards.size}`, false)
.addField('**❯❯ Uptime:**', moment.duration(process.uptime(), 'seconds').format('dd:hh:mm:ss'), false)
.addField('**❯❯ CPU:**', `${os.cpus().length}x ${os.cpus()[0].model}`, false)
.addField('**❯❯ Gateway Ping:**', `${client.ws.ping.toFixed(5)} ms`, false)
.addField('**❯❯ Load Average:**', `${os.loadavg()[1]}`, false)
.addField('**❯❯ Memory Usage:**', `${(process.memoryUsage().rss / 1048576).toFixed(2)}MB / ${(os.totalmem() / 1073741824).toFixed(2)}GB`, false)
.addField('**❯❯ System:**', `${os.type()} - ${os.arch()} ${os.release()}`, false)
.addField('**❯❯ Node Version:**', process.version, false)
.addField('**❯❯ Discord.js:**', `v${Discord.version}`, false)
.addField('**❯❯ GitHub:**', '[GitHub Repo](https://github.com/shikhir-arora/karma-simple).', true)
.setFooter('Project by .vlexar#0001 | KarmaBot Stats')
.setTimestamp()
await message.reply({ embed })
} catch (e) {
console.error(e)
}
}
const clean = (text) => {
if (typeof (text) === 'string') { return text.replace(/`/g, '`' + String.fromCharCode(8203)).replace(/@/g, '@' + String.fromCharCode(8203)) } else { return text }
}
const args = message.content.split(' ').slice(1)
if (message.content.startsWith(config.adminprefix + 'eval')) {
if (message.author.id !== config.ownerID) return
try {
const code = args.join(' ')
let evaled = await eval(code) // eslint-disable-line no-eval
if (typeof evaled !== 'string') { evaled = require('util').inspect(evaled, { depth: 0 }) }
if (evaled.includes(client.token || config.token)) {
evaled = evaled.replace(client.token, 'REDACTED!')
}
if (clean(evaled).length > 1800) {
await gist(clean(evaled))
.then(res => {
const embed = new Discord.MessageEmbed()
.setTitle('Eval output exceeds 2000 characters. View on Gist.')
.setURL(`${res.html.url}`)
.setColor(randomColor())
.setDescription(`Eval output exceeds 2000 characters. View Gist [here](${res.html_url}).`)
.setFooter('Eval Output')
.setTimestamp()
message.channel.send({ embed }).catch((e) => message.channel.send(e.message))
})
} else {
message.channel.send(clean(evaled), {
code: 'fix'
})
}
} catch (err) {
console.log(err)
err = err.toString() // eslint-disable-line no-ex-assign
if (err.includes(client.token || config.token)) {
err = err.replace(client.token, 'REDACTED!') // eslint-disable-line no-ex-assign
}
message.channel.send(`\`ERROR\` \`\`\`fix\n${clean(err)}\n\`\`\``)
}
}
if (message.content.startsWith(config.adminprefix + 'exec')) {
if (message.author.id !== config.ownerID) return
exec(args.join(' '), async (e, stdout, stderr) => {
if (stdout.length > 1800 || stderr.length > 1800) {
await gist(`${stdout}\n\n${stderr}`)
.then(res => {
const embed = new Discord.MessageEmbed()
.setTitle('Console output exceeds 2000 characters. View on Gist.')
.setURL(`${res.html_url}`)
.setColor(randomColor())
.setDescription(`Console output exceeds 2000 characters. View Gist [here](${res.html_url}).`)
.setFooter('Exec Output')
.setTimestamp()
message.channel.send({ embed }).catch((e) => message.channel.send(e.message))
})
} else {
stdout && message.channel.send(`\`INFO:\`\n\n\`\`\`fix\n${stdout}\`\`\``)
stderr && message.channel.send(`\`ERRORS:\`\n\n\`\`\`fix\n${stderr}\`\`\``)
if (!stderr && !stdout) { message.react('\u2705') }
}
})
}
})
async function postDiscordStats () {
const discordBots = axios({
method: 'post',
url: `https://discordbots.org/api/bots/${client.user.id}/stats`,
headers: {
Authorization: ''
},
data: {
server_count: client.guilds.size
}
})
const botlistSpace = axios({
method: 'post',
url: `https://api.botlist.space/v1/bots/${client.user.id}`,
headers: {
Authorization: ''
},
data: {
server_count: client.guilds.size
}
})
const botsgg = axios({
method: 'post',
url: `https://discord.bots.gg/api/v1/bots/${client.user.id}/stats`,
headers: {
Authorization: ''
},
data: {
guildCount: client.guilds.size
}
})
// eslint-disable-next-line no-unused-vars
const [dbres, bspaceres, botsggres] = await Promise.all([discordBots, botlistSpace, botsgg]) // lgtm [js/unused-local-variable]
}
client.on('ready', () => {
console.log(c.bgWhite(`[READY] Connected as ${c.red.bold.underline(client.user.username)}#${c.cyan.bold(client.user.discriminator)} ${c.green.bold(client.user.id)}`))
setInterval(() => client.user.setActivity('@KarmaBot help', { type: 'WATCHING' }), 90000)
postDiscordStats()
})
client.on('guildCreate', (guild) => {
console.log(c.bgGreen(`New guild joined: ${c.blue.bold.underline(guild.name)} (id: ${c.yellow.italic(guild.id)}). This guild has ${c.green.underline(guild.memberCount)} members!`))
postDiscordStats()
})
client.on('guildDelete', (guild) => {
console.log(c.bgWhite.underline(`I have been removed from: ${c.red.bold.underline(guild.name)} (id: ${c.yellow.bold(guild.id)})`))
postDiscordStats()
})
client.on('disconnect', (event) => {
setTimeout(() => client.destroy().then(() => client.login(config.token)), 10000)
console.log(c.bgRed.underline(`[DISCONNECT] Notice: Disconnected from gateway with code ${event.code} - Attempting reconnect.`))
})
client.on('reconnecting', () => {
console.log(c.bgYellow.italic('[NOTICE] ReconnectAction: Reconnecting to Discord...'))
})
client.on('rateLimit', console.log)
client.on('error', console.error)
client.on('warn', console.warn)
process.on('unhandledRejection', (error) => {
console.error(c.bgRed(`Uncaught Promise Error: \n${error.stack}`))
})
process.on('uncaughtException', (err) => {
const errmsg = (err ? err.stack || err : '').toString().replace(new RegExp(`${__dirname}/`, 'g'), './')
console.error(c.red.bold(errmsg))
})
client.login(config.token)