This repository has been archived by the owner on Nov 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
203 lines (166 loc) · 8.22 KB
/
main.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
const fs = require('fs');
const Discord = require('discord.js');
const RssFeedEmitter = require('rss-feed-emitter');
const config = require('./data/config.json');
const disabled = require('./data/disabled.json');
const connection = require('./dbPromised.js');
const experience_function = require('./custom_modules/experience.js');
const currency_function = require('./custom_modules/currency.js');
const { hearts, sad } = require('./data/constants.js');
const GuildSettings = require('./guildSettings.js');
const reactionRecently = new Set();
const client = new Discord.Client({autoReconnect:true});
const cooldowns = new Discord.Collection();
client.commands = new Discord.Collection();
const webhook = new Discord.WebhookClient(config.webhook.id, config.webhook.token);
client.config = config;
const settings = new GuildSettings();
let feeder = new RssFeedEmitter();
feeder.add({
url: 'http://blog.humblebundle.com/rss',
refresh: 2000
});
async function push_error(error) {
webhook.send(`**A global error has occurred**\n\`\`\`js\n${error}\`\`\``)
};
module.exports.feeder = feeder;
module.exports.settings = settings;
module.exports.client = client;
module.exports.webhook = webhook;
module.exports.errorEvent = push_error;
// ------------------------------------------------------------------------------------//
client.on('error', (err) => {
console.log(err);
})
fs.readdir("./events/", (err, files) => {
if (err) return console.error(err);
files.forEach(file => {
let eventFunction = require(`./events/${file}`);
let eventName = file.split(".")[0];
client.on(eventName, (...args) => eventFunction.run(client, ...args));
});
});
const commandFiles = fs.readdirSync('./commands');
for (const file of commandFiles) {
if (fs.lstatSync(`./commands/${file}`).isDirectory()) {
const subDirection = `./commands/${file}`;
const subCommandFiles = fs.readdirSync(`${subDirection}`)
for (const fileInDir of subCommandFiles) {
const command = require(`${subDirection}/${fileInDir}`);
client.commands.set(command.name, command);
}
} else {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
}
async function register_command(message, command) {
if (message.guild && (!command.testMode || !command.ownerOnly)) {
await connection.execute("INSERT INTO commands (guild_id, channel_id, author_id, prefix, command) VALUES (?, ?, ?, ?, ?);", [message.guild.id, message.channel.id, message.author.id, settings.get(message, 'prefix'), command.name]);
};
}
client.on('message', message => {
if (!message.author.bot && message.channel.type == 'text') {
experience_function(message)
currency_function(message)
}
if (!message.guild.me.permissionsIn(message.channel).has('SEND_MESSAGES')) {
return;
}
if (!message.author.bot && !reactionRecently.has(message.author.id)) { // && Math.floor(Math.random() * 5) + 1 === 2
if (message.content.includes(`❤`) || message.content.includes(`♥`)) {
message.channel.send(`${hearts[Math.floor(hearts.length * Math.random())]}`)
.catch(() => {})
}
if (message.content.includes(`😢`) || message.content.includes(`😭`) || message.content.includes(`😦`)) {
message.channel.send(`${sad[Math.floor(sad.length * Math.random())]}`)
.catch(() => {})
}
reactionRecently.add(message.author.id);
setTimeout(() => {
reactionRecently.delete(message.author.id);
}, 60000);
}
const prefixRegex = new RegExp(`^(<@!?${client.user.id}>|${settings.get(message, 'prefix').replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&')})\\s*`);
if (!prefixRegex.test(message.content) || message.author.bot) return;
const [matchedPrefix] = message.content.match(prefixRegex);
const botMentionRegex = new RegExp(`<@!?${client.user.id}>`);
if (botMentionRegex.test(matchedPrefix)) {
if (!botMentionRegex.test(message.content.slice(matchedPrefix.length))) {
message.mentions.users.delete(client.user.id);
if (message.mentions.members) {
message.mentions.members.delete(client.user.id);
}
}
}
const args = message.content.slice(matchedPrefix.length).split(/\s+/);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName)
|| client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
if (!command) return;
if (command.guildOnly && message.channel.type !== 'text') {
return message.reply('I can\'t execute that command inside DMs!');
}
if (!config.owners.includes(message.author.id)) {
if (disabled.channels.includes(message.channel.id)) {
if (message.member.id !== message.guild.owner.id) { return; };
if (!message.member.permissions.has("ADMINISTRATOR")) { return; };
if (!message.member.permissions.has("MANAGE_GUILD")) { return; };
};
if (command.NSFW && !message.channel.nsfw) {
return message.reply(`this command can only be used in a NSFW channel, gomenesai desu~`);
}
if (command.ownerOnly) {
return message.reply(`you're not allowed to use this command, gomenesai desu~`);
}
if (command.disabled){
return message.reply(`this command is disabled globally and can not be used, gomenesai desu~`);
}
if (command.permissions) {
if (message.guild) {
if (command.permissions === 'guild_owner' && message.member.id !== message.guild.owner.id) {
return message.reply(`you need to be the \`Guild Owner\` for this command, gomenesai desu~`)
}
if (command.permissions === 'administrator' && !message.member.permissions.has("ADMINISTRATOR")) {
return message.reply(`you need to have the \`Administrator\` permission for this command, gomenesai desu~`)
}
if (command.permissions === 'manage_guild' && !message.member.permissions.has("MANAGE_GUILD")) {
return message.reply(`you need to have the \`Manage Guild\` permission for this command, gomenesai desu~`)
}
} else {
return;
}
}
if (!cooldowns.has(command.name)) {
cooldowns.set(command.name, new Discord.Collection());
}
const now = Date.now();
const timestamps = cooldowns.get(command.name);
const cooldownAmount = (command.cooldown || 3) * 1000;
if (!timestamps.has(message.author.id)) {
timestamps.set(message.author.id, now);
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
}
else {
const expirationTime = timestamps.get(message.author.id) + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
return message.reply(`please wait ${timeLeft.toFixed(1)} more second(s) before reusing the \`${command.name}\` command.`);
}
timestamps.set(message.author.id, now);
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
}
}
if (command.args && !args.length) {
return message.reply(`this command requires arguments.\nFor more help about this command use: \`${settings.get(message, 'prefix')}help ${commandName}\``);
}
try {
register_command(message, command);
command.execute(message, args);
} catch (error) {
webhook.send(`**A global error has occurred**\n\`\`\`js\n${error.stack}\`\`\``)
console.error(error);
message.reply('there was an error trying to execute that command!');
}
});
client.login(config.token);