This repository has been archived by the owner on Sep 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
91 lines (80 loc) · 3.07 KB
/
index.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
const { token, adminChannel, newsChannel, newsInoltrerChannel } = require("./config.json");
const { Client, Collection, Intents } = require("discord.js"); // Require the necessary discord.js classes
const fs = require("fs");
const { baseEmbedGenerator } = require("./tools/baseEmbedFactory.js");
const { rss_sender } = require("./tools/rss_tools.js");
const { formattedDate, saveDebug } = require("./tools/miscelaneous.js");
let channel;
let rss_feed = JSON;
let startupChannel;
// Create a new client instance
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]
});
// Create a collection and then populate it with commands
client.commands = new Collection();
const commandFiles = fs
.readdirSync("./commands")
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
// Set a new item in the Collection with the key as the command name and the value as the exported module
client.commands.set(command.data.name, command);
}
// When the client is ready, run this code (only once)
client.once("ready", async () => {
if (typeof newsChannel !== "undefined" && newsChannel !== "") {
channel = await client.channels.fetch(newsChannel);
}
else {
saveDebug("[WARN] Undefined news channel. Check config.json");
}
if (typeof adminChannel !== "undefined" && adminChannel !== "") {
startupChannel = await client.channels.fetch(adminChannel);
await startupChannel.send("Bot succesfully started!");
}
else {
saveDebug("[WARN] Undefined admin channel. Check config.json");
}
let welcome = formattedDate() + ` Logged in as ${client.user.tag}!` + "\n";
saveDebug(welcome);
});
client.on("messageCreate", async (message) => {
// Utilize this command if you need to restart the bot
if (message.content === "!restartBot" && message.channel == adminChannel) {
await message.reply("Restarting...");
saveDebug(formattedDate() + " Restarting..." + "\n");
client.destroy();
process.exit(0);
}
// Utilize this command if you need to send a news outside the RSS feed
else if (message.channel == newsInoltrerChannel && message.content.startsWith("!news")) {
messaggio = message.content.split("\n");
baseEmbed = baseEmbedGenerator();
baseEmbed.setTitle(messaggio[1]);
baseEmbed.setDescription(messaggio.slice(2).join("\n"));
await channel.send({ embeds: [baseEmbed] });
}
});
client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return; // If command does not exist in the map, it returns null, so we exit early
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true
});
}
});
// Login to Discord with your client's token
client.login(token);
// RSS Feed Parser
if (typeof newsChannel !== "undefined" && newsChannel !== "") {
var intervalId = setInterval(async function () {
rss_feed = await rss_sender(rss_feed, channel);
}, 1800000);
}