/
index.js
321 lines (286 loc) · 9.53 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
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
312
313
314
315
316
317
318
319
320
321
import { Plugin, command } from 'munar-core'
import random from 'random-item'
import moment from 'moment'
import * as utils from './utils'
import KarmaModel from './Karma'
const debug = require('debug')('munar:karma')
export default class UserKarma extends Plugin {
static description = 'Keeps track of users\' earned internet points.'
constructor (bot, options) {
super(bot, options)
this.models({
Karma: KarmaModel
})
}
@command('karma', {
description: 'Show a user\'s karma.',
arguments: [
command.arg.user(),
command.arg.string()
]
})
async karma (message, username, time = 'w') {
const User = this.model('User')
const Karma = this.model('Karma')
let self = false
if (username && /(^[dwmf]$)|(^\d+$)/.test(username)) {
self = true
time = username
}
const since = utils.spanToTime(time)
const hours = moment().diff(since, 'hours')
const allTime = time === 'f'
if (username && username.charAt(0) === '@') username = username.slice(1)
if (!self && username) {
const adapter = message.source.getAdapterName()
const other = message.source.getUserByName(username)
let user
if (other) {
user = await User.from(other)
}
if (!user) {
user = await this.bot.findUser(username, { adapter: adapter })
}
if (!user) {
message.reply(`I don't know ${username} yet`)
return
}
const karmaList = await Karma.find({
createdAt: { $gte: since.toDate() },
target: user._id
}).select('amount')
const karma = karmaList.reduce((a, b) => a + b.amount, 0)
let msg = `${user.username} has ${karma} karma`
if (!allTime) msg += ` from the past ${utils.days(hours)}`
message.reply(`${msg}.`)
} else {
const user = await User.from(message.user)
if (!user) {
message.reply('who are you?')
return
}
const karmaList = await Karma.find({
createdAt: { $gte: since.toDate() },
target: user._id
}).select('amount')
const karma = karmaList.reduce((a, b) => a + b.amount, 0)
let msg = `you have ${karma} karma`
if (!allTime) msg += ` from the past ${utils.days(hours)}`
message.reply(`${msg}.`)
}
}
@command('karmawhores', {
description: 'Show the top 5 most popular users.',
arguments: [
command.arg.string()
]
})
async karmawhores (message, time = 'w') {
const User = this.model('User')
const Karma = this.model('Karma')
// stole all this shit from !mostplayed in MediaStats
const since = utils.spanToTime(time)
const hours = moment().diff(since, 'hours')
const allTime = time === 'f'
const karmaList = await Karma.aggregate()
.match({ createdAt: { $gte: since.toDate() } })
.group({ _id: '$target', karma: { $sum: '$amount' } })
.sort({ karma: -1 })
.limit(5)
.exec()
if (karmaList.length === 0) {
message.reply('none of you like each other T_T')
} else {
let karmas = {}
karmaList.forEach((k) => {
karmas[k._id] = k.karma
})
const users = await User.where('_id').in(Object.keys(karmas)).lean()
users.forEach((model) => {
model.karma = karmas[model._id]
})
users.sort((a, b) => a.karma > b.karma ? -1 : 1)
let response = 'Karma leaders'
if (!allTime) response += ` over the last ${utils.days(hours)}`
response += ':\n' + users.map((u, rank) => `${rank + 1} - ${u.username}: ${u.karma}`).join('\n')
message.reply(response)
}
}
@command('bump', {
description: 'Increase someone\'s karma.',
arguments: [
command.arg.user()
]
})
async bump (message, username, ...reason) {
const User = this.model('User')
const Karma = this.model('Karma')
const { user } = message
if (!username) {
message.reply('You must provide a user to bump')
return
}
if (username.charAt(0) === '@') username = username.slice(1)
let other = message.source.getUserByName(username)
if (!other) {
message.reply('That\'s not a real person…')
return
}
if (other.id === user.id) {
message.reply('Nice try, smartass!')
return
}
debug('karma bump', `${other.username} (${other.id})`)
const target = await User.from(other)
const giver = await User.from(user)
if (target) {
let bump = new Karma({ target, giver })
if (reason && reason.length > 0) {
bump.reason = reason.join(' ')
}
await bump.save()
if (reason && reason.length > 0) {
message.send(`@${user.username} bumped @${other.username}\'s karma ${reason.join(' ')}`)
} else {
message.send(`@${user.username} bumped @${other.username}\'s karma!`)
}
return target
}
}
@command('thump', {
description: 'Decrease someone\'s karma.',
arguments: [
command.arg.user()
]
})
async thump (message, username, ...reason) {
const User = this.model('User')
const Karma = this.model('Karma')
const { user } = message
if (!username) {
debug('karma thump no username')
message.reply('You must provide a user to thump')
return
}
if (username.charAt(0) === '@') username = username.slice(1)
let other = message.source.getUserByName(username)
if (!other) {
message.reply('That\'s not a real person…')
return
}
if (other.id === user.id) {
message.reply('Whatever you want')
}
const target = await User.from(other)
const giver = await User.from(user)
if (target) {
let thump = new Karma({ target, giver, amount: -1 })
if (reason && reason.length > 0) {
thump.reason = reason.join(' ')
}
await thump.save()
if (reason && reason.length > 0) {
message.send(`@${user.username} thumped @${other.username}\'s karma ${reason.join(' ')}`)
} else {
message.send(`@${user.username} thumped @${other.username}\'s karma!`)
}
return target
}
}
@command('bitch', {
description: 'Show the user who has given you the most karma recently.',
arguments: [
command.arg.string()
]
})
async bitch (message, time = 'w') {
const User = this.model('User')
const Karma = this.model('Karma')
const since = utils.spanToTime(time)
const hours = moment().diff(since, 'hours')
const allTime = time === 'f'
const user = await User.from(message.user)
const karmaList = await Karma.aggregate()
.match({ target: user._id, giver: { $ne: 0 }, createdAt: { $gte: since.toDate() } })
.group({ _id: '$giver', karma: { $sum: '$amount' } })
.sort({ karma: -1 })
.exec()
if (karmaList.length === 0) {
let msg = 'you have no bitches'
if (!allTime) msg += ` from the past ${utils.days(hours)}`
message.reply(`${msg}. :cry:`)
} else {
let karma = karmaList[0].karma
const usar = await User.findById(karmaList[0]._id)
let msg = `${usar.username} is your bitch. They gave you ${karma} karma`
if (!allTime) msg += ` in the past ${utils.days(hours)}`
message.reply(`${msg}.`)
}
}
@command('pimp', {
description: 'Show the user who has received the most karma from you recently.',
arguments: [
command.arg.string()
]
})
async pimp (message, time = 'w') {
const User = this.model('User')
const Karma = this.model('Karma')
const since = utils.spanToTime(time)
const hours = moment().diff(since, 'hours')
const allTime = time === 'f'
const user = await User.from(message.user)
const karmaList = await Karma.aggregate()
.match({ giver: user._id, target: { $ne: 0 }, createdAt: { $gte: since.toDate() } })
.group({ _id: '$target', karma: { $sum: '$amount' } })
.sort({ karma: -1 })
.exec()
if (karmaList.length === 0) {
let msg = 'you have been noone\'s bitch'
if (!allTime) msg += ` in the past ${utils.days(hours)}`
message.reply(`${msg}. :smirk:`)
} else {
let karma = karmaList[0].karma
const usar = await User.findById(karmaList[0]._id)
let msg = `${usar.username} is your pimp. You gave them ${karma} karma`
if (!allTime) msg += ` in the past ${utils.days(hours)}`
message.reply(`${msg}.`)
}
}
@command('fistbump', {
description: 'Show a random bump.'
})
async fistbump (message) {
const User = this.model('User')
const Karma = this.model('Karma')
const user = await User.from(message.user)
const reasonList = await Karma.find({ target: user._id, reason: { $ne: null } })
.where('amount').gt(0)
.populate('giver')
if (reasonList.length > 0) {
let chosen = random(reasonList)
message.reply(`you were bumped by @${chosen.giver.username} ` +
`"${chosen.reason}" ${moment(chosen.createdAt).fromNow()}`)
} else {
message.reply('no one can explain your mysterious allure.')
}
}
@command('fistthump', {
description: 'Show a random thump.'
})
async fistthump (message) {
const User = this.model('User')
const Karma = this.model('Karma')
const user = await User.from(message.user)
const reasonList = await Karma.find({ target: user._id, reason: { $ne: null } })
.where('amount').lt(0)
.populate('giver')
if (reasonList.length > 0) {
let chosen = random(reasonList)
message.reply(`you were thumped by @${chosen.giver.username} ` +
`"${chosen.reason}" ${moment(chosen.createdAt).fromNow()}`)
} else {
message.reply('no reason was ever given.')
}
}
}