1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
import {
BotListener,
CommandHandlerEvent,
Emitter,
emojis,
format,
formatList,
InhibitorReason,
type BotCommandHandlerEvents,
type CommandMessage
} from '#lib';
import { type Client, type InteractionReplyOptions, type ReplyMessageOptions } from 'discord.js';
export default class CommandBlockedListener extends BotListener {
public constructor() {
super('commandBlocked', {
emitter: Emitter.CommandHandler,
event: CommandHandlerEvent.CommandBlocked
});
}
public async exec(...[message, command, reason]: BotCommandHandlerEvents[CommandHandlerEvent.CommandBlocked]) {
return await CommandBlockedListener.handleBlocked(this.client, message, command, reason);
}
public static async handleBlocked(
client: Client,
...[message, command, reason]: BotCommandHandlerEvents[CommandHandlerEvent.CommandBlocked | CommandHandlerEvent.SlashBlocked]
) {
const isSlash = !!command && !!message.util?.isSlash;
void client.console.info(
`${isSlash ? 'SlashC' : 'c'}ommandBlocked`,
`<<${message.author.tag}>>${
command ? ` tried to run <<${command}>> but` : "'s message"
} was blocked because <<${reason}>>.`,
true
);
switch (reason) {
case InhibitorReason.Owner: {
return await respond({
content: `${emojis.error} Only my developers can run the ${format.input(command!.id)} command.`,
ephemeral: true
});
}
case InhibitorReason.SuperUser: {
return await respond({
content: `${emojis.error} You must be a superuser to run the ${format.input(command!.id)} command.`,
ephemeral: true
});
}
case InhibitorReason.DisabledGlobal: {
return await respond({
content: `${emojis.error} My developers disabled the ${format.input(command!.id)} command.`,
ephemeral: true
});
}
case InhibitorReason.DisabledGuild: {
return await respond({
content: `${emojis.error} The ${format.input(command!.id)} command is currently disabled in ${format.input(
message.guild!.name
)}.`,
ephemeral: true
});
}
case InhibitorReason.ChannelGlobalBlacklist:
case InhibitorReason.ChannelGuildBlacklist:
return isSlash
? await respond({
content: `${emojis.error} You cannot use this bot in this channel.`,
ephemeral: true
})
: await (message as CommandMessage).react(emojis.cross);
case InhibitorReason.UserGlobalBlacklist:
case InhibitorReason.UserGuildBlacklist:
return isSlash
? await respond({
content: `${emojis.error} You are blacklisted from using this bot.`,
ephemeral: true
})
: await (message as CommandMessage).react(emojis.cross);
case InhibitorReason.RoleBlacklist: {
return isSlash
? await respond({
content: `${emojis.error} One of your roles blacklists you from using this bot.`,
ephemeral: true
})
: await (message as CommandMessage).react(emojis.cross);
}
case InhibitorReason.RestrictedChannel: {
if (!command) break;
const channels = command.restrictedChannels;
const names: string[] = [];
channels!.forEach((c) => {
names.push(`<#${c}>`);
});
const pretty = formatList(names, 'and');
return await respond({
content: `${emojis.error} ${format.input(command!.id)} can only be run in ${pretty}.`,
ephemeral: true
});
}
case InhibitorReason.RestrictedGuild: {
if (!command) break;
const guilds = command.restrictedGuilds;
const names = guilds!.map((g) => format.input(client.guilds.cache.get(g)?.name ?? g));
const pretty = formatList(names, 'and');
return await respond({
content: `${emojis.error} ${format.input(command!.id)} can only be run in ${pretty}.`,
ephemeral: true
});
}
case InhibitorReason.CannotSend:
case InhibitorReason.Fatal:
// don't send anything
break;
default: {
return await respond({
content: `${emojis.error} Command blocked with reason ${format.input(reason ?? 'unknown')}.`,
ephemeral: true
});
}
}
// some inhibitors do not have message.util yet
function respond(content: string | (ReplyMessageOptions & InteractionReplyOptions)) {
return message.util ? message.util.reply(content) : message.reply(content);
}
}
}
|