简体   繁体   中英

How to store messages sent by specific user in discordjs?

I'm trying to create a discord js bot which can send a random message by a specified user. Here's my attept:

const ch = client.channels.cache.get("12345");
const soma = client.users.cache.get("4321");
if(message.content.startsWith(prefix+'test')){
    console.log(`${ch}`);
    ch.messages.fetch({ limit: 100 }).then(messages => {
        console.log(`Received ${messages.size} messages`);
        messages.forEach(message => message.author.id)
        messages.forEach(function (message){
            if (message.author.id === {soma}){
                console.log(message.content);
            }
        })
    })
};

I just can't figure out how to put the author id and the message content into an array or just go thru it when the command is executed.

Ok i read ur script and at the const soma = client.users.cache.get("4321"); part i noticed that ur trying to get the id from the users of the bot which isnot needed u can just use the id instantly so all u ahve to do is making soma defined as ur id no need for client.cache.get just like this const soma = "332036461669122048" for example, and for the channel you can just make it into const ch = message.channel.id instead of getting the channel from the client cuz ur getting it in a wrong way

Edit: and at the if (message.author.id === {soma}) you dont need to add the "{","}" its not needed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM