Using Discord.NET, Can i get a message data of a message with which a component has been sent?

375 views Asked by At

For example i have this code:

public class MyModule : InteractionModuleBase<SocketInteractionContext>
{
    [SlashCommand("example", "example command")]
    public async Task Expl()
    {
        var component = new ComponentBuilder()
            .WithButton("leave", "leave", ButtonStyle.Success);

        await Context.Interaction.RespondAsync("Response", components: component.Build());
    }

    [ComponentInteraction("leave")]
    public async Task Lv()
    {
    
    }
}

How can i access the ID of a message with which a component has been sent?

I tried using Context.Interaction.Data but it is obscure and i don't know what to do with it later. Don't have any other ideas, please help.

2

There are 2 answers

3
Tito On

Your title is asking to access the data, however your question is asking for the id
In the case of the ID you'll want to assign the sent message to a variable, and then you can access the id afterwards

[SlashCommand("example", "example command")]
public async Task Expl()
{
    var component = new ComponentBuilder()
        .WithButton("leave", "leave", ButtonStyle.Success);

    var msg = await RespondAsync("Response", components: component.Build());

    ulong id = msg.Id;
}

In the case of the data, if you're further down the chain of the interaction, you can call the original interaction message via Context.Interaction.GetOriginalResponseAsync

0
Pechenka On

YYou can use this, it allows you get access to original message if you're handlering button ineraction.

var interaction = (IComponentInteraction)Context.Interaction;
var message = interaction.Message;