/[sudobot]/trunk/src/commands/automation/ExpireScheduleCommand.ts
ViewVC logotype

Annotation of /trunk/src/commands/automation/ExpireScheduleCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26