From 3c10f4ed6f828c178d37cbbb10ff16e5f01ef19f Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Tue, 23 Apr 2024 21:10:52 +0200 Subject: [PATCH] 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. --- src/commands/listen.ts | 26 ++++++++++++++++++++++++++ src/index.ts | 5 ++++- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/commands/listen.ts diff --git a/src/commands/listen.ts b/src/commands/listen.ts new file mode 100644 index 0000000..3f1c39d --- /dev/null +++ b/src/commands/listen.ts @@ -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, + }); + } +} diff --git a/src/index.ts b/src/index.ts index a565a3f..eb7a9f7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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.mentions.has(message.client.user, { ignoreEveryone: true })) return; + if (!message.channel.isDMBased()) { + if (!message.mentions.has(message.client.user, { ignoreEveryone: true })) return; + } return queueRequest(message); });