单击按钮(discord.net)时如何执行命令?
How to execute a command when clicking a button (discord.net)?
我想在单击“Spieler moven”按钮时执行我的任务“test1”。我怎么做?目前你写 !test1 然后它执行。
因此玩家点击“Spieler moven”按钮,任务“test1”被执行。
[Command("spawner")]
public async Task Spawn()
{
var builder = new ComponentBuilder()
.WithButton("Spieler moven", "1", ButtonStyle.Success);
await ReplyAsync("Welche Option willst du ausführen?", components: builder.Build());
}
public async Task Test1()
{
var user = Context.User as SocketGuildUser;
var Queuelist = Context.Guild.GetVoiceChannel(936628853340778506).Users; //Checke ob jemand in der Warteschlange ist
bool Queue = false; //Standardmäßig Niemand in der Queue
foreach (var u in Queuelist)
{
Queue = true;
}
if (Queue == true)
{
var VoiceUsers = Context.Guild.GetVoiceChannel(936628853340778507).Users; // Checke ob Support 1 besetzt ist
bool sup1 = false;
foreach (var u in VoiceUsers)
{
bool supporter = u.Equals(Context.User); // Checke ob der Supporter selbst in Support 1 ist
if (supporter == false)
sup1 = true;
}
if (sup1 == false)
{
await user.ModifyAsync(x => { x.ChannelId = 936628853340778507; }); //Move zu Support 1
}
else
{
await user.ModifyAsync(x => { x.ChannelId = 936628853340778508; }); //Support 1 ist voll, move zu Support 2
}
} else
await ReplyAsync("" + Context.Message.Author.Mention + ", es befindet sich niemand bei Tränix :c");
}
预先致谢
您可以收听 InteractionCreated 事件,然后从那里触发您的任务。
我正以这种方式处理我的按钮,但今天可能有更简单的解决方案,因为这段代码是大约 3 个月前制作的。
public HandlingService(IServiceProvider services)
{
_client = services.GetRequiredService<DiscordSocketClient>();
_client.InteractionCreated += HandleInteractionCreated;
}
private async Task HandleInteractionCreated(SocketInteraction interaction)
{
switch (interaction.Type)
{
case InteractionType.ApplicationCommand:
//Handle SlashCommands
break;
case InteractionType.ApplicationCommandAutocomplete:
//Handle Autocomplete
break;
case InteractionType.MessageComponent:
MessageComponentHandlingService.MessageComponentHandler(interaction)
break;
default: // We dont support it
Console.WriteLine("Unsupported interaction type: " + interaction.Type);
break;
}
}
class MessageComponentHandlingService
{
public static async Task MessageComponentHandler(SocketInteraction interaction)
{
var parsedArg = (SocketMessageComponent)interaction;
switch (parsedArg.Data.CustomId)
{
case "Spieler moven": //maybe also "spieler moven" Here you have to insert the costom ID of the button
await Test1()
break;
}
}
}
我想在单击“Spieler moven”按钮时执行我的任务“test1”。我怎么做?目前你写 !test1 然后它执行。 因此玩家点击“Spieler moven”按钮,任务“test1”被执行。
[Command("spawner")]
public async Task Spawn()
{
var builder = new ComponentBuilder()
.WithButton("Spieler moven", "1", ButtonStyle.Success);
await ReplyAsync("Welche Option willst du ausführen?", components: builder.Build());
}
public async Task Test1()
{
var user = Context.User as SocketGuildUser;
var Queuelist = Context.Guild.GetVoiceChannel(936628853340778506).Users; //Checke ob jemand in der Warteschlange ist
bool Queue = false; //Standardmäßig Niemand in der Queue
foreach (var u in Queuelist)
{
Queue = true;
}
if (Queue == true)
{
var VoiceUsers = Context.Guild.GetVoiceChannel(936628853340778507).Users; // Checke ob Support 1 besetzt ist
bool sup1 = false;
foreach (var u in VoiceUsers)
{
bool supporter = u.Equals(Context.User); // Checke ob der Supporter selbst in Support 1 ist
if (supporter == false)
sup1 = true;
}
if (sup1 == false)
{
await user.ModifyAsync(x => { x.ChannelId = 936628853340778507; }); //Move zu Support 1
}
else
{
await user.ModifyAsync(x => { x.ChannelId = 936628853340778508; }); //Support 1 ist voll, move zu Support 2
}
} else
await ReplyAsync("" + Context.Message.Author.Mention + ", es befindet sich niemand bei Tränix :c");
}
预先致谢
您可以收听 InteractionCreated 事件,然后从那里触发您的任务。
我正以这种方式处理我的按钮,但今天可能有更简单的解决方案,因为这段代码是大约 3 个月前制作的。
public HandlingService(IServiceProvider services)
{
_client = services.GetRequiredService<DiscordSocketClient>();
_client.InteractionCreated += HandleInteractionCreated;
}
private async Task HandleInteractionCreated(SocketInteraction interaction)
{
switch (interaction.Type)
{
case InteractionType.ApplicationCommand:
//Handle SlashCommands
break;
case InteractionType.ApplicationCommandAutocomplete:
//Handle Autocomplete
break;
case InteractionType.MessageComponent:
MessageComponentHandlingService.MessageComponentHandler(interaction)
break;
default: // We dont support it
Console.WriteLine("Unsupported interaction type: " + interaction.Type);
break;
}
}
class MessageComponentHandlingService
{
public static async Task MessageComponentHandler(SocketInteraction interaction)
{
var parsedArg = (SocketMessageComponent)interaction;
switch (parsedArg.Data.CustomId)
{
case "Spieler moven": //maybe also "spieler moven" Here you have to insert the costom ID of the button
await Test1()
break;
}
}
}