skip to Main Content

I’m a new discord.js programmer. I’m trying to create a command that, once triggered, will listen for a message and send a reply. Right now, I am trying to get any form of response to the function, but neither the console.log or the message.reply methods are returning any value.
I believe my issue is with the client.on() method, I should have all of my intents in order.
Before it’s asked, I’m calling Events from discord.js because I’ve been using both "messageCreate" and Events.messageCreate interchangeably within testing. Neither is returning information.

My code (replier.js) is-

const { SlashCommandBuilder, Events, Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ 
    intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
    GatewayIntentBits.GuildMembers
    ] ,
    partials: ['MESSAGE', 'CHANNEL', 'REACTION'] }); 

module.exports = {
    data: new SlashCommandBuilder()
    .setName('replier')
    .setDescription('Responds to your input!'),
    async execute(interaction){
        await interaction.reply('Initial Message. Type a response')
        client.on("messageCreate", (message) => {
            console.log('Recieved Input')
            message.reply('Text')})}}

Once the user enters /replier on discord, the expected response is for the bot to send the Initial Message, and wait for a reply from the user. Currently, no response is being given

2

Answers


  1. Intents

    Make sure you have also enabled the MessageContent intent in the developer portal

    Discord Events

    A Discord event should only be defined whenever you want the bot to do something every time that specific event is fired. Normally you would define these event listeners in the root of your code.

    If you want your bot to listen to one or more messages when someone uses the replier command I’d suggest you use message collectors. Using a collector you can tell your bot to collect a set amount of messages that match the given filter. Here’s an example

    await interaction.editReply({ content: 'Initial message. Type a response' });
    
    const filter = (m) => m.author.id === interaction.user.id;
    const collector = interaction.channel.createMessageCollector({ filter, time: 15000, max: 1 });
    
    collector.on('collect', async (message) => {
      await message.reply({ content: `You said ${message.content}` });
    });
    
    collector.on('end', async (collected) => {
        if (collected.size === 0) await interaction.channel.send({ content: 'You did not reply in time' });
    });
    
    Login or Signup to reply.
  2. I don’t know why you are trying to use an eventListener here when you should be using something like interaction.channel.awaitMessages(...), heres the link to the documentation: https://discord.js.org/docs/packages/discord.js/main/BaseGuildTextChannel:Class#awaitMessages
    So basically instead of the whole client.on(...) eventListener, use this:

    const msg = await interaction.channel.awaitMessages({
      filter: (m => m.author.id === interaction.user.id),
      max: 1,
      time: 30_000, // Duration for user input
     });
    

    and since you are already using async, you don’t even need to make the function asynchronous.
    Now you have the msg from the user, you can do whatever you need with it.
    I hope this helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search