Stefanidze
Stefanidze

Reputation: 52

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

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.

Upvotes: 0

Views: 943

Answers (2)

Pechenka
Pechenka

Reputation: 46

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;

Upvotes: 1

Tito
Tito

Reputation: 68

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

Upvotes: 0

Related Questions