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 { setTimeoutv2 } from '../../utils/setTimeout'; |
28 |
import ScheduleMessageQueue from '../../queues/ScheduleMessageQueue'; |
29 |
import ExpireScheduleMessageQueue from '../../queues/ExpireScheduleMessageQueue'; |
30 |
|
31 |
export default class ExpireScheduleCommand extends BaseCommand { |
32 |
supportsInteractions = true; |
33 |
|
34 |
constructor() { |
35 |
super('expiresc', 'automation', []); |
36 |
} |
37 |
|
38 |
async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
39 |
if (!options.isInteraction && options.args[2] === undefined) { |
40 |
await msg.reply({ |
41 |
embeds: [ |
42 |
new MessageEmbed() |
43 |
.setColor('#f14a60') |
44 |
.setDescription(`This command requires at least three arguments.`) |
45 |
] |
46 |
}); |
47 |
|
48 |
return; |
49 |
} |
50 |
|
51 |
const time1 = ms(options.isInteraction ? <string> await options.options.getString('send-after') : options.args[0]); |
52 |
const time2 = ms(options.isInteraction ? <string> await options.options.getString('delete-after') : options.args[1]); |
53 |
|
54 |
if (!time1) { |
55 |
await msg.reply({ |
56 |
embeds: [ |
57 |
new MessageEmbed() |
58 |
.setColor('#f14a60') |
59 |
.setDescription(`Invalid time interval given (send-after).`) |
60 |
] |
61 |
}); |
62 |
|
63 |
return; |
64 |
} |
65 |
|
66 |
if (!time2) { |
67 |
await msg.reply({ |
68 |
embeds: [ |
69 |
new MessageEmbed() |
70 |
.setColor('#f14a60') |
71 |
.setDescription(`Invalid time interval given (delete-after).`) |
72 |
] |
73 |
}); |
74 |
|
75 |
return; |
76 |
} |
77 |
|
78 |
let channel: TextChannel = <TextChannel> msg.channel; |
79 |
let text: string = ''; |
80 |
|
81 |
if (options.isInteraction) { |
82 |
if (options.options.getChannel('channel')) { |
83 |
channel = <TextChannel> await options.options.getChannel('channel'); |
84 |
} |
85 |
|
86 |
text = <string> await options.options.getString('content'); |
87 |
} |
88 |
else if (msg instanceof Message) { |
89 |
const args = [...options.args]; |
90 |
args.shift(); |
91 |
args.shift(); |
92 |
|
93 |
if (msg.mentions.channels.last()) { |
94 |
channel = await <TextChannel> msg.mentions.channels.last(); |
95 |
args.pop(); |
96 |
} |
97 |
|
98 |
text = args.join(' '); |
99 |
} |
100 |
|
101 |
if (!channel.send) { |
102 |
await msg.reply({ |
103 |
content: 'Invalid text channel.', |
104 |
ephemeral: true |
105 |
}); |
106 |
|
107 |
return; |
108 |
} |
109 |
|
110 |
try { |
111 |
// const timeout = await setTimeoutv2('send-expire', time1, msg.guild!.id, `expiresc ${time1} ${time2} ${text!} #${channel.name}`, text!, channel.id, msg.guild!.id, time2); |
112 |
|
113 |
const { id } = await client.queueManager.addQueue(ExpireScheduleMessageQueue, { |
114 |
data: { |
115 |
messageID: msg.id, |
116 |
guildID: msg.guild!.id, |
117 |
channelID: msg.channel!.id, |
118 |
content: text, |
119 |
expires: time2 |
120 |
}, |
121 |
runAt: new Date(Date.now() + time1), |
122 |
guild: msg.guild!.id, |
123 |
}); |
124 |
|
125 |
await msg.reply({ |
126 |
embeds: [ |
127 |
new MessageEmbed() |
128 |
.setDescription('A queue job has been added.') |
129 |
.setFooter({ |
130 |
text: 'ID: ' + id // timeout.row.id |
131 |
}) |
132 |
], |
133 |
ephemeral: true |
134 |
}); |
135 |
} |
136 |
catch(e) { |
137 |
console.log(e); |
138 |
|
139 |
await msg.reply({ |
140 |
embeds: [ |
141 |
new MessageEmbed() |
142 |
.setColor('#f14a60') |
143 |
.setDescription(`I don't have enough permission to send messages on this channel.`) |
144 |
] |
145 |
}); |
146 |
|
147 |
return; |
148 |
} |
149 |
|
150 |
if (msg instanceof Message) { |
151 |
await msg.react('⏰'); |
152 |
} |
153 |
} |
154 |
} |