mirror of
https://github.com/keanuplayz/TravBot-v3.git
synced 2024-08-15 02:33:12 +00:00
Added special case for bot DMs
This commit is contained in:
parent
6eea068909
commit
2a4d08d0bc
1 changed files with 52 additions and 24 deletions
|
@ -12,7 +12,14 @@ export function addInterceptRule(handler: (message: Message) => boolean) {
|
|||
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: 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) => {
|
||||
for (const shouldIntercept of interceptRules) {
|
||||
if (shouldIntercept(message)) {
|
||||
|
@ -20,46 +27,65 @@ client.on("message", async (message) => {
|
|||
}
|
||||
}
|
||||
|
||||
const commands = await loadableCommands;
|
||||
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.
|
||||
if (channel.type === "dm" || channel.permissionsFor(client.user!)!.has(Permissions.FLAGS.SEND_MESSAGES)) {
|
||||
const text = content;
|
||||
else if (channel.permissionsFor(client.user!)!.has(Permissions.FLAGS.SEND_MESSAGES)) {
|
||||
const prefix = getPrefix(guild);
|
||||
|
||||
// First, test if the message is just a ping to the bot.
|
||||
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.
|
||||
else if (text.startsWith(prefix)) {
|
||||
const [header, ...args] = text.substring(prefix.length).split(/ +/);
|
||||
const commands = await loadableCommands;
|
||||
|
||||
if (commands.has(header)) {
|
||||
const command = commands.get(header)!;
|
||||
|
||||
// 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(
|
||||
const result = await command.execute(args, menu, {
|
||||
header,
|
||||
args,
|
||||
{
|
||||
author,
|
||||
channel,
|
||||
client,
|
||||
guild,
|
||||
member,
|
||||
message,
|
||||
args: []
|
||||
},
|
||||
{
|
||||
header,
|
||||
args,
|
||||
permission: 0,
|
||||
nsfw: false,
|
||||
channelType: CHANNEL_TYPE.ANY
|
||||
}
|
||||
);
|
||||
...defaultMetadata
|
||||
});
|
||||
|
||||
// If something went wrong, let the user know (like if they don't have permission to use a command).
|
||||
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(
|
||||
`I don't have permission to send messages in ${channel}. ${
|
||||
member!.hasPermission(Permissions.FLAGS.ADMINISTRATOR)
|
||||
|
|
Loading…
Reference in a new issue