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

Contents of /trunk/src/commands/automation/ScheduleCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 344 - (show annotations)
Mon Jul 29 17:29:40 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 3357 byte(s)
chore: eslint autofix
1 import { CommandInteraction, Message, TextChannel } from 'discord.js';
2 import BaseCommand from '../../utils/structures/BaseCommand';
3 import DiscordClient from '../../client/Client';
4 import CommandOptions from '../../types/CommandOptions';
5 import InteractionOptions from '../../types/InteractionOptions';
6 import MessageEmbed from '../../client/MessageEmbed';
7 import ms from 'ms';
8 import { setTimeoutv2 } from '../../utils/setTimeout';
9
10 export default class ScheduleCommand extends BaseCommand {
11 supportsInteractions = true;
12
13 constructor() {
14 super('schedule', 'automation', []);
15 }
16
17 async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
18 if (!options.isInteraction && options.args[1] === undefined) {
19 await msg.reply({
20 embeds: [
21 new MessageEmbed()
22 .setColor('#f14a60')
23 .setDescription(`This command requires at least two arguments.`)
24 ]
25 });
26
27 return;
28 }
29
30 const time = ms(options.isInteraction ? <string> await options.options.getString('time') : options.args[0]);
31
32 if (!time) {
33 await msg.reply({
34 embeds: [
35 new MessageEmbed()
36 .setColor('#f14a60')
37 .setDescription(`Invalid time interval given.`)
38 ]
39 });
40
41 return;
42 }
43
44 let channel: TextChannel = <TextChannel> msg.channel;
45 let text: string;
46
47 if (options.isInteraction) {
48 if (options.options.getChannel('channel')) {
49 channel = <TextChannel> await options.options.getChannel('channel');
50 }
51
52 text = <string> await options.options.getString('content');
53 }
54 else if (msg instanceof Message) {
55 const args = [...options.args];
56 args.shift();
57
58 if (msg.mentions.channels.last()) {
59 channel = await <TextChannel> msg.mentions.channels.last();
60 args.pop();
61 }
62
63 text = args.join(' ');
64 }
65
66 if (!channel.send) {
67 await msg.reply({
68 content: 'Invalid text channel.',
69 ephemeral: true
70 });
71
72 return;
73 }
74
75 try {
76 const timeout = await setTimeoutv2('send', time, msg.guild!.id, `schedule ${time} ${text!} #${channel.name}`, text!, channel.id, msg.guild!.id);
77
78 await msg.reply({
79 embeds: [
80 new MessageEmbed()
81 .setDescription('A queue job has been added.')
82 .setFooter({
83 text: 'ID: ' + timeout.row.id
84 })
85 ],
86 ephemeral: true
87 });
88 }
89 catch(e) {
90 console.log(e);
91
92 await msg.reply({
93 embeds: [
94 new MessageEmbed()
95 .setColor('#f14a60')
96 .setDescription(`I don't have enough permission to send messages on this channel.`)
97 ]
98 });
99
100 return;
101 }
102
103 if (msg instanceof Message) {
104 await msg.react('⏰');
105 }
106 }
107 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26