-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
80 lines (73 loc) · 2.19 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
const Discord = require("discord.js");
require("dotenv").config();
const fs = require("fs");
const path = require("path");
const prefix = "$";
const pathName = path.join(__dirname + "/subjects/");
const client = new Discord.Client({
intents: [Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES],
});
client.on("ready", () => {
let name = client.user.tag;
console.log(pathName + name + ".txt");
console.log(name.substring(0, name.indexOf("#")) + " is ready to go!");
});
client.on("messageCreate", async (msg) => {
// if author bot, don't send any message
if (msg.author.bot) return;
// if msg starts with prefix, store commans in arry
if (msg.content.startsWith(prefix)) {
const [commandName, ...args] = msg.content.trim().substring().split(/\s+/);
switch (commandName) {
case "$coa":
sendDocuments("coa", args, msg);
break;
case "$ps":
sendDocuments("ps", args, msg);
break;
case "$np":
sendDocuments("np", args, msg);
break;
case "$ml":
sendDocuments("ml", args, msg);
break;
case "$se":
sendDocuments("se", args, msg);
break;
case "$ele":
sendDocuments("ele", args, msg);
break;
case "$commands":
sendDocuments("default", args, msg);
break;
default:
sendDocuments("default", args, msg);
}
}
});
function sendDocuments(name, args, msg) {
if (args == "") {
// fetchsubject(name);
fs.readFile(pathName + name + ".txt", (err, data) => {
if (err) throw err;
let out = data.toString();
out = out.replace(/\{|}|\[|]/g, "");
msg.reply(out);
});
} else {
if (args == "syllabus") {
fs.readFile(pathName + name + ".txt", (err, data) => {
if (err) throw err;
let out = data.toString();
msg.reply(out.substring(out.indexOf("[") + 1, out.indexOf("]")));
});
} else if (args == "material") {
fs.readFile(pathName + name + ".txt", (err, data) => {
if (err) throw err;
let out = data.toString();
msg.reply(out.substring(out.indexOf("{") + 1, out.indexOf("}")));
});
}
}
}
client.login(process.env.TOKEN);