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 { EmbedBuilder, PermissionsBitField, User } from "discord.js"; |
21 |
import Command, { |
22 |
ArgumentType, |
23 |
BasicCommandContext, |
24 |
CommandMessage, |
25 |
CommandReturn, |
26 |
ValidationRule |
27 |
} from "../../core/Command"; |
28 |
import { isSystemAdmin } from "../../utils/utils"; |
29 |
import { AFKsCommandScope } from "./AFKsCommand"; |
30 |
|
31 |
export default class AFKRemoveCommand extends Command { |
32 |
public readonly name = "afks__remove"; |
33 |
public readonly validationRules: ValidationRule[] = [ |
34 |
{ |
35 |
types: [ArgumentType.User], |
36 |
name: "user", |
37 |
entity: { |
38 |
notNull: false |
39 |
}, |
40 |
errors: { |
41 |
required: "Please provide a user to remove their AFK!", |
42 |
"type:invalid": "Please provide a valid user!", |
43 |
"entity:null": "Please provide a valid user!" |
44 |
} |
45 |
} |
46 |
]; |
47 |
public readonly permissions = [PermissionsBitField.Flags.ModerateMembers]; |
48 |
public readonly description = "Removes AFK status for a user."; |
49 |
public readonly availableOptions = { |
50 |
"-s, --scope=[everywhere|guild|global]": |
51 |
"Change the scope of this removal [System Admin Only]" |
52 |
}; |
53 |
public readonly aliases = ["afks__delete"]; |
54 |
|
55 |
async execute(message: CommandMessage, context: BasicCommandContext): Promise<CommandReturn> { |
56 |
await this.deferIfInteraction(message); |
57 |
|
58 |
const optionIndex = context.isLegacy |
59 |
? context.args.findIndex(value => value === "-s" || value === "--scope") |
60 |
: -1; |
61 |
|
62 |
if (context.isLegacy && optionIndex !== -1 && !context.args[optionIndex + 1]) { |
63 |
await this.error( |
64 |
message, |
65 |
`Option \`${context.args[optionIndex]}\` requires an argument with one of the following values: \`everywhere\`, \`global\`, \`guild\`.` |
66 |
); |
67 |
return; |
68 |
} |
69 |
|
70 |
const scope = |
71 |
<AFKsCommandScope | null>( |
72 |
(context.isLegacy |
73 |
? optionIndex === -1 |
74 |
? null |
75 |
: context.args[optionIndex + 1] |
76 |
: context.options.getString("scope")) |
77 |
) ?? "guild"; |
78 |
|
79 |
if (scope !== "guild" && !isSystemAdmin(this.client, message.member?.user.id as string)) { |
80 |
await this.error(message, `Only system admins can remove ${scope}-scoped AFK entries.`); |
81 |
return; |
82 |
} |
83 |
|
84 |
const user: User = context.isLegacy |
85 |
? context.parsedNamedArgs.user |
86 |
: context.options.getString("user", true); |
87 |
const result: [guild: boolean, global: boolean] = [false, false]; |
88 |
let mentions = 0; |
89 |
|
90 |
if (scope === "everywhere" || scope === "guild") { |
91 |
const entry = await this.client.afkService.removeAFK( |
92 |
message.guildId!, |
93 |
user.id, |
94 |
true, |
95 |
true |
96 |
); |
97 |
result[0] = !!entry; |
98 |
mentions += entry?.mentions.length ?? 0; |
99 |
} |
100 |
|
101 |
if (scope === "everywhere" || scope === "global") { |
102 |
const entry = await this.client.afkService.removeAFK("global", user.id, true, true); |
103 |
result[1] = !!entry; |
104 |
mentions += entry?.mentions.length ?? 0; |
105 |
} |
106 |
|
107 |
if (!result[0] && !result[1]) { |
108 |
await this.error(message, "That user is not AFK in the given scope."); |
109 |
return; |
110 |
} |
111 |
|
112 |
await this.deferredReply(message, { |
113 |
embeds: [ |
114 |
new EmbedBuilder({ |
115 |
author: { |
116 |
name: user.username, |
117 |
icon_url: user.displayAvatarURL() |
118 |
}, |
119 |
color: 0x007bff, |
120 |
description: `Successfully removed AFK status for this user. They had **${mentions}** mentions total in the given scope.`, |
121 |
fields: [ |
122 |
{ |
123 |
name: "Scope", |
124 |
value: `${scope[0].toUpperCase()}${scope.substring(1)}` |
125 |
} |
126 |
], |
127 |
footer: { |
128 |
text: "Removed" |
129 |
} |
130 |
}).setTimestamp() |
131 |
] |
132 |
}); |
133 |
} |
134 |
} |