Added special case for bot DMs

This commit is contained in:
WatDuhHekBro 2021-04-04 20:01:29 -05:00
parent 6eea068909
commit 2a4d08d0bc

View file

@ -12,7 +12,14 @@ export function addInterceptRule(handler: (message: Message) => boolean) {
interceptRules.push(handler); interceptRules.push(handler);
} }
const defaultMetadata = {
permission: 0,
nsfw: false,
channelType: CHANNEL_TYPE.ANY
};
// Note: client.user is only undefined before the bot logs in, so by this point, client.user cannot be undefined. // Note: client.user is only undefined before the bot logs in, so by this point, client.user cannot be undefined.
// Note: guild.available will never need to be checked because the message starts in either a DM channel or an already-available guild.
client.on("message", async (message) => { client.on("message", async (message) => {
for (const shouldIntercept of interceptRules) { for (const shouldIntercept of interceptRules) {
if (shouldIntercept(message)) { if (shouldIntercept(message)) {
@ -20,46 +27,65 @@ client.on("message", async (message) => {
} }
} }
const commands = await loadableCommands;
const {author, channel, content, guild, member} = message; const {author, channel, content, guild, member} = message;
const text = content;
const menu = {
author,
channel,
client,
guild,
member,
message,
args: []
};
// Execute a dedicated block for messages in DM channels.
if (channel.type === "dm") {
// In a DM channel, simply forget about the prefix and execute any message as a command.
const [header, ...args] = text.split(/ +/);
if (commands.has(header)) {
const command = commands.get(header)!;
// Send the arguments to the command to resolve and execute.
const result = await command.execute(args, menu, {
header,
args,
...defaultMetadata
});
// If something went wrong, let the user know (like if they don't have permission to use a command).
if (result) {
channel.send(result);
}
} else {
channel.send(
`I couldn't find the command or alias that starts with \`${header}\`. To see the list of commands, type \`help\``
);
}
}
// Continue if the bot has permission to send messages in this channel. // Continue if the bot has permission to send messages in this channel.
if (channel.type === "dm" || channel.permissionsFor(client.user!)!.has(Permissions.FLAGS.SEND_MESSAGES)) { else if (channel.permissionsFor(client.user!)!.has(Permissions.FLAGS.SEND_MESSAGES)) {
const text = content;
const prefix = getPrefix(guild); const prefix = getPrefix(guild);
// First, test if the message is just a ping to the bot. // First, test if the message is just a ping to the bot.
if (new RegExp(`^<@!?${client.user!.id}>$`).test(text)) { if (new RegExp(`^<@!?${client.user!.id}>$`).test(text)) {
channel.send(`${author}, my prefix on this guild is \`${prefix}\`.`); channel.send(`${author}, my prefix on this server is \`${prefix}\`.`);
} }
// Then check if it's a normal command. // Then check if it's a normal command.
else if (text.startsWith(prefix)) { else if (text.startsWith(prefix)) {
const [header, ...args] = text.substring(prefix.length).split(/ +/); const [header, ...args] = text.substring(prefix.length).split(/ +/);
const commands = await loadableCommands;
if (commands.has(header)) { if (commands.has(header)) {
const command = commands.get(header)!; const command = commands.get(header)!;
// Send the arguments to the command to resolve and execute. // Send the arguments to the command to resolve and execute.
// TMP[MAKE SURE TO REPLACE WITH command.execute WHEN FINISHED] const result = await command.execute(args, menu, {
const result = await command.execute( header,
args, args,
{ ...defaultMetadata
author, });
channel,
client,
guild,
member,
message,
args: []
},
{
header,
args,
permission: 0,
nsfw: false,
channelType: CHANNEL_TYPE.ANY
}
);
// If something went wrong, let the user know (like if they don't have permission to use a command). // If something went wrong, let the user know (like if they don't have permission to use a command).
if (result) { if (result) {
@ -67,7 +93,9 @@ client.on("message", async (message) => {
} }
} }
} }
} else { }
// Otherwise, let the sender know that the bot doesn't have permission to send messages.
else {
author.send( author.send(
`I don't have permission to send messages in ${channel}. ${ `I don't have permission to send messages in ${channel}. ${
member!.hasPermission(Permissions.FLAGS.ADMINISTRATOR) member!.hasPermission(Permissions.FLAGS.ADMINISTRATOR)