-
Notifications
You must be signed in to change notification settings - Fork 1
/
Naho.js
executable file
·149 lines (135 loc) · 5.16 KB
/
Naho.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
// Naho Discord Bot (c) 2019 - 2021 Shin#0484
const { FriendlyError } = require('discord.js-commando');
const { oneLine } = require('common-tags');
const path = require('path');
const winston = require('winston');
const SequelizeProvider = require('./providers/Sequelize');
const Currency = require('./structures/currency/Currency');
const Experience = require('./structures/currency/Experience');
const userName = require('./models/UserName');
const CommandoClient = require('./structures/CommandoClient');
const { OWNERS, COMMAND_PREFIX, TOKEN } = process.env;
const client = new CommandoClient({
owner: OWNERS.split(','),
commandPrefix: COMMAND_PREFIX,
unknownCommandResponse: false,
disableEveryone: true
});
let earnedRecently = [];
let gainedXPRecently = [];
// Set Sequelize Provided via database
client.setProvider(new SequelizeProvider(client.database));
client.dispatcher.addInhibitor(msg => {
const blacklist = client.provider.get('global', 'userBlacklist', []);
if (!blacklist.includes(msg.author.id)) return false;
return `Has been blacklisted.`;
});
// Advanced Error logging
client.on('error', winston.error)
.on('warn', winston.warn)
.once('ready', () => Currency.leaderboard())
.on('ready', () => {
winston.info(oneLine`
[DISCORD]: Client ready...
Logged in as ${client.user.tag} (${client.user.id})
`);
})
.on('disconnect', () => winston.warn('[DISCORD]: Disconnected!'))
.on('reconnect', () => winston.warn('[DISCORD]: Reconnecting...'))
.on('commandRun', (cmd, promise, msg, args) =>
winston.info(oneLine`
[DISCORD]: ${msg.author.tag} (${msg.author.id})
> ${msg.guild ? `${msg.guild.name} (${msg.guild.id})` : 'DM'}
>> ${cmd.groupID}:${cmd.memberName}
${Object.values(args).length ? `>>> ${Object.values(args)}` : ''}
`)
)
.on('unknownCommand', msg => {
if (msg.channel.type === 'dm') return;
if (msg.author.bot) return;
if (msg.content.split(msg.guild.commandPrefix)[1] === 'undefined') return;
const args = { name: msg.content.split(msg.guild.commandPrefix)[1].toLowerCase() };
client.registry.resolveCommand('tags:tag').run(msg, args);
})
.on('message', async message => {
if (message.channel.type === 'dm') return;
if (message.author.bot) return;
const channelLocks = client.provider.get(message.guild.id, 'locks', []);
if (channelLocks.includes(message.channel.id)) return;
if (!earnedRecently.includes(message.author.id)) {
const hasImageAttachment = message.attachments.some(attachment =>
attachment.url.match(/\.(png|jpg|jpeg|gif|webp)$/)
);
const moneyEarned = hasImageAttachment
? Math.ceil(Math.random() * 7) + 5
: Math.ceil(Math.random() * 7) + 1;
Currency._changeBalance(message.author.id, moneyEarned);
earnedRecently.push(message.author.id);
setTimeout(() => {
const index = earnedRecently.indexOf(message.author.id);
earnedRecently.splice(index, 1);
}, 8000);
}
if (!gainedXPRecently.includes(message.author.id)) {
const xpEarned = Math.ceil(Math.random() * 9) + 3;
const oldLevel = await Experience.getLevel(message.author.id);
Experience.addExperience(message.author.id, xpEarned).then(async () => {
const newLevel = await Experience.getLevel(message.author.id);
if (newLevel > oldLevel) {
Currency._changeBalance(message.author.id, 100 * newLevel);
}
}).catch(err => null); // eslint-disable-line no-unused-vars, handle-callback-err
gainedXPRecently.push(message.author.id);
setTimeout(() => {
const index = gainedXPRecently.indexOf(message.author.id);
gainedXPRecently.splice(index, 1);
}, 60 * 1000);
}
})
.on('commandError', (cmd, err) => {
if (err instanceof FriendlyError) return;
winston.error(`[DISCORD]: Error in command ${cmd.groupID}:${cmd.memberName}`, err);
})
.on('commandBlocked', (msg, reason) => {
winston.info(oneLine`
[DISCORD]: Command ${msg.command ? `${msg.command.groupID}:${msg.command.memberName}` : ''}
blocked; User ${msg.author.tag} (${msg.author.id}): ${reason}
`);
})
.on('commandPrefixChange', (guild, prefix) => {
winston.info(oneLine`
[DISCORD]: Prefix changed to ${prefix || 'the default'}
${guild ? `in guild ${guild.name} (${guild.id})` : 'globally'}.
`);
})
.on('commandStatusChange', (guild, command, enabled) => {
winston.info(oneLine`
[DISCORD]: Command ${command.groupID}:${command.memberName}
${enabled ? 'enabled' : 'disabled'}
${guild ? `in guild ${guild.name} (${guild.id})` : 'globally'}.
`);
})
.on('groupStatusChange', (guild, group, enabled) => {
winston.info(oneLine`
[DISCORD]: Group ${group.id}
${enabled ? 'enabled' : 'disabled'}
${guild ? `in guild ${guild.name} (${guild.id})` : 'globally'}.
`);
})
.on('userUpdate', (oldUser, newUser) => {
if (oldUser.username !== newUser.username) {
userName.create({ userID: newUser.id, username: oldUser.username }).catch(err => null); // eslint-disable-line no-unused-vars, handle-callback-err, max-len
}
});
// Client Command Categories
client.registry
.registerGroups([
['economy', 'Economy'],
['social', 'Social'],
['games', 'Games'],
['item', 'Item']
])
.registerDefaults()
.registerTypesIn(path.join(__dirname, 'types'))
.registerCommandsIn(path.join(__dirname, 'commands'));
client.login(TOKEN);