crycode-de / ioBroker.discord

ioBroker Discord bot integration
MIT License
6 stars 4 forks source link

Interaction caching for non-commands #66

Closed Babywombat closed 2 months ago

Babywombat commented 3 months ago

Firstly I want to thank you for all the work that you do on this. I really love it!

Is your feature request related to a problem? Please describe. I wanted to use buttons for my automation and enabled the raw message option. I tried to answer to an interaction by a button I created using the id in the raw interaction, but noticed that I get an error. Looking through the code I saw that only command interactions are cached and can be responded to.

Describe the solution you'd like Please implement the option to cache interactions that do not relate to commands.

Describe alternatives you've considered I could just check for changes in the raw interaction and respond with a normal message, but I will always get the error when pressing the button.

crycode-de commented 3 months ago

Thank you for submitting this issue. 👍

I haven't used buttons in discord yet.

Can you provide a detailed example of what you have done including some example code?

Babywombat commented 3 months ago

Thank you for your immediate response.

This is the code I was using to test what I wanted to do:

function sendNotificationWithButton(notification) {
    sendTo('discord.0', 'sendMessage', {
        serverId: '',
        channelId: '',
        content: {
            content: notification,
            components: [
                {
                    "type" : 1,
                    "components": [
                        {
                            "type": 2,
                            "label": "Click me!",
                            "style": 1,
                            "custom_id": "test_id"
                        }
                    ]
                }
            ]
        }
    }, (ret) => {
        console.log(ret);
    }); 
}

function sendInteractionReply(interactionId) {
    sendTo('discord.0', 'sendCustomCommandReply', {
        interactionId: interactionId,
        content: {
            content: "Response"
        }
    }, (ret) => {
        console.log(ret);
    });
}

function getInteractionIdFromString(interactionString) {
    var interactionJson = JSON.parse(interactionString);
    return interactionJson.id;
}

on({id: "discord.0.raw.interactionJson", change: "ne"}, async function (obj) {
    var interactionId = getInteractionIdFromString(obj.state.val);
    console.log(interactionId);

    sendInteractionReply(interactionId);
});

sendNotificationWithButton("test");

This is the error I am getting after clicking the button:

script.js.Notification.Test: { error: 'Error sending reply: Error: No current interaction with ID 1258860476159496202 found for reply!', interactionId: '1258860476159496202', content: { content: 'Response' } }

crycode-de commented 3 months ago

👍 I'll have a look at this in the next days.

crycode-de commented 2 months ago

@Babywombat Can you try the current GitHub version? This should work with your provided example. grafik

Internally all unknown interactions are now cached and should be usable by sendCustomCommandReply for 15 minutes after they are created. The reply to these interactions is always deferred, so the user has some time to send the reply.

Babywombat commented 2 months ago

@crycode-de I just tested it out and it works like a charm. Thank you for the quick addition.

crycode-de commented 2 months ago

Great, thank you! I'll release a new latest version probably tomorrow.

crycode-de commented 2 months ago

Version 2.4.0 released containing this changes.