1 |
rakinar2 |
577 |
/** |
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 { setTimeoutv2 } from '../../utils/setTimeout'; |
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 |
|
|
await msg.reply({ |
98 |
|
|
embeds: [ |
99 |
|
|
new MessageEmbed() |
100 |
|
|
.setDescription('A queue job has been added.') |
101 |
|
|
.setFooter({ |
102 |
|
|
text: 'ID: ' + timeout.row.id |
103 |
|
|
}) |
104 |
|
|
], |
105 |
|
|
ephemeral: true |
106 |
|
|
}); |
107 |
|
|
} |
108 |
|
|
catch(e) { |
109 |
|
|
console.log(e); |
110 |
|
|
|
111 |
|
|
await msg.reply({ |
112 |
|
|
embeds: [ |
113 |
|
|
new MessageEmbed() |
114 |
|
|
.setColor('#f14a60') |
115 |
|
|
.setDescription(`I don't have enough permission to send messages on this channel.`) |
116 |
|
|
] |
117 |
|
|
}); |
118 |
|
|
|
119 |
|
|
return; |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
if (msg instanceof Message) { |
123 |
|
|
await msg.react('⏰'); |
124 |
|
|
} |
125 |
|
|
} |
126 |
|
|
} |