Discord.js - 秘密获取用户输入

Fir*_*o99 1 javascript node.js discord discord.js

我正在使用 Discord.js 制作 2 人石头剪刀布游戏。

遗憾的是,Discord API 非常慢,并且 afaik 不提供任何类型的中间件。当有人选择他们的形状时,另一个人会在很长一段时间内看到反应(或聊天消息),直到机器人将其删除,从而破坏整个游戏。

我能想到的秘密获取输入的唯一方法是在私人聊天中向用户发送一条消息,他可以对此做出反应。但在我看来,必须从服务器切换到私人聊天然后再返回服务器只会使游戏无法玩。与简单地点击反应相比,这对用户来说工作量太大了。

另一种选择是在聊天中发送消息,只有特定用户才能看到。它可以说“1 = 剪刀,2 = 石头,3 = 布”。(每个玩家的映射都是随机的)。然后用户从选项1、2和3中选择相应的反应。但Discord似乎不允许在聊天中发送消息,只有特定用户才能看到。或者有什么办法吗?

有没有什么方法可以在用户不必切换聊天的情况下秘密获取用户输入?

API 是否可能为我忽略的消息或反应提供任何类型的中间件?

Sku*_*sal 6

\n

Discord 不允许在聊天中发送消息,只有特定用户才能看到该消息。或者有什么办法吗?

\n
\n

不,没有。Discord API 不允许您指定可以查看特定公会消息的用户。

\n
\n

有没有什么方法可以在用户不必切换聊天的情况下秘密获取用户输入?

\n
\n

肯定有!

\n

您可以使用一个相当新的功能,按钮。下面是一个示例代码,您可以使用它来构建您的游戏。剩下要实施的事情是:

\n
    \n
  • 游戏状态,例如谁轮到、谁有多少分等。
  • \n
  • interactionCreate在事件的回调中更新游戏状态(通过 id 识别游戏状态?) 。
  • \n
  • 向玩家显示游戏状态,例如更新原始消息。
  • \n
  • 不允许玩家修改其他游戏对的游戏状态。
  • \n
  • 让用户指定指挥对手!createGame
  • \n
  • 实际的游戏逻辑(决定谁赢了,谁得分等)
  • \n
\n

目前我能想到的就是这些。这些更多的是建议而不是要求。一个人的创造力是没有界限的。

\n
// ... Some object to store the gamestates in? ...\n\nclient.on("messageCreate", async (message) => {\n  \n    // Don\'t reply to bots\n    if (message.author.bot) return;\n\n    // Basic command handler, just for showcasing the buttons\n    if (message.content.startsWith("!createGame")) {\n\n        // ... Probably some argument handling for the opponent (e.g. a mention) ...\n\n        // Create an action row component with buttons attached to it\n        const actionRow = new Discord.MessageActionRow()\n            .addComponents(\n                [["Rock", ""], ["Paper", ""], ["Scissors", "\xe2\x9c\x82\xef\xb8\x8f"]].map((buttonProperties) => {\n                    return new Discord.MessageButton()\n                        .setStyle("PRIMARY")\n                        .setLabel(buttonProperties[0])\n                        .setEmoji(buttonProperties[1])\n                        .setCustomId(`rpsgame_${buttonProperties[0].toLowerCase()}`);\n                })\n            );\n\n        // Send the game message/playground\n        message.channel.send({\n            content: "Rock, Paper and Scissors: The game!",\n            components: [actionRow]\n        });\n        \n    }\n\n});\n
Run Code Online (Sandbox Code Playgroud)\n

为了处理按钮点击,我们使用该interactionCreate事件。

\n
client.on("interactionCreate", (interaction) => {\n    // If the interaction is a button\n    if (interaction.isButton()) {\n        // If the button belongs to our game\n        if (interaction.customId.startsWith("rpsgame")) {\n            // Log what the user has selected to console, as a test\n            console.log(`A user \'${interaction.member.user.tag}\' selected ${interaction.component.emoji.name}.`);\n            // Don\'t forget to reply to an interaction,\n            // otherwise an error will be shown on discord.\n            interaction.update("Update GameState...");\n        }\n    }\n});\n
Run Code Online (Sandbox Code Playgroud)\n

在此输入图像描述

\n

没有人会看到其他用户所选择的内容,除非您希望他们看到。

\n

在此输入图像描述

\n

使用discord.js ^13.0.1。如果您使用的是 v12,有一个不错的软件包称为discord-buttons.

\n