-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
107 lines (90 loc) · 3.41 KB
/
app.js
File metadata and controls
107 lines (90 loc) · 3.41 KB
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
const Discord = require('discord.js');
const cron = require("node-cron");
const client = new Discord.Client({
intents: [Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES, Discord.Intents.FLAGS.GUILD_MESSAGE_REACTIONS, Discord.Intents.FLAGS.GUILD_MEMBERS],
partials: ['MESSAGE', 'CHANNEL', 'REACTION', 'MEMBERS']
});
const delimiter = process.env.DELIMITER;
const channelID = process.env.CHANNELID;
const readingClub = require("./controllers/readingClub")
// MONGOnpm
const mongoose = require("mongoose");
const mongoURL = process.env.MONGO_CONNECT;
const connectionParams = {
useNewUrlParser: true,
useUnifiedTopology: true
}
mongoose.connect(mongoURL, connectionParams)
.then(() => {
console.log('Connected to database ')
})
.catch((err) => {
console.error(`Error connecting to the database. \n${err}`);
});
client.on("ready", function () {
console.log("Mon BOT est Connecté");
});
client.on("messageCreate", function (message) {
if (message.channel.id === channelID && message.content.charAt(0) === delimiter) {
processRequest(message)
}
});
client.login(process.env.BOT_LOGIN);
// Tirage du livre et du film mensuel
cron.schedule('0 18 1 * *', async function () {
await showBookOTM(await readingClub.pickBookOTM(), true);
await showMovieOTM(await readingClub.pickMovieOTM(), true);
})
async function showMovieOTM(movie, first) {
const embedBook = new Discord.MessageEmbed()
.setColor('#852994')
.setTitle("Livre du mois : " + movie.title)
.setDescription("" + movie.description)
.setFooter({text: movie.runtime + "min"});
const readingChannel = await client.channels.cache.get(channelID).fetch(true);
if (first)
readingChannel.send("everyone");
readingChannel.send({embeds: [embedBook]});
}
async function showBookOTM(book, first) {
const pages = book.pages ? book.pages : "X";
const embedBook = new Discord.MessageEmbed()
.setColor('#852994')
.setTitle("Livre du mois : " + book.title)
.setImage(book.image)
.setDescription("" + book.link)
.setFooter({text: pages + " pages - " + book.categories});
const readingChannel = await client.channels.cache.get(channelID).fetch(true);
if (first)
readingChannel.send("everyone");
readingChannel.send({embeds: [embedBook]});
}
async function addBook(book) {
const pages = book.pages ? book.pages : "X";
const embedBook = new Discord.MessageEmbed()
.setColor('#4aa827')
.setTitle("Ajout : " + book.title)
.setImage(book.image)
.setDescription("" + book.link)
.setFooter({text: pages + " pages - " + book.categories});
const readingChannel = await client.channels.cache.get(channelID).fetch(true);
readingChannel.send({embeds: [embedBook]});
}
async function processRequest(message) {
const request = message.content.substring(1, message.content.length);
const words = request.split(" ");
switch (words[0]) {
case "add":
try {
const isbn = words.length > 1 ? words[1] : null;
await addBook(await readingClub.add(message.author, isbn))
} catch (error) {
console.error(error)
}
break;
case "review":
case "current":
console.log("current")
await showBookOTM(await readingClub.getBookOTM(), false);
}
}