简体   繁体   English

Discord.js 对应多种表情

[英]Discord.js Responding to multiple types of emojis

I'm currently looking at getting a soundboard in Discord.js by using emojis to act as the controls to the soundboard, it should then respond with an appropriate sound based on the emoji sent.我目前正在考虑通过使用表情符号作为音板的控件在 Discord.js 中获取音板,然后它应该根据发送的表情符号以适当的声音响应。 However, I'm having issues with at least testing if I can receive different types of emojis based on the emojis array.但是,我至少在测试是否可以根据emojis数组接收不同类型的表情符号时遇到问题。

main.js main.js

var soundBoard;

client.on('message', async message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    switch (command) {
    ...
    case 'soundboard':
            client.commands.get('soundboard').execute(message, args, soundBoard);
            break;
    ...
    }
}

soundboard.js音板.js

module.exports = {
    name: 'soundboard',
    description: "Soundboard",
    async execute(message, args, sb) {
        const emojis = [
            '😄',
            '✋'
        ];
        if (!sb) {
            sb = await message.channel.send('Soundboard Active');
            const filter = (reaction, user) => {
                return emojis.includes(reaction.emoji.name);
            };
            sb.awaitReactions(filter)
                .then(collected => {
                    console.log("Collected: " + collected);
                    sb.react('✅');
                })
                .catch(console.error);
            for (let e of emojis) {
                sb.react(e);
            }
        }
        else {
            console.log(sb);
        }
    }
}

sb is passed from a global variable declared in my main.js . sb是从我的main.js中声明的全局变量传递的。 Unfortunately at the moment all that seems to happen is the message appears but whenever I click the emojis to respond the expected result of adding a ✅ to the original message fails.不幸的是,目前似乎发生的所有事情都是出现消息,但是每当我单击表情符号以响应在原始消息中添加 ✅ 的预期结果时都会失败。

First of all, I would recommend limiting your reaction collector by a specific amount of time and by only allowing the user who sent the message to interact with it.首先,我建议将您的反应收集器限制在特定的时间量内,并且只允许发送消息的用户与之交互。 I would also recommend using a reactionCollector instead of awaitReactions here.我还建议在这里使用reactionCollector而不是awaitReactions

I believe the problem you are having is that .then() is only called on awaitReactions() after the time limit of awaitReactions() has ended.我相信您遇到的问题是 .then( .then()仅在awaitReactions()的时间限制结束后才在awaitReactions() () 上调用。 In other words, your current reaction collector is attempting to react with a check emoji after a certain period of time has passed, instead of attempting to do so after one reaction has been collected.换句话说,您当前的反应收集器会在一段时间后尝试使用检查表情符号做出反应,而不是在收集到一个反应后尝试这样做。 You are essentially attempting to listen to the equivalent of a reactionCollector 's "end" event instead of its "collect" event.您本质上是在尝试听等效于reactionCollector的“结束”事件而不是其“收集”事件。

By using a reactionCollector , you can specifically run code every time an individual emoji is clicked on by listening to the "collect" event.通过使用reactionCollector ,您可以在每次单击单个表情符号时通过侦听“收集”事件专门运行代码。 Here's an example:这是一个例子:

let collector = sb.createReactionCollector((r, user) => emojis.includes(r.emoji.name) && user.id === message.author.id, { time: 120000 });

//This is the event you need to use
collector.on("collect", r => {
     console.log("This message is logged when we receive a reaction.");
     sb.react('✅');
});

//This is the equivalent of the event you are currently using
collector.on("end", collected => {
     console.log("This is where your current code is trying to add ✅");
     console.log("This would only be logged after 120 secs in this example.")
})

Relevant resources:相关资源:
https://discord.js.org/#/docs/main/stable/class/Message?scrollTo=createReactionCollector https://discord.js.org/#/docs/main/stable/class/Message?scrollTo=awaitReactions https://discord.js.org/#/docs/main/stable/class/Message?scrollTo=createReactionCollector https://discord.js.org/#/docs/main/stable/class/Message?scrollTo=awaitReactions

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

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