|
| 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 | +} |
0 commit comments