This repository has been archived by the owner on May 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
199 lines (175 loc) · 6.36 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
//modules and files
const discord = require("discord.js");
const requireDir = require('require-dir');
const botCmd = requireDir("./commands", { recurse: true });
const parseOptions = require("./parser.js");
const updateServerCount = require("./serverCount.js");
const settings = require("./settings.json");
const tokens = require("./tokens.json");
console.log("registerd commands:\n", botCmd);
//new client
const client = new discord.Client
client.login(tokens.discord);
//events
client
.on("error", (err) => {
console.error(err);
})
.on("warn", console.warn)
.on("debug", console.log)
.on("ready", () => {
console.log(`Client ready; logged in as ${client.user.username}#${client.user.discriminator} (${client.user.id})`);
client.user.setGame(`ASCII help`);
//updates server count on startup
updateServerCount(client.guilds.size, client.user.id);
})
.on("disconnect", () => { console.warn("Disconnected!"); })
.on("reconnecting", () => { console.warn("Reconnecting..."); })
.on("guildCreate", (guild) => {
console.log(`ASCII bot has been added to the guild ${guild.name}`);
updateServerCount(client.guilds.size, client.user.id);
})
.on("guildDelete", (guild) => {
console.log(`ASCII bot has been removed from the guild ${guild.name}`);
updateServerCount(client.guilds.size, client.user.id);
})
.on("message", (message) => {
//ignore bots (and itself)
if (message.author.bot) return;
//check if message is a botCmd
let isCmd = cmdTest(message, message.content);
if (!isCmd.isCmd) return;
let alreadyRan = false;
//check if message has attached images
if (message.attachments.size > 0) {
message.attachments.forEach((attachment) => {
//if the attachment is an image
if (!attachment.width || !attachment.height) return;
alreadyRan = true;
//if the attachment is in a supported format
if (attachment.url.endsWith(".jpg") || attachment.url.endsWith(".jpeg") || attachment.url.endsWith(".png")) {
console.log("running ASCII:image directly");
botCmd.ASCII.image.run(message, {text: attachment.url}, client);
} else {
message.reply(`File: \`${attachment.filename}\`; \`${attachment.filename.split(".").last()}\` is not a supported format`).catch((err) => {
//ignore permission errors
if (err.code === 50013 || err.message === "Missing Permissions") return;
//log the error
console.error(err);
logErr(err);
});
}
});
}
//get args for getCmd() funct
let strNoPrefix = message.content.substring(isCmd.cutfrom); //this could be moved in getName()
let foundCmdName = getName(strNoPrefix);
//if there is no command
if (foundCmdName === undefined) {
if (alreadyRan) return;
message.reply("Please specify a command.\nUse `help` to get a list of all the commands.").catch((err) => {
//ignore permission errors
if (err.code === 50013 || err.message === "Missing Permissions") return;
//log the error
console.error(err);
});
return;
}
//call getCmd() funct
let gotCmd = getCmd(botCmd, foundCmdName);
//give error if command is not found
if (gotCmd.category === "notFound" || gotCmd.command === "notFound") {
if (alreadyRan) return;
message.reply("Error: command not found.\nUse `help` to get a list of all the commands.").catch((err) => {
//ignore permission errors
if (err.code === 50013 || err.message === "Missing Permissions") return;
//log the error
console.error(err);
});
return;
}
//parse arguments
let strNoCmd = strNoPrefix.substring(strNoPrefix.indexOf(foundCmdName) + foundCmdName.length + 1); //to fix: +1 will break if there are more spaces
console.log(`parsing arguments for ${gotCmd.category}:${gotCmd.command}`);
let args = parseOptions(strNoCmd, botCmd[gotCmd.category][gotCmd.command].args);
//call the Cmd
console.log(`running ${gotCmd.category}:${gotCmd.command}`);
//if the command is help it also needs the botCmd obj
if (gotCmd.category === "util" && gotCmd.command === "help") {
botCmd[gotCmd.category][gotCmd.command].run(message, args, client, botCmd);
return;
}
botCmd[gotCmd.category][gotCmd.command].run(message, args, client);
});
function getCmd(botCmd, foundCmdName) {
//for each category
for (var i in botCmd) {
if (botCmd.hasOwnProperty(i)) {
var category = botCmd[i];
//for each command
for (var z in category) {
if (category.hasOwnProperty(z)) {
var command = category[z];
//for each alias
for (var x = 0; x < command.cmdNames.length; x++) {
var currentName = command.cmdNames[x];
if (foundCmdName.toLowerCase() === currentName) {
return {
category: i,
command: z
}
}
}
}
}
}
}
return {
category: "notFound",
command: "notFound"
}
}
function getName(strNoPrefix) {
let split = strNoPrefix.split(" ");
//delete white space
split = split.filter((str) => {
return /\S/.test(str);
});
return split[0];
}
function cmdTest(message, msgContent) {
const cmdRegex = new RegExp(`^(${settings.prefix}|<@!?${client.user.id}>)`, "i");
//if message is from dm it's certainly a command
if (message.channel.type === "dm") {
//if message ha prefix handle like regular command
if (cmdRegex.test(msgContent)) {
let isCmd = cmdRegex.test(msgContent);
let match = cmdRegex.exec(msgContent);
let cutfrom = isCmd ? match.index + match[0].length : undefined;
return {
isCmd: isCmd,
cutfrom: cutfrom
}
//if hasn't got prefix cut from 0 to avoid cutting arguments
} else {
return {
isCmd: true,
cutfrom: 0
}
}
//if message is from server, check if it's command and return
} else {
let isCmd = cmdRegex.test(msgContent);
//bug to fix (status: probably fixed): tries to give values even if there isn't a match
let match = cmdRegex.exec(msgContent);
let cutfrom = isCmd ? match.index + match[0].length : undefined;
return {
isCmd: isCmd,
cutfrom: cutfrom
}
}
}
//adds .last method to array
Array.prototype.last = function () {
return this[this.length - 1];
};