Repository navigation
Expand file tree
/
Copy pathbot.js
More file actions
137 lines (109 loc) · 3.69 KB
/
Copy pathbot.js
File metadata and controls
137 lines (109 loc) · 3.69 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
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
import dotenv from "dotenv";
dotenv.config();
import dayjs from "dayjs";
import RelativeTime from "dayjs/plugin/relativeTime.js";
import "dayjs/locale/fr.js";
dayjs.extend(RelativeTime);
dayjs.locale("fr");
import { Client, Events, GatewayIntentBits, Partials } from "discord.js";
import { Low } from "lowdb";
import { JSONFile } from "lowdb/node";
import { join } from "path";
// listeners imports
import { onMessageUpdate, onGuildMemberRemove } from "./admin/listeners.js";
import {
onInteractionCreate,
onMessageCreate,
onReactionAdd,
onReactionRemove,
onThreadCreate,
} from "./listeners.js";
// admin inits
import { roleInit } from "./admin/role.js";
// commands import
import { initPollsCollector } from "./commands/polls/pollsCollectors.js";
import { initBirthdays } from "./commands/birthday.js";
import { setGiftTimeoutLoop } from "./commands/gift/gift.js";
import { initReminder } from "./commands/reminder.js";
import { slashCommandsInit } from "./commands/slash.js";
// helpers imports
// jsons import
import { COMMONS } from "./classes/commons.js";
// fun imports
import { setActivity, updateActivity } from "./fun.js";
// DB
const file = join("db", "db.json"); // Use JSON file for storage
const adapter = new JSONFile(file);
const db = new Low(adapter, {});
db.read(); // Read data from JSON file, this will set db.data content
db.wasUpdated = false;
setInterval(async () => {
// db updater loop, used to centralize db.write()
if (db.wasUpdated) {
await db.write();
db.wasUpdated = false;
}
}, 10000);
// Discord CLIENT
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildMessageTyping,
GatewayIntentBits.MessageContent,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildModeration,
],
partials: [
Partials.Channel, // Required to receive DMs
Partials.Message, // MESSAGE && REACTION for role handling
Partials.Reaction,
],
});
client.db = db; // db cache
client.remindme = []; // reminders cache
// Create bot startup
client.once(Events.ClientReady, async () => {
// Time variables
const tomorrow = dayjs().add(1, "day").hour(8).minute(0).second(0);
const tomorrowDiff = tomorrow.diff(dayjs()); //diff between tomorrow 8am and now in ms
const frequency = 24 * 60 * 60 * 1000; // 24 hours in ms
// Bot init
console.log("I am ready!");
roleInit(client); //role handler init
//polls
client.voteBuffers = {}; //init poll votes buffer
initPollsCollector(client); //start db polls collectors
//Ewibot activity
setActivity(client);
updateActivity(client);
//slash commands
const server =
process.env.DEBUG === "yes" ? COMMONS.getTest() : COMMONS.getProd();
const guildId = server.guildId;
slashCommandsInit(guildId, client); //commands submit to API
//gift
setGiftTimeoutLoop(client); //gift timeout loop init
//reminders
initReminder(client);
//birthdays
initBirthdays(client, tomorrowDiff, frequency);
});
process.on("unhandledRejection", (error) => {
console.error("Unhandled promise rejection:", error);
});
// Create an event listener for messages
client.on(Events.MessageCreate, onMessageCreate);
client.on(Events.MessageReactionAdd, onReactionAdd);
client.on(Events.MessageReactionRemove, onReactionRemove);
// listener to autojoin threads
client.on(Events.ThreadCreate, onThreadCreate);
// listener for buttons/modals
client.on(Events.InteractionCreate, onInteractionCreate);
// listeners for FUN
client.on(Events.MessageUpdate, onMessageUpdate);
client.on(Events.GuildMemberRemove, onGuildMemberRemove);
// Log the bot in
client.login(process.env.TOKEN);