Skip to content

Commit baa1d9e

Browse files
Add scheduling messages to github
1 parent c09b6d1 commit baa1d9e

File tree

6 files changed

+164
-0
lines changed

6 files changed

+164
-0
lines changed

83-Scheduling-Messages/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.env
2+
node_modules
Binary file not shown.
+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
const momentTimezone = require('moment-timezone')
2+
const { MessageCollector } = require('discord.js')
3+
4+
const scheduledSchema = require('../models/scheduled-schema')
5+
6+
module.exports = {
7+
requiredPermissions: ['ADMINISTRATOR'],
8+
expectedArgs: '<Channel tag> <YYYY/MM/DD> <HH:MM> <"AM" or "PM"> <Timezone>',
9+
minArgs: 5,
10+
maxArgs: 5,
11+
init: (client) => {
12+
const checkForPosts = async () => {
13+
const query = {
14+
date: {
15+
$lte: Date.now(),
16+
},
17+
}
18+
19+
const results = await scheduledSchema.find(query)
20+
21+
for (const post of results) {
22+
const { guildId, channelId, content } = post
23+
24+
const guild = await client.guilds.fetch(guildId)
25+
if (!guild) {
26+
continue
27+
}
28+
29+
const channel = guild.channels.cache.get(channelId)
30+
if (!channel) {
31+
continue
32+
}
33+
34+
channel.send(content)
35+
}
36+
37+
await scheduledSchema.deleteMany(query)
38+
39+
setTimeout(checkForPosts, 1000 * 10)
40+
}
41+
42+
checkForPosts()
43+
},
44+
callback: async ({ message, args }) => {
45+
const { mentions, guild, channel } = message
46+
47+
const targetChannel = mentions.channels.first()
48+
if (!targetChannel) {
49+
message.reply('Please tag a channel to send your message in.')
50+
return
51+
}
52+
53+
// Remve the channel tag from the args array
54+
args.shift()
55+
56+
const [date, time, clockType, timeZone] = args
57+
58+
if (clockType !== 'AM' && clockType !== 'PM') {
59+
message.reply(
60+
`You must provide either "AM" or "PM", you provided "${clockType}"`
61+
)
62+
return
63+
}
64+
65+
const validTimeZones = momentTimezone.tz.names()
66+
if (!validTimeZones.includes(timeZone)) {
67+
message.reply(
68+
'Unknown timezone! Please use one of the following: <https://gist.github.com/AlexzanderFlores/d511a7c7e97b4c3ae60cb6e562f78300>'
69+
)
70+
return
71+
}
72+
73+
const targetDate = momentTimezone.tz(
74+
`${date} ${time} ${clockType}`,
75+
'YYYY-MM-DD HH:mm A',
76+
timeZone
77+
)
78+
79+
message.reply('Please send the message you would like to schedule.')
80+
81+
const filter = (newMessage) => {
82+
return newMessage.author.id === message.author.id
83+
}
84+
85+
const collector = new MessageCollector(channel, filter, {
86+
max: 1,
87+
time: 1000 * 60, // 60 seconds
88+
})
89+
90+
collector.on('end', async (collected) => {
91+
const collectedMessage = collected.first()
92+
93+
if (!collectedMessage) {
94+
message.reply('You did not reply in time.')
95+
return
96+
}
97+
98+
message.reply('Your message has been scheduled.')
99+
100+
await new scheduledSchema({
101+
date: targetDate.valueOf(),
102+
content: collectedMessage.content,
103+
guildId: guild.id,
104+
channelId: targetChannel.id,
105+
}).save()
106+
})
107+
},
108+
}

83-Scheduling-Messages/index.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const DiscordJS = require('discord.js')
2+
const WOKCommands = require('wokcommands')
3+
require('dotenv').config()
4+
5+
const client = new DiscordJS.Client()
6+
7+
client.on('ready', () => {
8+
new WOKCommands(client, {
9+
commandsDir: 'commands',
10+
showWarns: false,
11+
}).setMongoPath(process.env.MONGO_URI)
12+
})
13+
14+
client.login(process.env.TOKEN)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const mongoose = require('mongoose')
2+
3+
const reqString = {
4+
type: String,
5+
required: true,
6+
}
7+
8+
const scheduledSchema = new mongoose.Schema({
9+
date: {
10+
type: Date,
11+
required: true,
12+
},
13+
content: reqString,
14+
guildId: reqString,
15+
channelId: reqString,
16+
})
17+
18+
const name = 'scheduled-posts-tutorial'
19+
20+
module.exports =
21+
mongoose.model[name] || mongoose.model(name, scheduledSchema, name)

83-Scheduling-Messages/package.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "83-Scheduling-Messages",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"discord.js": "^12.5.1",
14+
"dotenv": "^8.2.0",
15+
"moment-timezone": "^0.5.33",
16+
"mongoose": "^5.12.0",
17+
"wokcommands": "^1.2.8"
18+
}
19+
}

0 commit comments

Comments
 (0)