/[sudobot]/branches/7.x/src/commands/automation/AFKCommand.ts
ViewVC logotype

Contents of /branches/7.x/src/commands/automation/AFKCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (show annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 4508 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 /**
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 {
21 ChatInputCommandInteraction,
22 GuildMember,
23 Message,
24 SlashCommandBuilder,
25 User,
26 escapeMarkdown,
27 messageLink
28 } from "discord.js";
29 import Command, { ArgumentType, BasicCommandContext, CommandMessage, CommandReturn, ValidationRule } from "../../core/Command";
30
31 export default class AFKCommand extends Command {
32 public readonly name = "afk";
33 public readonly validationRules: ValidationRule[] = [
34 {
35 types: [ArgumentType.StringRest],
36 name: "reason",
37 optional: true,
38 errors: {
39 "type:invalid": "Please provide a usable/valid reason!"
40 }
41 }
42 ];
43 public readonly permissions = [];
44 public readonly aliases = ["gafk"];
45 public readonly description = "Sets your AFK status, and tells others that you're away.";
46 public readonly slashCommandBuilder = new SlashCommandBuilder()
47 .addStringOption(option => option.setName("reason").setDescription("The reason of you being AFK"))
48 .addBooleanOption(option => option.setName("global").setDescription("Globally set your AFK status. Defaults to false"));
49
50 async execute(message: CommandMessage, context: BasicCommandContext): Promise<CommandReturn> {
51 const { id } = (await this.deferIfInteraction(message, { fetchReply: true })) ?? {};
52
53 const reason: string | undefined =
54 (context.isLegacy ? context.parsedNamedArgs.reason : context.options.getString("reason")) ?? undefined;
55 const global = (context.isLegacy ? context.argv[0] === "gafk" : context.options.getBoolean("global")) ?? false;
56
57 if (message instanceof ChatInputCommandInteraction && reason) {
58 const newMessage = {
59 ...message,
60 id,
61 author: message.member!.user as User,
62 member: message.member! as GuildMember,
63 guild: message.guild!,
64 deletable: true,
65 url: messageLink(message.channelId!, id ?? "123", message.guildId!),
66 delete: async () => {
67 console.log("Done");
68 await this.error(message, "Your AFK status is blocked by the message rules configured in the server.");
69 },
70 content: reason
71 } as unknown as Message;
72
73 let deleted: boolean | undefined = await this.client.messageRuleService.onMessageCreate(newMessage);
74
75 console.log("Rules", deleted);
76
77 if (deleted) {
78 return;
79 }
80
81 deleted = await this.client.messageFilter.onMessageCreate(newMessage);
82 console.log("Message Filter", deleted);
83
84 if (deleted) {
85 return;
86 }
87 }
88
89 const isAFK = this.client.afkService.isAFK(message.guildId!, message.member!.user.id);
90
91 if (isAFK && !!this.client.afkService.get(`global_${message.member!.user.id}`) !== global) {
92 if (message instanceof ChatInputCommandInteraction) {
93 await this.success(message, "Successfully removed your AFK status.");
94 }
95
96 return;
97 }
98
99 await this.client.afkService.startAFK(message.guildId!, message.member!.user.id, reason, global);
100
101 await this.deferredReply(message, {
102 embeds: [
103 {
104 color: 0x007bff,
105 description: `You're${global ? " globally" : ""} AFK now${
106 reason ? `, for reason: **${escapeMarkdown(reason)}**` : ""
107 }.`
108 }
109 ],
110 allowedMentions: {
111 users: [],
112 repliedUser: true,
113 roles: []
114 }
115 });
116 }
117 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26