-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
218 lines (185 loc) · 7.14 KB
/
bot.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
/**
Saihttam, Matthias's Discord bot.
**/
// Global dependencies
const passwords = require('./passwords.json');
// const mysql = require('mysql');
const Discord = require('discord.js');
const Client = new Discord.Client();
// I have a MySql server set up for Saihttam, but it's not currently being used
/*const con = mysql.createConnection({
host: "localhost",
user: "SaihttamBot",
password: passwords.mysql,
database: "saihttambot",
});
con.connect();
*/
const prefix = "./";
var runOnMessage = [];
function addReacts (message, codePoint, numReacts) {
if (codePoint - 127462 >= numReacts) {
return;
} else {
message.react(String.fromCodePoint(codePoint)).then(() => {
addReacts(message, codePoint + 1, numReacts);
});
}
}
var commands = [
{
command: ["say", "echo"],
func: function say (message, content) {
message.channel.send(content);
},
args: "<message>",
help: "Repeats back your message"
},
{
command: ["help"],
func: function help(message) {
const helpEmbed = new Discord.RichEmbed({
author: {
name: "Saihttam Help",
icon_url: "https://cdn.discordapp.com/avatars/251143910347243531/6613b6866204a95c82781c528da8bf99.jpg?size=64"
},
color: 0x377735
});
for (let {command: commandArr, args: arguments, help: description, hidden: hidden} of commands) {
if (hidden) continue;
helpEmbed.addField(prefix + commandArr[0] + (arguments ? " " + arguments : ""), description, true);
}
helpEmbed.addField("\u200B", "*Saihttam is coded and hosted by <@226887818364846082>, and is open source [on Github](https://github.com/MatthiasSaihttam/SaihttamBot).*");
message.channel.send(helpEmbed);
},
hidden: true
},
{
command: ["math"],
func: function math(message, content) {
message.channel.send(new Function("return " + (content.replace(/[^0-9+\/\-()*]/g, "")))());
},
args: "<expression>",
help: "Evaluates *expression*"
},
{
command: ["poll", "reactions"],
func: function poll(message, content) {
const args = content.split(" ");
const numReactions = parseInt(args[0] || "2", 10);
if (Number.isNaN(numReactions)) {
message.channel.send("Error. Invalid number.");
return;
}
message.channel.fetchMessages({ limit: 2 }).then(messages => messages.entries()).then(messages => {
messages.next(); //Not the last message (the ./poll)
const reactMessage = messages.next().value[1]; //But the one before that
if (reactMessage.author !== message.author) {
message.channel.send("Use on your own message. (Right after you've sent a message)");
return;
}
if (numReactions === 2) {
reactMessage.react("\u{1f44d}").then(() => {
reactMessage.react("\u{1f44e}").catch(console.error);;
}).catch(console.error);
}else {
addReacts(reactMessage, 127462, numReactions);
}
})
},
args: "[count]",
help: "Adds *count* reactions to the previous message (so people can vote)"
},
{
command: ["eval"],
func: function (message) {
if (message.author.id !== "226887818364846082") {
message.channel.send("You don't have permission to use this.");
return;
}
try {
var code = message.content.replace(/^\.\/eval\s*(?:-[a-zA-Z]+)?\s*(?:(?:```\w*[\r\n])|`)?([\s\S]*?)`{0,3}$/, "$1");
var output = eval(code);
//If -s flag is added, message is silent and doesn't send a reply automatically.
var toOutput = !(message.content.match(/^\.\/eval\s*-s/));
if (toOutput) {
if (output instanceof Promise) {
output.then(a => message.channel.send("```" + a + "```")).catch(e => {
message.channel.send("Failed with error\n```" + e + "```\nFull data printed to console.");
console.log(e);
});
}else {
message.channel.send("```" + output + "```");
}
}
}catch(e) {
message.channel.send("```" + e + "```");
}
},
hidden: true,
args: "[-s] <command>",
help: "eval (for)"
},
];
function verifyCarets (message) {
//Verify carets
if (message.channel.name === "caret") {
var content = message.content
.replace(/```(?:\w+\n)?([\s\S]+?)```/g, "$1")
.replace(/`([\s\S]+?)`/g, "$1")
.replace(/_([\s\S]+?)_/g, "$1")
.replace(/\*([\s\S]+?)\*/g, "$1")
.replace(/~~([\s\S]+?)~~/g, "$1")
if (content.match(/[^^\uFF3E\s]/) || message.attachments.size || message.embeds.length || message.mentions.length) {
message.delete();
return false;
}
return true;
}
}
Client.on('message', (message) => {
try {
for (var i = 0; i < runOnMessage.length; i++) {
try {
runOnMessage[i](message);
}catch (e) {
message.channel.send("```" + e + "```");
message.channel.send("Run on message function failed. Removing it to save you.");
runOnMessage.splice(i, 1);
i--;
}
}
const content = message.content;
verifyCarets(message);
if (content.startsWith(prefix)) {
//Lowercase, starting past the prefix, grab until the first space or to the end of the string
const commandName = content.toLowerCase().substring(prefix.length, (content.indexOf(" ") +1 || content.length +1) -1)
const command = commands.find(c => c.command.indexOf(commandName) > -1);
if (command) {
//Call the command, remove the prefix, command name, and space
command.func(message, content.slice((prefix + commandName).length + 1))
}else {
message.channel.send("It looks like you may have been trying to use a command. Try `./help`.");
}
}
} catch (e) {
message.channel.send('Error: ```javascript\n' + e + '```')
}
})
Client.login(passwords.discord).catch(console.error);
Client.on('ready', () => {
console.log("Connected to Discord");
Client.user.setActivity(`for ${prefix}help`, { type: "WATCHING" });
// Client.user.setAvatar('./images/image.png')
})
Client.on('messageUpdate', (oldMess, newMess) => {
verifyCarets(newMess);
})
function exitHandler(options, err) {
Client.destroy();
// con.end();
process.exit();
}
process.on('SIGINT', exitHandler);
process.on('exit', exitHandler);
process.on('uncaughtException', exitHandler);