Allow chatting with bot through direct messages

Added new command to make the bot see direct messages,
because by default these are not visible,
unless bot opens direct message with the user.
This commit is contained in:
Wroclaw 2024-04-23 21:10:52 +02:00
parent 63cb52e7f4
commit 3c10f4ed6f
2 changed files with 30 additions and 1 deletions

26
src/commands/listen.ts Normal file
View file

@ -0,0 +1,26 @@
import {ApplicationCommandType, ChatInputCommandInteraction } from "discord.js";
import Command, { ApplicationIntegrationType, InteractionContextTypes } from "../command";
export default class Listen extends Command implements Command {
// This command exists because Discord bots don't receive direct messages
// unless they explicitly open a DM channel with the user
name = "listen";
description = "Makes the bot listen on your direct messages";
type = ApplicationCommandType.ChatInput;
options = [];
integration_types = [
ApplicationIntegrationType.Guild_Install,
ApplicationIntegrationType.User_Install
];
contexts = [InteractionContextTypes.BotDM];
async execute(interaction: ChatInputCommandInteraction) {
await interaction.user.createDM();
await interaction.reply({
content: "I'm now listening to your direct messages",
ephemeral: true,
});
}
}

View file

@ -11,6 +11,7 @@ const discord = new DiscordApi.Client({
intents: [
DiscordApi.GatewayIntentBits.Guilds,
DiscordApi.GatewayIntentBits.GuildMessages,
DiscordApi.GatewayIntentBits.DirectMessages,
DiscordApi.GatewayIntentBits.MessageContent,
]
});
@ -58,7 +59,9 @@ discord.on("ready", event => {
discord.on("messageCreate", message => {
if (message.author.bot) return;
if (!message.channel.isDMBased()) {
if (!message.mentions.has(message.client.user, { ignoreEveryone: true })) return;
}
return queueRequest(message);
});