Условие if не перестает быть включенным - PullRequest
4 голосов
/ 04 мая 2020

Я пишу бота для своей школы, и в настоящее время я разрабатываю функцию, которая позволяет ученикам разговаривать по голосовому каналу, если ученик отправил конкретное сообщение c и если учитель отреагировал на него с указанием c эмодзи. Вот код.

if (handRaiseModeActive === true) {
client.on('message', handMsg => {
  if (handRaiseModeActive === true) {
  if ((handMsg.content.includes(PREFIX + 'talk')) && handMsg.channel.id === hgCID) {
    superConsole(`**@${handMsg.author.tag} asked to speak ||\`${handMsg.author.id}\`||**`)
    handMsg.react('✅')
    handMsg.react('❎')
    client.on('messageReactionAdd', (reaction, user) => {

      if (reaction._emoji.name == '✅' && user.id !== botID && (user.id == teacherID || devID.includes(user.id))) {
        handMsg.member.voice.setMute(false)
        handMsg.reactions.removeAll();
        superConsole(`handRaiseMode | permission to speak given to @${handMsg.author.tag} ||\`${handMsg.author.id}\`||`)
      } else if (reaction._emoji.name == '❎' && user.id !== botID && (user.id == teacherID || devID.includes(user.id))) {
        handMsg.reactions.removeAll()
        superConsole(`handRaiseMode | permission to speak denied to @${handMsg.author.tag} ||\`${handMsg.author.id}\`||`)
      }

    });
  }
  }
})
}

teacherID - это идентификатор учителя, devID - это массив со всеми идентификаторами разработчиков и botID ... идентификатор бота. Команда устанавливает handRaiseModeActive на true или false. superConsole - это функция, в которой событие отправляется по каналу и в консоли.

И вот моя проблема: когда студент впервые спрашивает разрешение на выступление, все работает нормально, но если, после этого другой ученик получает разрешение говорить с помощью handRaiseMode, все ученики, которые ранее просили выступить, без звука ... Кажется, что l.4 все еще работает, несмотря на то, что он должен был закончиться. Я не очень понимаю, как это работает. Нужна помощь!

Заранее спасибо за ответ.

Примечание: я знаю, что есть два if (handRaisModeActive === true), спасибо. Второй бесполезен. Я удалю это.

Ответы [ 2 ]

2 голосов
/ 04 мая 2020

Я думаю, что ваша проблема может быть в том, что вы вкладываете свои события, что является плохой привычкой, поскольку это может вызвать утечки памяти и еще много чего.

Я думаю, что ваша проблема может быть просто исправлена ​​с помощью awaitReactions() вместо:

const filter = (reaction, user) => {
    return ['✅', '❎'].includes(reaction.emoji.name) && (user.id == teacherID || devID.includes(user.id))
};

handMsg.awaitReactions(filter, {
        max: 1,
        time: 60000,
        errors: ['time']
    })
    .then(collected => {
        const reaction = collected.first();

        if (reaction.emoji.name === '✅') {
            handMsg.member.voice.setMute(false)
            handMsg.reactions.removeAll();
            superConsole(`handRaiseMode | permission to speak given to @${handMsg.author.tag} ||\`${handMsg.author.id}\`||`)
        } else {
            handMsg.reactions.removeAll()
            superConsole(`handRaiseMode | permission to speak denied to @${handMsg.author.tag} ||\`${handMsg.author.id}\`||`)
        }
    })
    .catch(collected => {
        handMsg.reply('the teacher did not give you permission in time!');
    });
0 голосов
/ 04 мая 2020

Этот код должен работать, действительно классная идея, у вас есть! Wi sh моя школа использовала разногласия Было бы здорово написать для них бота:)

Если у вас возникли проблемы с этим кодом, не стесняйтесь комментировать, и я исправлю его

Примечание: я протестировал этот код, и он, кажется, работает для меня, я использовал эти значения / функции для тестирования

handRaiseModeActive = true;

function superConsole(str) {
    console.log(str)
}
// I will use this function, it makes it really easy to change later on.ah
    async function addEmoji(message, validReactions) {

        // Valid reactions = ["emoji", "emoji"]
        for (const reaction of validReactions) await message.react(reaction)

        // Define the filter. (const filter = ${condition})
        const filter = (reaction, user) => validReactions.includes(reaction.emoji.name) && (!user.bot) // && (user.id == teacherID || devID.includes(user.id))

        // return the name of the emoji that the user reacted (with filters applied)
        return message
            .awaitReactions(filter, {
                max: 1 // max amount of reactions it will read, (1 = waits for one reaction so forth)
                    /* if you wanted to you could also add a time limit
                     * time: ${time} (int)
                     */
            })
            // grab the first emoji that was reacted
            .then(collected => collected.first() && collected.first().emoji.name);
    }


    async function putYourHandsToTheSky(handMsg) {
        // Better to use this than what you have, if someone tries to explain the !talk command to someone it will run

        superConsole(`**@${handMsg.author.tag} asked to speak ||\`${handMsg.author.id}\`||**`)

        /* run the addEmoji function passing in :
         * the message variable,
         * and a array with the valid reactions that it will await for
         */

        const emoji = await addEmoji(handMsg, ["✅", "❎"])
        let myHomie = handMsg.guild.members.get(handMsg.author.id);

        if (emoji == "✅") {
            superConsole(`handRaiseMode | permission to speak given to @${handMsg.author.tag} ||\`${handMsg.author.id}\`||`)
            await myHomie.setMute(false)
            if (handMsg.deletable == true) {
                handMsg.delete() // deletes message, you can change this if you want. just cleans up my chat while testing this
            } else {
                superConsole(`I cannot delete this message! Role hierarchy I suppose...`)
            }
        } else if (emoji == "❎") {
            superConsole(`handRaiseMode | permission to speak denied to @${handMsg.author.tag} ||\`${handMsg.author.id}\`||`)
            if (handMsg.deletable == true) {
                handMsg.delete() // deletes message, you can change this if you want. just cleans up my chat while testing this
            } else {
                superConsole(`I cannot delete this message! Role hierarchy I suppose...`)
            }
        }
    }
    client.on('message', (handMsg) => {
        if (handMsg.content.startsWith(PREFIX + talk) && handMsg.channel.id == hgCID) {
            putYourHandsToTheSky(handMsg)
        }
    });

...