-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbot.js
165 lines (141 loc) · 4.75 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
require("dotenv").config();
const Discord = require("discord.js");
const getFiles = require("./getFiles");
const remind = require("./remind");
const verify = require("./verify");
const tleHandler = require("./utils/TLE");
const getPrefix = require("./utils/db/getCommandPrefix");
const reactionHandler = require("./utils/reactionHandler");
const user = require("./utils/db/discordMemberHandlers");
const { addRole } = require("./utils/guildMemberHandlers");
const bot = new Discord.Client({
partials: ["MESSAGE", "CHANNEL", "REACTION"],
});
bot.commands = new Discord.Collection();
bot.on("guildMemberAdd", async (member) => {
try {
if (member.user.bot === true) return;
const channel = member.guild.channels.cache.find(
(ch) => ch.id === process.env.WELCOME_CHANNEL_ID
);
if (!channel) return;
const prefix = await getPrefix();
let name = member.user.username;
let welcomeEmbed = new Discord.MessageEmbed()
.setColor("#176ffc")
.setTitle(`Yay! ${name} you made it to KPH Discord Server `)
.setDescription(
`
I am KLE, a bot specially designed for this server!
Head to <#${process.env.RULES_CHANNEL_ID}> to get the Member role.
`
)
.setFooter(`Use ${prefix}help command to know more about me.`);
channel.send(welcomeEmbed);
const userObj = await user.existsInUsers(member.id);
if (userObj !== null) {
await user.updateIsMember(member.id, true);
if (userObj.batch !== undefined) {
addRole(bot, member.id, "JIITian");
addRole(bot, member.id, userObj.batch);
}
}
} catch (error) {
bot.channels.cache.get(process.env.ERROR_LOG_CHANNEL).send(error.stack);
}
});
bot.on("guildMemberRemove", async (member) => {
const userObj = await user.existsInUsers(member.id);
if (userObj !== null) await user.updateIsMember(member.id, false);
});
bot.on("ready", () => {
getFiles("./commands")
.then((files) => {
for (const file of files) {
const command = require(file);
bot.commands.set(command.name, command); // subcommands are included
}
})
.catch((error) => {
bot.channels.cache.get(process.env.ERROR_LOG_CHANNEL).send(error.stack);
});
console.log("The KLE bot is online!");
});
setInterval(async () => {
try {
await remind(bot);
} catch (error) {
bot.channels.cache.get(process.env.ERROR_LOG_CHANNEL).send(error.stack);
}
}, 3000000);
bot.on("message", async (message) => {
try {
if (message.channel.type === "dm") return;
if (message.author.id === process.env.TLE_ID) {
tleHandler(message);
return;
}
const prefix = await getPrefix();
if (!message.content.startsWith(prefix)) return;
const args = message.content
.slice(prefix.length)
.trim()
.split(/\r\n|\r|\n| +/); // removes any whitespace in the message
const command = args.shift().toLowerCase();
if (message.channel.id === process.env.VERIFY_CHANNEL_ID) {
if (command === "verify") {
try {
await verify(bot, message.author, prefix);
} catch (error) {
bot.channels.cache
.get(process.env.ERROR_LOG_CHANNEL)
.send(error.stack);
}
}
if (command !== "clearchannel") return;
}
// command is not present or it is a subcommand
if (
!bot.commands.has(command) ||
bot.commands.get(command).parentName !== undefined
) {
bot.commands.get("invalid").execute(bot, message, args, prefix);
return;
}
// execute the command
try {
await bot.commands.get(command).execute(bot, message, args, prefix);
} catch (error) {
bot.channels.cache.get(process.env.ERROR_LOG_CHANNEL).send(error.stack);
message.reply("There was some error in executing that command! 🙁");
}
} catch (error) {
bot.channels.cache.get(process.env.ERROR_LOG_CHANNEL).send(error.stack);
}
});
bot.on("messageReactionAdd", async (reaction, discordUser) => {
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
try {
await reactionHandler(bot, reaction, discordUser, true);
} catch (error) {
bot.channels.cache.get(process.env.ERROR_LOG_CHANNEL).send(error.stack);
}
});
bot.on("messageReactionRemove", async (reaction, discordUser) => {
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
try {
await reactionHandler(bot, reaction, discordUser, false);
} catch (error) {
bot.channels.cache.get(process.env.ERROR_LOG_CHANNEL).send(error.stack);
}
});
bot.login(process.env.BOT_TOKEN);
// web server
const http = require("http");
const server = http.createServer((req, res) => {
res.writeHead(200);
res.end("ok");
});
server.listen(3000);