-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
90 lines (81 loc) · 2.36 KB
/
app.js
File metadata and controls
90 lines (81 loc) · 2.36 KB
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
#!/usr/bin/env node
const fs = require('fs');
const Discord = require('discord.js');
const client = new Discord.Client();
const { prefix, token } = require('./config.json');
const { Sequelize, DataTypes } = require('sequelize');
// Utilities
function loadDir(path, fn)
{
const dir = fs.readdirSync(path).filter(f => f.endsWith('.js'));
for (f of dir) {
e = require(`${path}/${f}`);
fn(e);
}
}
// Set-up database
const sql = new Sequelize({
dialect: 'sqlite',
logging: false,
storage: 'db.sqlite'
});
loadDir('./mdls', e => {
e(sql, DataTypes);
});
sql.sync();
const Users = sql.model('Users');
// Set-up command handling
client.cmds = new Discord.Collection();
loadDir('./cmds', e => {
client.cmds.set(e.name, e);
});
// Set-up word filters
const filter = new RegExp(fs.readFileSync('./filters.txt', 'utf8')
.split('\n')
.map(x => x.trim())
.filter(x => !x.startsWith('#'))
.join('|')
.slice(0, -1),
'gi');
client.once('ready', () => {
console.log('Up and running!');
});
client.on('message', msg => {
if (msg.author.bot) return;
// Execute a command
if (msg.content.startsWith(prefix)) {
const args = msg.content.slice(prefix.length).trim().split(/ +/);
const cmd = args.shift().toLowerCase();
const fn = client.cmds.get(cmd);
if (fn != undefined) {
fn.execute.call(sql, msg, args);
} else {
msg.react('350710108684025857');
}
}
// Look for filtered words
if (msg.content.match(filter)) {
const target = msg.author;
const errfn = err => {
console.error(err);
msg.react('350710109367828481');
};
Users.findByPk(target.id).then(
usr => {
if (usr) {
usr.amount += 1;
return usr.save();
} else {
return Users.create({
id: target.id,
username: target.username,
amount: 1
});
}
}, errfn).then(
ok => {
msg.react('350710110215208960');
}, errfn);
}
});
client.login(token);