-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
152 lines (126 loc) · 4.26 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
const fs = require("fs");
const express = require("express");
const Discord = require("discord.js");
const mongoose = require("mongoose");
var cron = require("node-cron");
var pjson = require("./package.json");
const { prefix, token, db_url } = require("./config.js");
const { reaction } = require("./reaction.js");
var Reminder = require("./models/reminder").Reminder;
// DB connection
mongoose.connect(db_url, { useNewUrlParser: true });
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
// Discord Client
const client = new Discord.Client({
partials: ["MESSAGE", "CHANNEL", "REACTION"],
});
// Load commands
client.commands = new Discord.Collection();
const commandFiles = fs
.readdirSync("./commands")
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
console.log(`${command.name} has been added`);
client.commands.set(command.name, command);
}
// Load reactions
client.reactions = new Discord.Collection();
const reactionFiles = fs
.readdirSync("./reactions")
.filter((file) => file.endsWith(".js"));
for (const file of reactionFiles) {
const reaction = require(`./reactions/${file}`);
console.log(`${reaction.identifier} has been added`);
client.reactions.set(reaction.identifier, reaction);
}
client.once("ready", () => {
console.log("Ready!");
});
client.on("message", (message) => {
if (message.content.startsWith(prefix)) {
const args = message.content.slice(prefix.length).trim().split(" ");
const command = args.shift().toLowerCase();
if (!client.commands.has(command)) {
message.reply(
`command :${command} not found, use help to list the commands!`
);
return;
}
try {
client.commands.get(command).execute(message, args, { client, Discord });
} catch (error) {
console.error(error);
message.reply(`there was an error trying to execute ${command} command!`);
}
} else {
reaction(message);
}
});
client.on("messageReactionAdd", async (reaction, user) => {
// When we receive a reaction we check if the reaction is partial or not
const { name } = reaction.emoji;
if (!client.reactions.has(name)) {
//Early return if not reaction found
// console.log(`${name} reaction not found`);
return;
}
if (reaction.partial) {
// If the message this reaction belongs to was removed the fetching might result in an API error, which we need to handle
try {
console.log(`${name} reaction found`);
await reaction.fetch();
client.reactions.get(name).execute(reaction, { client, Discord });
} catch (error) {
console.log("Something went wrong when fetching the message: ", error);
// Return as `reaction.message.author` may be undefined/null
return;
}
}
});
client.on("shardError", (error) => {
console.error("A websocket connection encountered an error:", error);
});
client.login(token);
////// We need this in order to keep the aplication alive on server
const app = express();
app.get("/", (request, response) => {
console.log(Date.now() + " Ping Received");
response.send({ version: pjson.version });
});
app.listen(process.env.PORT);
console.log(`${Date.now()} Ready on port ${process.env.PORT}`);
////// Schedule for missing reminders
cron.schedule("*/5 * * * *", async () => {
const events = await Reminder.find({
sended: false,
schedule_date: { $lt: new Date() },
});
events.map((event) => {
const randomColor = Math.floor(Math.random() * 16777215).toString(16);
const remindEndMsg = createEmbedMsg(
event.title,
event.message,
event.footer,
randomColor
);
const author = client.users.resolve(event.owner_id);
client.channels.resolve(event.room_id).send(`Reminder for ${author}`);
client.channels.resolve(event.room_id).send(remindEndMsg);
event.sended = true;
event.save(errorOnSave);
console.log("fired reminder");
});
});
const createEmbedMsg = (title, msg, timestamp = "", randomColor) =>
new Discord.MessageEmbed()
.setColor(`#${randomColor}`)
.setTitle(title)
.setDescription(msg)
.setFooter(timestamp);
function errorOnSave(err) {
if (err) {
console.log(`${message.author} Error saving reminder :/`, { err });
}
}