【问题标题】:Discord.js, detect if there's a message sent by a certain user?Discord.js,检测是否有某个用户发送的消息?
【发布时间】:2021-03-03 14:18:22
【问题描述】:

所以我正在搞乱创建一个不和谐的机器人,它会反复 ping 用户,直到他们在聊天中回复/说出任何内容(很烦人,对吗?)。如果需要,还可以调整 ping 用户的次数和每次 ping 之间的时间。但是,我似乎无法找到一种方法来检测被 ping 的用户是否真的在聊天中说了什么,以及一种停止循环的方法。

代码的实际 ping 部分在这个 for 循环中:

const ping = async () => {
            for(var i = 1; i <= pingAmount; i++){

                //the wait() command
                await new Promise(r => setTimeout(r, pingTime * 1000));

                //the actual ping
                message.channel.send(`hey <@${userID}> let\'s play minecraft`);

            }

            //sends a message once pinging is finished
            message.channel.send("Pinging Complete.");

        };

我尝试在该循环中嵌套以下代码,但没有得到任何结果。

                client.on('message', message =>{
                    if(message.author == taggedUser) {
                        message.channel.send('User has replied. Stopping pings.')
                        return;
                    }
                });

感谢任何帮助!

完整代码如下:

module.exports = {
    name: 'Ping',
    description: "Pings specified user until they appear",
    execute(message, args, Discord){

        //initialize variables

        const client = new Discord.Client();

        const taggedUser = message.mentions.users.first();

        const userID = message.mentions.users.first().id;

        //splits the command

        const slicedString = message.content.split(' ');

        //grabs specific numbers from command as input

        const pingAmount = slicedString.slice(4,5);

        const pingTime = slicedString.slice(5);


        //display confirmation info in chat

        message.channel.send(`So, ${message.author.username}, you want to annoy ${taggedUser.username}? Alright then lol`);
        message.channel.send(`btw ${taggedUser.username}\'s user ID is ${userID} lmao`);
        message.channel.send(`amount of times to ping: ${pingAmount}`);
        message.channel.send(`time between pings: ${pingTime} seconds`);

        //checks to make sure pingTime isnt too short

        if(pingTime < 5){
            if(pingTime == 1){
                message.channel.send(`1 second is too short!`);
                return;
            } else {
                message.channel.send(`${pingTime} seconds is too short!`);
                return;
            }
        }

        //timer and loop using pingAmount and pingTime as inputs

        const ping = async () => {
            for(var i = 1; i <= pingAmount; i++){

                //the wait() command
                await new Promise(r => setTimeout(r, pingTime * 1000));

                //the actual ping
                message.channel.send(`hey <@${userID}> let\'s play minecraft`);


                const pingedUsers = [taggedUser];
                // doodle message
                const msg = {author: {id:1}};

                // message event
                const onMessage = (message) => {
                if (pingedUsers.indexOf(message.author.id) != -1) {
                console.log("user replied");
                }
                }

                onMessage(msg); // nothing
                pingedUsers.push(msg.author.id); // push the author id
                onMessage(msg); // he replied!
                

            }

            //sends a message once pinging is finished
            message.channel.send("Pinging Complete.");

        };

        //runs the ping function
        ping();

    }
}

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    在这种情况下,您应该比较作者的雪花 (id)。 您可以将被 ping 过的用户放在一个列表中,然后查看消息作者是否在该列表中。

    const pingedUsers = [];
    // doodle message
    const msg = {author: {id:1}};
    
    // message event
    const onMessage = (message) => {
      if (pingedUsers.indexOf(message.author.id) != -1) {
        console.log("user replied");
      }
    }
    
    onMessage(msg); // nothing
    pingedUsers.push(msg.author.id); // push the author id
    onMessage(msg); // he replied!

    【讨论】:

    • 我尝试将该代码添加到循环中并将 pingedUsers 设置为 taggedUser 的 id,但不是在特定用户说什么时打印到控制台,而是在每次该用户被标记时打印机器人。我认为这里有一些我不明白的地方。
    • @Joodoon 是您在消息事件侦听器中的循环,也就是 .on
    • 刚刚编辑了我的原始帖子以包含我的完整代码 - 希望这可以澄清问题。我认为我没有事件侦听器,所以这可能是问题所在?我将如何在我的代码中实现它?再次感谢您的帮助
    猜你喜欢
    • 2023-03-11
    • 2021-04-27
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 2020-10-01
    相关资源
    最近更新 更多