Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
659 views
in Technique[技术] by (71.8m points)

dm - How to use the awaitMessages function in discord.js

I have some code for discord.js that sends a user a DM when they join the server. They then have to enter the password given to them, and it will then give them a role that allows them access to channels.

const Discord = require('discord.js');
const client = new Discord.Client();

client.once('ready', () => {
    console.log('Ready!');
});

client.on('guildMemberAdd', guildMember => {
    console.log("Joined");
    guildMember.send("Welcome to the server! Please enter the password given to you to gain access to the server:")
      .then(function(){
        guildMember.awaitMessages(response => message.content, {
          max: 1,
          time: 300000000,
          errors: ['time'],
        })
        .then((collected) => {
            if(response.content === "Pass"){
              guildMember.send("Correct password! You now have access to the server.");
            }
            else{
              guildMember.send("Incorrect password! Please try again.");
            }
          })
          .catch(function(){
            guildMember.send('You didnt input the password in time.');
          });
      });
});

client.login("token");

The thing is, I don't really know how to use the awaitResponses function. I do not know how to call it, because all the tutorials I find use it with message.member.

When I run my code, I get these three errors: UnhandledPromiseRejectionWarning: TypeError: guildMember.awaitMessages is not a function

UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)

Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.

I do not know what lines these are referring to, so I am very confused.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here's a quick rundown on how awaitMessages works:

First of all, the .awaitMessages() function extends GuildChannel, meaning you had already messed up a little bit by extending the GuildMember object instead. - You could easily fetch a channel using

const channelObject = guildMember.guild.channels.cache.get('Channel ID Here'); // Gets a channel object based on it's ID
const channelObject = guildMember.guild.channels.cache.find(ch => ch.name === 'channel name here') // Gets a channel object based on it's name

awaitMessages() also gives you the ability to add a filter for a certain condition an input must have. Let's take the freshly new member as an example. We could simply tell the client to only accept input from members who have the same id as the guildMember object, so basically only the new Member.

const filter = m => m.author.id === guildMember.id

Now, finally, after we've gathered all of the resources needed, this should be our final code to await Messages.

const channelObject = guildMember.guild.channels.cache.get('Channel ID Here');
    const filter = m => m.author.id === guildMember.id
    channelObject.awaitMessages(filter, {
        max: 1, // Requires only one message input
        time: 300000000, // Maximum amount of time the bot will await messages for
        errors: 'time' // would give us the error incase time runs out.
    })

To read more about awaitMessages(), click here!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...