1 |
rakinar2 |
577 |
/** |
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 |
|
|
import { CommandInteraction, Message, Permissions, Util } from "discord.js"; |
21 |
|
|
import DiscordClient from "../../client/Client"; |
22 |
|
|
import CommandOptions from "../../types/CommandOptions"; |
23 |
|
|
import InteractionOptions from "../../types/InteractionOptions"; |
24 |
|
|
import { emoji } from "../../utils/Emoji"; |
25 |
|
|
import BaseCommand from "../../utils/structures/BaseCommand"; |
26 |
|
|
import Pagination from "../../utils/Pagination"; |
27 |
|
|
import MessageEmbed from "../../client/MessageEmbed"; |
28 |
|
|
|
29 |
|
|
export default class BlockedTokenCommand extends BaseCommand { |
30 |
|
|
permissions = [Permissions.FLAGS.MANAGE_GUILD]; |
31 |
|
|
name = "blockedtoken"; |
32 |
|
|
group = "settings"; |
33 |
|
|
aliases = ["btoken", "blockedtokens", "bannedtoken", "bannedtoken"]; |
34 |
|
|
supportsInteractions = true; |
35 |
|
|
|
36 |
|
|
async run(client: DiscordClient, message: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
37 |
|
|
const subcommand = options.isInteraction ? options.options.getSubcommand(true) : options.argv[1]; |
38 |
|
|
|
39 |
|
|
const subcommands = ["add", "remove", "has", "list"]; |
40 |
|
|
|
41 |
|
|
if (!subcommand) { |
42 |
|
|
await message.reply(`${emoji('error')} You must provide a subcommand with this command. The valid subcommands are: \`${subcommands.join('`, `')}\`.`); |
43 |
|
|
return; |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
if (!subcommands.includes(subcommand)) { |
47 |
|
|
await this.deferReply(message, `${emoji('error')} Invalid subcommand provided. The valid subcommands are: \`${subcommands.join('`, `')}\`.`); |
48 |
|
|
return; |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
if (!options.isInteraction && options.argv[2] === undefined && subcommand !== 'list') { |
52 |
|
|
await message.reply(`${emoji('error')} You must specify a token ${subcommand === 'add' ? 'to block' : (subcommand === 'remove' ? 'to remove' : 'to check')}!`); |
53 |
|
|
return; |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
if (message instanceof CommandInteraction) { |
57 |
|
|
await message.deferReply(); |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
switch (subcommand) { |
61 |
|
|
case 'add': |
62 |
|
|
const tokenToBlock = message instanceof Message ? message.content.slice(client.config.get('prefix').length).trim().slice((options as CommandOptions).argv[0].length).trim().slice(subcommand.length).trim() : message.options.getString('token', true); |
63 |
|
|
|
64 |
|
|
if (client.config.props[message.guildId!]?.filters.tokens.includes(tokenToBlock)) { |
65 |
|
|
await this.deferReply(message, `${emoji('error')} The given token is already blocked.`); |
66 |
|
|
return; |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
client.config.props[message.guildId!]?.filters.tokens.push(tokenToBlock); |
70 |
|
|
client.config.write(); |
71 |
|
|
|
72 |
|
|
await this.deferReply(message, `${emoji('check')} The given token has been blocked.`); |
73 |
|
|
break; |
74 |
|
|
|
75 |
|
|
case 'has': |
76 |
|
|
const token = !options.isInteraction ? options.argv[2] : options.options.getString('token', true); |
77 |
|
|
|
78 |
|
|
if (client.config.props[message.guildId!]?.filters.tokens.includes(token)) { |
79 |
|
|
await this.deferReply(message, `${emoji('check')} This token is in the blocklist.`); |
80 |
|
|
return; |
81 |
|
|
} |
82 |
|
|
else { |
83 |
|
|
await this.deferReply(message, `${emoji('error')} This token is not in the blocklist.`); |
84 |
|
|
return; |
85 |
|
|
} |
86 |
|
|
break; |
87 |
|
|
|
88 |
|
|
case 'remove': |
89 |
|
|
const tokenToUnblock = message instanceof Message ? message.content.slice(client.config.get('prefix').length).trim().slice((options as CommandOptions).argv[0].length).trim().slice(subcommand.length).trim() : message.options.getString('token', true); |
90 |
|
|
|
91 |
|
|
const index = client.config.props[message.guildId!]?.filters.tokens.indexOf(tokenToUnblock); |
92 |
|
|
|
93 |
|
|
if (index === -1) { |
94 |
|
|
await this.deferReply(message, `${emoji('error')} The given token was not blocked.`); |
95 |
|
|
return; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
client.config.props[message.guildId!]?.filters.tokens.splice(index, 1); |
99 |
|
|
|
100 |
|
|
client.config.write(); |
101 |
|
|
await this.deferReply(message, `${emoji('check')} The given token has been unblocked.`); |
102 |
|
|
break; |
103 |
|
|
|
104 |
|
|
case 'list': |
105 |
|
|
const tokens: string[] = client.config.props[message.guildId!]?.filters.tokens ?? []; |
106 |
|
|
const safeTokens: string[][] = []; |
107 |
|
|
let length = 0; |
108 |
|
|
|
109 |
|
|
for (const unsafeToken of tokens) { |
110 |
|
|
if (safeTokens.length === 0) |
111 |
|
|
safeTokens.push([]); |
112 |
|
|
|
113 |
|
|
const token = Util.escapeMarkdown(unsafeToken); |
114 |
|
|
|
115 |
|
|
if ((length + token.length) >= 3000) { |
116 |
|
|
safeTokens.push([token]); |
117 |
|
|
length = token.length; |
118 |
|
|
continue; |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
const index = safeTokens.length - 1; |
122 |
|
|
|
123 |
|
|
safeTokens[index].push(token); |
124 |
|
|
length += token.length; |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
const pagination = new Pagination(safeTokens, { |
128 |
|
|
channel_id: message.channelId!, |
129 |
|
|
guild_id: message.guildId!, |
130 |
|
|
limit: 1, |
131 |
|
|
timeout: 120_000, |
132 |
|
|
user_id: message.member!.user.id, |
133 |
|
|
embedBuilder({ currentPage, data, maxPages }) { |
134 |
|
|
return new MessageEmbed({ |
135 |
|
|
author: { |
136 |
|
|
name: `Blocked tokens in ${message.guild!.name}`, |
137 |
|
|
iconURL: message.guild!.iconURL() ?? undefined |
138 |
|
|
}, |
139 |
|
|
description: '`' + data[0].join('`, `') + '`', |
140 |
|
|
footer: { |
141 |
|
|
text: `Page ${currentPage} of ${maxPages}` |
142 |
|
|
} |
143 |
|
|
}); |
144 |
|
|
}, |
145 |
|
|
}); |
146 |
|
|
|
147 |
|
|
let reply = await this.deferReply(message, await pagination.getMessageOptions()); |
148 |
|
|
|
149 |
|
|
if (message instanceof CommandInteraction) |
150 |
|
|
reply = (await message.fetchReply()) as Message; |
151 |
|
|
|
152 |
|
|
pagination.start(reply); |
153 |
|
|
break; |
154 |
|
|
} |
155 |
|
|
} |
156 |
|
|
} |