forked from Dougley/DiscordFeedback
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
108 lines (98 loc) · 3.76 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
const Discordie = require('discordie')
const UserVoice = require('uservoice-nodejs')
const Events = Discordie.Events
const Config = require('./config.js')
const Commands = require('./Utils/command_engine').Commands
const AccessChecker = require('./Utils/access_checker')
const ErrorLog = require('./Utils/error_loggers')
const GenericLog = require('./Utils/generic_logger')
const bot = new Discordie({
autoReconnect: true
})
var bugsnag = require('bugsnag')
bugsnag.register(Config.discord.bugsnag)
const UVRegex = /http[s]?:\/\/[\w.]*\/forums\/([0-9]{6,})-[\w-]+\/suggestions\/([0-9]{8,})-[\w-]*/
var uvClient = {
v1: new UserVoice.Client({
subdomain: Config.uservoice.subdomain.trim(),
domain: Config.uservoice.domain.trim(),
apiKey: Config.uservoice.key.trim(),
apiSecret: Config.uservoice.secret.trim()
}),
v2: new UserVoice.ClientV2({
clientId: Config.uservoice.key.trim(),
subdomain: Config.uservoice.domain.trim()
})
}
bot.Dispatcher.on(Events.MESSAGE_CREATE, (c) => {
if (c.message.channel.id !== Config.discord.feedChannel && c.message.content.match(UVRegex) !== null && c.message.content.indexOf(Config.discord.prefix) !== 0) {
let parts = c.message.content.match(UVRegex)
Commands['chatVoteInit'].fn(c.message, parts[2], uvClient)
}
if (c.message.channel.id === Config.discord.feedChannel && c.message.author.id !== bot.User.id && c.message.author.bot) {
Commands['newCardInit'].fn(c.message)
return
}
if (c.message.isPrivate === true) return
if (c.message.content.indexOf(Config.discord.prefix) === 0) {
var cmd = c.message.content.substr(Config.discord.prefix.length).split(' ')[0].toLowerCase()
var suffix
suffix = c.message.content.substr(Config.discord.prefix.length).split(' ')
suffix = suffix.slice(1, suffix.length).join(' ')
var msg = c.message
if (Commands[cmd]) {
if (Commands[cmd].internal === true) return
AccessChecker.getLevel(msg.member, (level) => {
if (level === 0 && Commands[cmd].modOnly === true) {
if (Commands[cmd].phantom !== undefined) msg.reply('this command is restricted, and not available to you.')
return
} else if (level !== 2 && Commands[cmd].adminOnly === true) return
try {
Commands[cmd].fn(bot, msg, suffix, uvClient, function (res) {
GenericLog.log(bot, c.message.author, {
message: `Ran the command \`${cmd}\``,
result: res.result,
affected: res.affected
})
})
} catch (e) {
ErrorLog.log(bot, {
cause: cmd,
message: e.message
})
msg.reply('an error occurred while processing this command, the admins have been alerted, please try again later')
}
})
}
}
})
bot.Dispatcher.on(Events.MESSAGE_REACTION_ADD, (m) => {
if (m.user.id !== bot.User.id) {
if (m.message === null) {
bot.Channels.get(m.data.channel_id).fetchMessages().then(() => {
m.message = bot.Messages.get(m.data.message_id)
})
}
Commands['registerVote'].fn(m.message, m.emoji, bot, uvClient, m.user)
}
})
bot.Dispatcher.on(Events.GATEWAY_READY, () => {
setInterval(() => {
bot.Users.fetchMembers() // Hacky way to cache offline users, #blamelazyloading
}, 600000)
console.log('Feedback bot is ready!')
})
process.on('unhandledRejection', (reason, p) => {
if (p !== null && reason !== null) {
bugsnag.notify(new Error(`Unhandled promise: ${require('util').inspect(p, {depth: 3})}: ${reason}`))
}
})
bot.Dispatcher.on(Events.DISCONNECTED, (e) => {
console.error('Connection to Discord has been lost... Delay till reconnect:', e.delay)
})
bot.Dispatcher.on(Events.GATEWAY_RESUMED, () => {
console.log('Reconnected.')
})
bot.connect({
token: Config.discord.token
})