-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
81 lines (69 loc) · 2.64 KB
/
app.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
const { Client, Collection, Events, GatewayIntentBits, userMention, bold } = require('discord.js');
const mongoose = require('mongoose');
const fs = require('node:fs');
const path = require('node:path');
const cron = require('node-cron');
const { token } = require('./config.json');
const { slashCommandHandeler } = require('./handelers/slashCommand');
const { modalSubmissionHandeler } = require('./handelers/modalSubmit');
// const { remainderScript } = require('./utils/remainderScript');
const { Remainder } = require('./modules/reminder');
const { DateTime } = require('luxon');
// const { userMention, bold } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMembers ]});
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
client.commands = new Collection();
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
client.commands.set(command.data.name, command);
}
// mongoose connection
let conn_string = 'mongodb://localhost:27017/discord';
mongoose.connect(conn_string)
.then(() => console.log("Database Connected"))
.catch((err) => console.error(err));
client.once(Events.ClientReady, () => {
console.log('Ready!');
});
cron.schedule( '* * * * *', async () => {
try{
let current_time = DateTime.now();
let remainders = await Remainder.find({ date: {
$lt: current_time.ts
} }).lean();
console.log(`got ${remainders.length} to send`);
for( let i=0;i<remainders.length;i++){
// ?if want in channel
// let channel = await client.channels.fetch(remainders[i].channelId);
// await channel.send(`Remainder for ${userMention(remainders[i].userId)}\n${bold('description: ')}${remainders[i].description}`);
// ? personal DM
await client.users.send(remainders[i].userId, `A Remainder:\n> ${remainders[i].description}`);
}
await Remainder.deleteMany({ date: {
$lt: current_time.ts
} });
return;
}
catch(err){
console.error(err);
return;
}
} ); // every minute check and send remainder
// slash command handeler
client.on(Events.InteractionCreate, async interaction => {
let res;
if( interaction.isChatInputCommand() ){
res = await slashCommandHandeler(client, interaction);
}
else if( interaction.isModalSubmit() ){
res = await modalSubmissionHandeler(interaction);
}
else{
return;
}
if(res.status) return;
return interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
});
client.login(token);