/// <summary> /// Handle an incoming webhook request and return the default data to send back to Discord. /// </summary> /// <param name="request">HttpRequest for the interaction POST</param> /// <returns>Handle Webhook operation</returns> public async Task <InteractionResponse?> HandleWebhookPost(string requestBody) { try { // Attempt to get the Interact object from the JSON ... var i = JsonConvert.DeserializeObject <Interaction>(requestBody); // ... and tell the handler to run the command ... var jobj = JObject.Parse(requestBody); DiscordUser?user = jobj["member"]?["user"]?.ToObject <DiscordUser>(); // ... because we cant serialize direct to a DiscordMember, we are working around this // and using a DiscordUser instead. I would have to set the Lib as upstream to this before I // would be able to change this. i.User = user; BaseDiscordClient?client = null; if (_discord is not null) { client = _discord; } else if (_sharded is not null) { foreach (var shard in _sharded.ShardClients) { if (shard.Value.Guilds.ContainsKey(i.GuildId)) { client = shard.Value; } } } if (client is null) { throw new Exception("Failed to get a proper client for this request."); } await _slash.HandleInteraction(client, i, this); } catch (Exception ex) { // ... if it errors, log and return null. _logger.LogError(ex, "Webhook Handler failed."); return(null); } // ... return the default interaction type. var response = new InteractionResponseBuilder() .WithType(_config.DefaultResponseType); if (_config.DefaultResponseType != Enums.InteractionResponseType.Acknowledge && _config.DefaultResponseType != Enums.InteractionResponseType.ACKWithSource) { response.Data = _config.DefaultResponseData; } return(response.Build()); }
public async Task HelloWorldSlashCommandAsync(InteractionContext ctx) { var response = new InteractionResponseBuilder() .WithType(InteractionResponseType.ChannelMessageWithSource) .WithData(new InteractionApplicationCommandCallbackDataBuilder() .WithEmbed(new DiscordEmbedBuilder() .WithTitle("Hello World!") .WithDescription($"And hello to you too, {ctx.Interaction.User.Username}")) .WithContent("How's Life?")); await ctx.ReplyAsync(response.Build()); }
private InteractionResponseBuilder GetDeafultResponse() { // createa new response object .... var response = new InteractionResponseBuilder() .WithType(_config.DefaultResponseType); // ... add the optional configs ... if (_config.DefaultResponseType == InteractionResponseType.ChannelMessageWithSource) { response.Data = _config.DefaultResponseData; } // ... and return the builder object. return(response); }
public async Task ArgumentSubcommandCommandAsync(InteractionContext ctx, TestChoices choice, int age, string name, bool female, DiscordUser user, DiscordChannel channel, DiscordRole role) { var response = new InteractionResponseBuilder() .WithType(InteractionResponseType.ChannelMessage) .WithData(new InteractionApplicationCommandCallbackDataBuilder() .WithEmbed(new DiscordEmbedBuilder() .WithTitle("Testing Arguments!") .WithDescription($"Choice: {choice}\n" + $"Age: {age}\n" + $"Name: {name}\n" + $"Female? {female}\n" + $"User: {user.Username}\n" + $"Channel: {channel.Name}\n" + $"Role: {role.Name}")) .WithContent("How's Life?")); await ctx.ReplyAsync(response.Build()); }