/[sudobot]/branches/8.x/src/commands/automation/QueueAddCommand.ts
ViewVC logotype

Annotation of /branches/8.x/src/commands/automation/QueueAddCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (hide annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 6245 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 /**
2     * This file is part of SudoBot.
3     *
4     * Copyright (C) 2021-2023 OSN Developers.
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 { formatDistanceToNowStrict } from "date-fns";
21     import { EmbedBuilder, Message, PermissionsBitField, escapeMarkdown } from "discord.js";
22     import path from "path";
23     import Command, {
24     BasicCommandContext,
25     CommandMessage,
26     CommandReturn,
27     ValidationRule
28     } from "../../core/Command";
29     import QueueEntry from "../../utils/QueueEntry";
30     import { stringToTimeInterval } from "../../utils/datetime";
31    
32     export default class QueueAddCommand extends Command {
33     public readonly name = "queue__add";
34     public readonly validationRules: ValidationRule[] = [];
35     public readonly aliases = ["queueadd", "addqueue", "createqueue"];
36     public readonly permissions = [PermissionsBitField.Flags.ManageMessages];
37     public readonly description = "Creates a new command queue";
38     public readonly since = "5.57.0";
39     public readonly supportsInteractions: boolean = true;
40     public readonly supportsLegacy: boolean = true;
41    
42     async execute(message: CommandMessage, context: BasicCommandContext): Promise<CommandReturn> {
43     const deferredMessage = await (await this.deferIfInteraction(message))?.fetch();
44     const runAfterString = context.isLegacy
45     ? context.args[0]
46     : context.options.getString("run_after", true);
47    
48     if (!runAfterString) {
49     await this.error(message, "Please specify after how much time the queue should run!");
50     return;
51     }
52    
53     const { error, result: runAfter } = stringToTimeInterval(runAfterString, {
54     milliseconds: true
55     });
56    
57     if (error) {
58     await this.error(message, error);
59     return;
60     }
61    
62     if (context.isLegacy && !context.args[1]) {
63     await this.error(message, "Please specify the command to queue!");
64     return;
65     }
66    
67     const commandString = context.isLegacy
68     ? (message as Message).content
69     .substring(
70     this.client.configManager.config[message.guildId!]?.prefix?.length ?? 1
71     )
72     .trimStart()
73     .substring(context.argv[0] === "queue" ? "queue".length : 0)
74     .trimStart()
75     .substring(
76     context.argv[0] === "queue"
77     ? this.name.replace("queue__", "").length
78     : context.argv[0].length
79     )
80     .trimStart()
81     .substring(runAfterString.length)
82     .trim()
83     : context.options.getString("command", true).trim();
84    
85     let commandName = "",
86     content = "";
87    
88     const prefix = this.client.configManager.config[message.guildId!]?.prefix ?? "-";
89    
90     if (message instanceof Message) {
91     content = message.content;
92     message.content = prefix + commandString;
93     } else {
94     commandName = message.commandName;
95     message.commandName = commandString.split(/ +/)[0];
96    
97     if (
98     this.client.commands.get(message.commandName) &&
99     !this.client.commands.get(message.commandName)?.supportsLegacy
100     ) {
101     message.commandName = commandName;
102     await this.error(
103     message,
104     "This command doesn't support legacy mode, and only legacy commands can be queued!"
105     );
106     return;
107     }
108     }
109    
110     const result = await this.client.commandManager.runCommandFromMessage(
111     message as Message,
112     true,
113     true
114     );
115    
116     if (message instanceof Message) {
117     message.content = content;
118     } else {
119     message.commandName = commandName;
120     }
121    
122     if (result === null) {
123     return;
124     }
125    
126     if (result === false) {
127     await this.error(
128     message,
129     "The command you've specified could not be found. Please check for spelling errors."
130     );
131     return;
132     }
133    
134     const id = await this.client.queueManager.add(
135     new QueueEntry({
136     args: [
137     message.channelId!,
138     message instanceof Message ? message.id : deferredMessage!.id,
139     commandString
140     ],
141     client: this.client,
142     createdAt: new Date(),
143     filePath: path.resolve(__dirname, "../../queues/CommandQueue"),
144     guild: message.guild!,
145     name: "CommandQueue",
146     userId: message.member!.user.id,
147     willRunAt: new Date(Date.now() + runAfter)
148     })
149     );
150    
151     await this.deferredReply(message, {
152     embeds: [
153     new EmbedBuilder({
154     title: "Queue created",
155     color: 0x007bff,
156     description: `${this.emoji(
157     "check"
158     )} Successfully added the queue. The following command will be run after **${formatDistanceToNowStrict(
159     new Date(Date.now() - runAfter)
160     )}**:\n\n\`\`\`${escapeMarkdown(prefix + commandString)}\`\`\``,
161     fields: [
162     {
163     name: "Queue ID",
164     value: id.toString()
165     }
166     ]
167     }).setTimestamp()
168     ]
169     });
170     }
171     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26