I have a button command, that sometimes works, and sometimes gives me an "Invalid context type. Expected SocketInteractionContext'1, got SocketInteractionContext" exception. I'm not even really sure what code I can provide here to help with this, but I'll provide what makes sense to. It seems to error out most of the time but every once and a while it works (very rarely).
The command sending the button:
[Group("voicemaster", "Commands for managing automatically created voice channels.")]
public class VoiceMasterModule : InteractionModuleBase<SocketInteractionContext>
{
[SlashCommand("settings", "View (and change) the settings for voice master.")]
[RequireUserPermission(GuildPermission.Administrator, Group = "Permission")]
[RequireOwner(Group = "Permission")]
public async Task SettingsAsync()
{
var settingsEmbed = new EmbedBuilder()
.WithTitle("Voice Master Settings")
.WithColor(Color.Blue)
.WithDescription("Please select one of the options below.")
.AddField("1️⃣", "Set the voice master channel.", true)
.AddField("❌", "Remove voice master functionality from a channel.", true)
.AddField("✏️", "Modify the name template of temporary voice channels.", true)
.Build();
var settingsComponents = new ComponentBuilder()
.WithButton("1️⃣", "voicemaster.setchannel", ButtonStyle.Primary)
.WithButton("✖️", "voicemaster.removechannel", ButtonStyle.Danger)
.WithButton("✏️", "voicemaster.setname", ButtonStyle.Secondary)
.Build();
// Send the embed with its components.
await RespondAsync(embed: settingsEmbed, components: settingsComponents, ephemeral: true);
}
}
The ComponentCommand for "voicemaster.setchannel" (I have not implemented the others):
public class VoiceMasterComponentModule : InteractionModuleBase<SocketInteractionContext<SocketMessageComponent>>
{
[ComponentInteraction("voicemaster.setchannel")]
public async Task SetChannelAsync()
{
var setChannelEmbed = new EmbedBuilder()
.WithColor(Color.Blue)
.WithDescription("Please select the voice master channel below.")
.Build();
// Get a list of all of the Context.Guild's voice channels.
var voiceChannels = Context.Guild.Channels.Where(c => c.ChannelType == ChannelType.Voice);
var options = voiceChannels.Select(vc => new SelectMenuOptionBuilder
{
Label = vc.Name,
Value = vc.Id.ToString()
}).ToList();
var setChannelComponents = new ComponentBuilder()
.WithSelectMenu("voicemaster.setchannel.select", options)
.Build();
await RespondAsync(embed: setChannelEmbed, components: setChannelComponents, ephemeral: true);
}
}
DiscordSocketClient.ButtonExecuted event handler:
private async Task ButtonExecuted(SocketMessageComponent component)
{
try
{
var context = new SocketInteractionContext<SocketMessageComponent>(Client, component);
await InteractionService.ExecuteCommandAsync(context, ServiceProvider);
}
catch (Exception e)
{
Log.Error("An error occurred while executing a command.", e);
}
}
Thanks in advance for any help!