This repository has been archived by the owner on Aug 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
281 lines (240 loc) · 9.07 KB
/
app.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
const Discord = require('discord.js')
const Neko = require('nekos.life')
const Redis = require('ioredis')
const Database = require('sqlite-async')
const RateLimiter = require('async-ratelimiter')
const properties = require('properties')
const winston = require('winston')
const _client = new Discord.Client()
const _neko = new Neko()
// SQLite 3 queries
const CREATE_TABLE = 'CREATE TABLE IF NOT EXISTS channels (`_Guild` BLOB UNIQUE, _Channel BLOB UNIQUE);'
const INSERT_OR_REPLACE = 'INSERT OR REPLACE INTO channels (_Guild,_Channel) VALUES ($guild,$channel);'
const SELECT_FROM = 'SELECT * FROM channels WHERE _Guild=$guild LIMIT 1;'
const DELETE_FROM = 'DELETE FROM channels WHERE _Guild=$guild;'
// Bot commands
const _commands = {
setchannel: {
permission: 'ADMINISTRATOR',
trigger: async function (message, args) {
const limit = await _limiter.get({
id: message.guild.id
})
if (!_config.debug && !limit.remaining) {
_neko.sfw.OwOify({
text: `You've already changed the channel a while ago, please be patient`
}).then(function (res) {
message.reply(`${res.owo}~`)
})
return
}
_store.run(INSERT_OR_REPLACE, {
$guild: message.guild.id,
$channel: message.channel.id
}).then(() => {
_logger.debug(`User ${message.author.tag} set the channel of ${message.guild.name} to #${message.channel.name}.`)
_neko.sfw.OwOify({
text: `I'm now serving in {0} for you!`
}).then(function (res) {
message.reply(`${res.owo}~`.replace('{0}', `<#${message.channel.id}>`))
})
})
}
}
}
properties.parse('./config.properties', {
path: true
}, function (error, prop) {
if (error) console.log(error)
else {
_config = prop
start()
}
})
var _logger
var _config
var _limiter
var _store
async function start() {
_logger = winston.createLogger({
level: _config.debug ? 'debug' : 'info',
transports: [
new winston.transports.Console({
format: winston.format.combine(
winston.format.splat(),
winston.format.colorize(),
winston.format.simple()
)
}),
new winston.transports.File({
filename: 'log.txt',
format: winston.format.combine(
winston.format.splat(),
winston.format.timestamp(),
winston.format.json()
)
})
]
})
_limiter = new RateLimiter({
db: new Redis(_config.redis),
duration: 600000,
max: 1
})
Database.open('store.db')
.then(database => {
_store = database
_store.run(CREATE_TABLE).then(() => {
_logger.debug(`Connected to sqlite-database ${_store.filename}`)
})
})
.catch(error => {
_logger.error(`Could not connect to sqlite-database ${_store.filename}: ${error.message}`)
return
})
_client.login(_config.token)
if (_config.debug)
_logger.warn('This bot is running in \'DEBUG\' mode, consider disabling it if you notice any inconvenience.')
}
function isBotOwner(user) {
return new Promise(function (fulfill, reject) {
_client.fetchApplication()
.then(application => fulfill(application.owner.id === user.id))
.catch(error => {
_logger.debug(`Could not fetch bot-owner status of ${user.tag}: ${error.message}`)
fulfill(false)
})
})
}
_client.once('ready', async function () {
_logger.info(`The Disord-client (${_client.user.tag}) logged in and is currently serving for ${_client.guilds.size} guild(s).`)
_client.user.setPresence({
game: {
name: _config.debug ? 'debug-mode (Maintenance)' : '@nekos-chat~',
type: _config.debug ? 'PLAYING' : 'LISTENING'
},
status: _config.debug ? 'dnd' : 'available'
})
})
_client.on('guildCreate', async function (guild) {
_logger.info(`Invited to guild: ${guild.name}`)
guild.systemChannel.send({
embed: {
title: 'nekos-chat~',
description: 'Allows you to communicate with \*real* [nekos.life](https://nekos.life) nekos!',
color: 12390624,
thumbnail: {
url: _client.user.avatarURL
},
fields: [{
name: 'Setup',
value: `Just type ${_config.prefix}setchannel in the channel you want me to chill in.`
},
{
name: 'OpenSource',
value: '[GitHub](https://github.com/syntax-yt/NekosChat)',
inline: true
},
{
name: 'Endpoint',
value: '[nekos.life](https://nekos.life/api/v2/chat)',
inline: true
}
]
}
}).catch(error => _logger.warn(`Could not send greeting to #${guild.defaultChannel.name} in ${guild.name}: ${error.message}`))
})
_client.on('guildDelete', async function (guild) {
_logger.info(`Kicked from guild ${guild.name}`)
_store.run(DELETE_FROM, {
$guild: guild.id
})
.then(() => _logger.debug(`Deleted data for guild with id ${guild.id}.`))
.catch(error => _logger.warn(`Could not delete data for guild with id ${guild.id}.`))
})
_client.on('error', async function (error) {
_logger.error(`A discord-error ocurred: ${error.message}`)
})
_client.on('message', async function (message) {
if (message.author.bot) {
_logger.debug(`Ignored 'message' event #${message.id} because author of the message is not an user.`)
return
}
if (!message.cleanContent.startsWith(_config.prefix)) {
let mention = message.mentions.users.first()
if (mention && _client.user.id === mention.id) {
_store.get(SELECT_FROM, {
$guild: message.guild.id
}).then(res => {
if (!res) return
if (message.channel.id !== res['_Channel']) return
let query = message.content.replace(/ *\<[^\]]*>/, '')
if (!query.replace(/\s/g, '').length) {
message.channel.send({
embed: {
title: 'nekos-chat~',
description: 'Allows you to communicate with \*real* [nekos.life](https://nekos.life) nekos!',
thumbnail: {
url: _client.user.avatarURL
},
color: 12390624,
fields: [{
name: 'OpenSource',
value: '[GitHub](https://github.com/syntax-yt/NekosChat)',
inline: true
},
{
name: 'Endpoint',
value: '[nekos.life](https://nekos.life/api/v2/chat)',
inline: true
}
]
}
})
return
}
_neko.sfw.chat({
text: query,
owo: Math.random() >= 0.5
}).then(chat => {
message.reply(chat.response.replace(/ *\<[^\]]*>/, ''))
})
})
}
return
}
let args = message.cleanContent.toLowerCase().substr(_config.prefix.length).split(' ')
if (args[0] === '') return
isBotOwner(message.author).then(val => {
if (_config.debug && !val) {
_logger.debug(`Ignored 'message' event #${message.id} because author of the message is not the bot owner while the debug-mode is enabled.`)
return
}
let command = _commands[args[0]]
if (!command) {
_neko.sfw.OwOify({
text: 'Sorry but I don\'t know how to do that'
}).then(function (res) {
message.reply(`${res.owo}~`)
})
return
}
if (command.permission && !message.member.hasPermission(command.permission)) {
_neko.sfw.OwOify({
text: `Sorry but you're lacking some permissions`
}).then(function (res) {
message.reply(`${res.owo}~`)
})
return
}
try {
command.trigger(message, args.shift())
} catch (error) {
_neko.sfw.OwOify({
text: `Sorry but an internal error occurred`
}).then(function (res) {
message.reply(`${res.owo}~\`\`\`${error}\`\`\``)
})
}
})
})