【问题标题】:Discord.js - How to change style of ButtonDiscord.js - 如何更改按钮的样式
【发布时间】:2021-10-16 05:44:12
【问题描述】:

这就是我创建和发送按钮的方式:

client.on('messageCreate', (message) => {
    /* ... Checking Command ... */

    const actionRow = new MessageActionRow().addComponents(
        new MessageButton()
        .setStyle("PRIMARY")
        .setLabel("X")
        .setCustomId("test"));


    message.channel.send({ content: "Test", components: [actionRow] });
}

如预期的那样,聊天中会出现一个蓝色按钮。

这是我的按钮监听器:

client.on("interactionCreate", (interaction) => {
    if (interaction.isButton()) {
        if (interaction.customId === "test") {
            //Before: console.log(interaction.component);

            interaction.component.setStyle("DANGER");

            //After: console.log(interaction.component);
        }
    }
});

.setStyle("DANGER") 之前和之后记录组件对象还显示,样式已成功从主要更改为危险。

但在我的 Discord 客户端中,样式/颜色没有改变,除此之外,我收到一个错误,说交互失败。

样式属性似乎不是只读的:https://discord.js.org/#/docs/main/stable/class/MessageButton?scrollTo=style

那我做错了什么?

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    您仅在本地更新了样式,没有将更改的组件发送回 Discord API。

    要摆脱“此交互失败”的错误,您需要respond 进行交互。一种响应方式是使用MessageComponentInteraction.update(),它会更新原始消息。

    client.on("interactionCreate", (interaction) => {
        if (interaction.isButton()) {
            if (interaction.customId === "test") {
    
                // Change the style of received button component
                interaction.component.setStyle("DANGER");
    
                // Respond to the interaction,
                // and send updated component to the Discord API
                interaction.update({
                    components: [
                        new MessageActionRow().addComponents(interaction.component)
                    ]
                });
    
            }
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多