1 |
/** |
2 |
* This file is part of SudoBot. |
3 |
* |
4 |
* Copyright (C) 2021-2022 OSN Inc. |
5 |
* |
6 |
* SudoBot is free software; you can redistribute it and/or modify it |
7 |
* under the terms of the GNU Affero General Public License as published by |
8 |
* the Free Software Foundation, either version 3 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* SudoBot is distributed in the hope that it will be useful, but |
12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU Affero General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU Affero General Public License |
17 |
* along with SudoBot. If not, see <https://www.gnu.org/licenses/>. |
18 |
*/ |
19 |
|
20 |
import { CommandInteraction, Message, TextChannel } from 'discord.js'; |
21 |
import BaseCommand from '../../utils/structures/BaseCommand'; |
22 |
import DiscordClient from '../../client/Client'; |
23 |
import CommandOptions from '../../types/CommandOptions'; |
24 |
import InteractionOptions from '../../types/InteractionOptions'; |
25 |
import MessageEmbed from '../../client/MessageEmbed'; |
26 |
import ms from 'ms'; |
27 |
import ScheduleMessageQueue from '../../queues/ScheduleMessageQueue'; |
28 |
|
29 |
export default class ScheduleCommand extends BaseCommand { |
30 |
supportsInteractions = true; |
31 |
|
32 |
constructor() { |
33 |
super('schedule', 'automation', []); |
34 |
} |
35 |
|
36 |
async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
37 |
if (!options.isInteraction && options.args[1] === undefined) { |
38 |
await msg.reply({ |
39 |
embeds: [ |
40 |
new MessageEmbed() |
41 |
.setColor('#f14a60') |
42 |
.setDescription(`This command requires at least two arguments.`) |
43 |
] |
44 |
}); |
45 |
|
46 |
return; |
47 |
} |
48 |
|
49 |
const time = ms(options.isInteraction ? <string> await options.options.getString('time') : options.args[0]); |
50 |
|
51 |
if (!time) { |
52 |
await msg.reply({ |
53 |
embeds: [ |
54 |
new MessageEmbed() |
55 |
.setColor('#f14a60') |
56 |
.setDescription(`Invalid time interval given.`) |
57 |
] |
58 |
}); |
59 |
|
60 |
return; |
61 |
} |
62 |
|
63 |
let channel: TextChannel = <TextChannel> msg.channel; |
64 |
let text: string = ''; |
65 |
|
66 |
if (options.isInteraction) { |
67 |
if (options.options.getChannel('channel')) { |
68 |
channel = <TextChannel> await options.options.getChannel('channel'); |
69 |
} |
70 |
|
71 |
text = <string> await options.options.getString('content'); |
72 |
} |
73 |
else if (msg instanceof Message) { |
74 |
const args = [...options.args]; |
75 |
args.shift(); |
76 |
|
77 |
if (msg.mentions.channels.last()) { |
78 |
channel = await <TextChannel> msg.mentions.channels.last(); |
79 |
args.pop(); |
80 |
} |
81 |
|
82 |
text = args.join(' '); |
83 |
} |
84 |
|
85 |
if (!channel.send) { |
86 |
await msg.reply({ |
87 |
content: 'Invalid text channel.', |
88 |
ephemeral: true |
89 |
}); |
90 |
|
91 |
return; |
92 |
} |
93 |
|
94 |
try { |
95 |
// const timeout = await setTimeoutv2('send', time, msg.guild!.id, `schedule ${time} ${text!} #${channel.name}`, text!, channel.id, msg.guild!.id); |
96 |
|
97 |
const { id } = await client.queueManager.addQueue(ScheduleMessageQueue, { |
98 |
data: { |
99 |
guildID: msg.guild!.id, |
100 |
channelID: msg.channel!.id, |
101 |
content: text |
102 |
}, |
103 |
runAt: new Date(Date.now() + time), |
104 |
guild: msg.guild!.id, |
105 |
}); |
106 |
|
107 |
await msg.reply({ |
108 |
embeds: [ |
109 |
new MessageEmbed() |
110 |
.setDescription('A queue job has been added.') |
111 |
.setFooter({ |
112 |
text: 'ID: ' + id // timeout.row.id |
113 |
}) |
114 |
], |
115 |
ephemeral: true |
116 |
}); |
117 |
} |
118 |
catch(e) { |
119 |
console.log(e); |
120 |
|
121 |
await msg.reply({ |
122 |
embeds: [ |
123 |
new MessageEmbed() |
124 |
.setColor('#f14a60') |
125 |
.setDescription(`I don't have enough permission to send messages on this channel.`) |
126 |
] |
127 |
}); |
128 |
|
129 |
return; |
130 |
} |
131 |
|
132 |
if (msg instanceof Message) { |
133 |
await msg.react('⏰'); |
134 |
} |
135 |
} |
136 |
} |