简体   繁体   English

如何将特定用户发送的消息存储在discordjs中?

[英]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.我正在尝试创建一个 discord js 机器人,它可以由指定用户发送随机消息。 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.我只是不知道如何在执行命令时将作者 ID 和消息内容放入一个数组或只是 go 到它。

Ok i read ur script and at the const soma = client.users.cache.get("4321");好的,我阅读了您的脚本并在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我注意到你试图从机器人的用户那里获取不需要的 id 你可以立即使用 id 所以你要做的就是将 soma 定义为你的 id 不需要 client.cache.get 就像这样const soma = "332036461669122048"例如,对于频道,您可以将其设置为const ch = message.channel.id而不是从客户端获取频道,因为您以错误的方式获取它

Edit: and at the if (message.author.id === {soma}) you dont need to add the "{","}" its not needed编辑:并且在if (message.author.id === {soma})你不需要添加它不需要的 "{","}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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