static async Task InteractionHandler(DiscordClient s, InteractionCreateEventArgs e) { await e.Interaction.CreateResponseAsync( InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder { Content = "soon tm" } ); }
private Task OnInteractionCreated(DiscordClient sender, InteractionCreateEventArgs e) { _ = Task.Run(async() => { try { await _commandHandler.HandleAsync(e.Interaction); } catch (Exception ex) { _logger.LogError(ex, "Handling of interaction {InteractionId} failed", e.Interaction.Id); } }); return(Task.CompletedTask); }
public async Task <bool> HandleGatewayEvent(DiscordClient client, InteractionCreateEventArgs args) { await _slash.HandleInteraction(client, args.Interaction, this); var data = GetDeafultResponse().Build(); var msg = new HttpRequestMessage() { Method = HttpMethod.Post, RequestUri = GetGatewayFollowupUri(args.Interaction.Id.ToString(), args.Interaction.Token), Content = new StringContent(JsonConvert.SerializeObject(data, _jsonSettings)) }; msg.Content.Headers.ContentType = new(_contentType); var res = await _http.SendAsync(msg); return(res.IsSuccessStatusCode); }
public static async Task Button_Roles(object Client, InteractionCreateEventArgs e) { if (e.Interaction.Type == InteractionType.Component && !e.Interaction.User.IsBot && e.Interaction.Guild != null) { var ButtonRoleInfo = DB.DBLists.ButtonRoles.Where(w => w.Server_ID == e.Interaction.GuildId && w.Channel_ID == e.Interaction.ChannelId && e.Interaction.Guild.Roles.Any(f => f.Value.Id == Convert.ToUInt64(w.Button_ID))).ToList(); if (ButtonRoleInfo.Count > 0) { DiscordMember member = e.Interaction.User as DiscordMember; if (member.Roles.Any(w => w.Id == Convert.ToUInt64(e.Interaction.Data.CustomId))) { await member.RevokeRoleAsync(e.Interaction.Guild.Roles.FirstOrDefault(w => w.Value.Id == Convert.ToUInt64(e.Interaction.Data.CustomId)).Value); } else { await member.GrantRoleAsync(e.Interaction.Guild.Roles.FirstOrDefault(w => w.Value.Id == Convert.ToUInt64(e.Interaction.Data.CustomId)).Value); } await e.Interaction.CreateResponseAsync(InteractionResponseType.DefferedMessageUpdate); } } }
private async Task Discord_InteractionCreated(DiscordClient sender, InteractionCreateEventArgs e) { if (e.Interaction.Type != InteractionType.AutoComplete) { return; } this.Discord.Logger.LogInformation($"AutoComplete: Focused: {e.Interaction.Data.Options.First().Focused}, Data: {e.Interaction.Data.Options.First().Value}"); var option = e.Interaction.Data.Options.First(); if (string.IsNullOrEmpty(option.Value as string)) { return; } var builder = new DiscordInteractionResponseBuilder() .AddAutoCompleteChoice(new DiscordAutoCompleteChoice(option.Value as string, "pog ig")); await e.Interaction.CreateResponseAsync(InteractionResponseType.AutoCompleteResult, builder); return; }
//Handler internal Task InteractionHandler(DiscordClient client, InteractionCreateEventArgs e) { _ = Task.Run(async() => { InteractionContext context = new InteractionContext { Interaction = e.Interaction, Channel = e.Interaction.Channel, Guild = e.Interaction.Guild, User = e.Interaction.User, Client = client, SlashCommandsExtension = this, CommandName = e.Interaction.Data.Name, InteractionId = e.Interaction.Id, Token = e.Interaction.Token }; try { var methods = CommandMethods.Where(x => x.Id == e.Interaction.Data.Id); var groups = GroupCommands.Where(x => x.Id == e.Interaction.Data.Id); var subgroups = SubGroupCommands.Where(x => x.Id == e.Interaction.Data.Id); if (!methods.Any() && !groups.Any() && !subgroups.Any()) { throw new SlashCommandNotFoundException("An interaction was created, but no command was registered for it"); } if (methods.Any()) { var method = methods.First(); List <object> args = new List <object> { context }; var parameters = method.Method.GetParameters().Skip(1); for (int i = 0; i < parameters.Count(); i++) { var parameter = parameters.ElementAt(i); if (parameter.IsOptional && (e.Interaction.Data.Options == null || e.Interaction.Data.Options?.ElementAtOrDefault(i) == default)) { args.Add(parameter.DefaultValue); } else { var option = e.Interaction.Data.Options.ElementAt(i); if (ReferenceEquals(parameter.ParameterType, typeof(string))) { args.Add(option.Value.ToString()); } else if (ReferenceEquals(parameter.ParameterType, typeof(long))) { args.Add((long)option.Value); } else if (ReferenceEquals(parameter.ParameterType, typeof(bool))) { args.Add((bool)option.Value); } else if (ReferenceEquals(parameter.ParameterType, typeof(DiscordUser))) { if (e.Interaction.Data.Resolved.Members.TryGetValue((ulong)option.Value, out var member)) { args.Add(member); } else if (e.Interaction.Data.Resolved.Users.TryGetValue((ulong)option.Value, out var user)) { args.Add(user); } else { args.Add(await Client.GetUserAsync((ulong)option.Value)); } } else if (ReferenceEquals(parameter.ParameterType, typeof(DiscordChannel))) { if (e.Interaction.Data.Resolved.Channels.TryGetValue((ulong)option.Value, out var channel)) { args.Add(channel); } else { args.Add(e.Interaction.Guild.GetChannel((ulong)option.Value)); } } else if (ReferenceEquals(parameter.ParameterType, typeof(DiscordRole))) { if (e.Interaction.Data.Resolved.Roles.TryGetValue((ulong)option.Value, out var role)) { args.Add(role); } else { args.Add(e.Interaction.Guild.GetRole((ulong)option.Value)); } } else { throw new ArgumentException($"How on earth did that happen"); } } } var classinstance = Activator.CreateInstance(method.ParentClass); var task = (Task)method.Method.Invoke(classinstance, args.ToArray()); await task; } else if (groups.Any()) { var command = e.Interaction.Data.Options.First(); var method = groups.First().Methods.First(x => x.Key == command.Name).Value; List <object> args = new List <object> { context }; var parameters = method.GetParameters().Skip(1); for (int i = 0; i < parameters.Count(); i++) { var parameter = parameters.ElementAt(i); if (parameter.IsOptional && (command.Options == null || command.Options?.ElementAtOrDefault(i) == default)) { args.Add(parameter.DefaultValue); } else { var option = command.Options.ElementAt(i); if (ReferenceEquals(parameter.ParameterType, typeof(string))) { args.Add(option.Value.ToString()); } else if (ReferenceEquals(parameter.ParameterType, typeof(long))) { args.Add((long)option.Value); } else if (ReferenceEquals(parameter.ParameterType, typeof(bool))) { args.Add((bool)option.Value); } else if (ReferenceEquals(parameter.ParameterType, typeof(DiscordUser))) { if (e.Interaction.Data.Resolved.Members.TryGetValue((ulong)option.Value, out var member)) { args.Add(member); } else if (e.Interaction.Data.Resolved.Users.TryGetValue((ulong)option.Value, out var user)) { args.Add(user); } else { args.Add(await Client.GetUserAsync((ulong)option.Value)); } } else if (ReferenceEquals(parameter.ParameterType, typeof(DiscordChannel))) { if (e.Interaction.Data.Resolved.Channels.TryGetValue((ulong)option.Value, out var channel)) { args.Add(channel); } else { args.Add(e.Interaction.Guild.GetChannel((ulong)option.Value)); } } else if (ReferenceEquals(parameter.ParameterType, typeof(DiscordRole))) { if (e.Interaction.Data.Resolved.Roles.TryGetValue((ulong)option.Value, out var role)) { args.Add(role); } else { args.Add(e.Interaction.Guild.GetRole((ulong)option.Value)); } } else { throw new ArgumentException($"How on earth did that happen"); } } } var classinstance = Activator.CreateInstance(groups.First().ParentClass); var task = (Task)method.Invoke(classinstance, args.ToArray()); await task; } else if (subgroups.Any()) { var command = e.Interaction.Data.Options.First(); var group = subgroups.First(x => x.SubCommands.Any(y => y.Name == command.Name)).SubCommands.First(x => x.Name == command.Name); var method = group.Methods.First(x => x.Key == command.Options.First().Name).Value; List <object> args = new List <object> { context }; var parameters = method.GetParameters().Skip(1); for (int i = 0; i < parameters.Count(); i++) { var parameter = parameters.ElementAt(i); if (parameter.IsOptional && (command.Options == null || command.Options?.ElementAtOrDefault(i) == default)) { args.Add(parameter.DefaultValue); } else { var option = command.Options.ElementAt(i); if (ReferenceEquals(parameter.ParameterType, typeof(string))) { args.Add(option.Value.ToString()); } else if (ReferenceEquals(parameter.ParameterType, typeof(long))) { args.Add((long)option.Value); } else if (ReferenceEquals(parameter.ParameterType, typeof(bool))) { args.Add((bool)option.Value); } else if (ReferenceEquals(parameter.ParameterType, typeof(DiscordUser))) { if (e.Interaction.Data.Resolved.Members.TryGetValue((ulong)option.Value, out var member)) { args.Add(member); } else if (e.Interaction.Data.Resolved.Users.TryGetValue((ulong)option.Value, out var user)) { args.Add(user); } else { args.Add(await Client.GetUserAsync((ulong)option.Value)); } } else if (ReferenceEquals(parameter.ParameterType, typeof(DiscordChannel))) { if (e.Interaction.Data.Resolved.Channels.TryGetValue((ulong)option.Value, out var channel)) { args.Add(channel); } else { args.Add(e.Interaction.Guild.GetChannel((ulong)option.Value)); } } else if (ReferenceEquals(parameter.ParameterType, typeof(DiscordRole))) { if (e.Interaction.Data.Resolved.Roles.TryGetValue((ulong)option.Value, out var role)) { args.Add(role); } else { args.Add(e.Interaction.Guild.GetRole((ulong)option.Value)); } } else { throw new ArgumentException($"How on earth did that happen"); } } } var classinstance = Activator.CreateInstance(group.ParentClass); var task = (Task)method.Invoke(classinstance, args.ToArray()); await task; } await _executed.InvokeAsync(this, new SlashCommandExecutedEventArgs { Context = context }); } catch (Exception ex) { await _error.InvokeAsync(this, new SlashCommandErrorEventArgs { Context = context, Exception = ex }); } }); return(Task.CompletedTask); }
private async Task <List <object> > ResloveInteractionCommandParameters(InteractionCreateEventArgs e, InteractionContext context, MethodInfo method) { List <object> args = new List <object> { context }; var parameters = method.GetParameters().Skip(1); for (int i = 0; i < parameters.Count(); i++) { var parameter = parameters.ElementAt(i); if (parameter.IsOptional && (e.Interaction.Data.Options == null || e.Interaction.Data.Options?.ElementAtOrDefault(i) == default)) { args.Add(parameter.DefaultValue); } else { var option = e.Interaction.Data.Options.ElementAt(i); if (ReferenceEquals(parameter.ParameterType, typeof(string))) { args.Add(option.Value.ToString()); } else if (ReferenceEquals(parameter.ParameterType, typeof(long))) { args.Add((long)option.Value); } else if (ReferenceEquals(parameter.ParameterType, typeof(bool))) { args.Add((bool)option.Value); } else if (ReferenceEquals(parameter.ParameterType, typeof(DiscordUser))) { if (e.Interaction.Data.Resolved.Members != null && e.Interaction.Data.Resolved.Members.TryGetValue((ulong)option.Value, out var member)) { args.Add(member); } else if (e.Interaction.Data.Resolved.Users != null && e.Interaction.Data.Resolved.Users.TryGetValue((ulong)option.Value, out var user)) { args.Add(user); } else { args.Add(await Client.GetUserAsync((ulong)option.Value)); } } else if (ReferenceEquals(parameter.ParameterType, typeof(DiscordChannel))) { if (e.Interaction.Data.Resolved.Channels != null && e.Interaction.Data.Resolved.Channels.TryGetValue((ulong)option.Value, out var channel)) { args.Add(channel); } else { args.Add(e.Interaction.Guild.GetChannel((ulong)option.Value)); } } else if (ReferenceEquals(parameter.ParameterType, typeof(DiscordRole))) { if (e.Interaction.Data.Resolved.Roles != null && e.Interaction.Data.Resolved.Roles.TryGetValue((ulong)option.Value, out var role)) { args.Add(role); } else { args.Add(e.Interaction.Guild.GetRole((ulong)option.Value)); } } else { throw new ArgumentException($"How on earth did that happen"); } } } return(args); }
//Handler private Task InteractionHandler(DiscordClient client, InteractionCreateEventArgs e) { _ = Task.Run(async() => { InteractionContext context = new InteractionContext { Interaction = e.Interaction, Channel = e.Interaction.Channel, Guild = e.Interaction.Guild, User = e.Interaction.User, Client = client, SlashCommandsExtension = this, CommandName = e.Interaction.Data.Name, InteractionId = e.Interaction.Id, Token = e.Interaction.Token, Services = _configuration?.Services }; try { var methods = CommandMethods.Where(x => x.Id == e.Interaction.Data.Id); var groups = GroupCommands.Where(x => x.Id == e.Interaction.Data.Id); var subgroups = SubGroupCommands.Where(x => x.Id == e.Interaction.Data.Id); if (!methods.Any() && !groups.Any() && !subgroups.Any()) { throw new Exception("An interaction was created, but no command was registered for it"); } if (methods.Any()) { var method = methods.First(); var args = await ResloveInteractionCommandParameters(e, context, method.Method); var classinstance = ActivatorUtilities.CreateInstance(_configuration?.Services, method.ParentClass); var task = (Task)method.Method.Invoke(classinstance, args.ToArray()); await task; } else if (groups.Any()) { var command = e.Interaction.Data.Options.First(); var method = groups.First().Methods.First(x => x.Key == command.Name).Value; var args = await ResloveInteractionCommandParameters(e, context, method); var classinstance = ActivatorUtilities.CreateInstance(_configuration?.Services, groups.First().ParentClass); var task = (Task)method.Invoke(classinstance, args.ToArray()); await task; } else if (subgroups.Any()) { var command = e.Interaction.Data.Options.First(); var group = subgroups.First(x => x.SubCommands.Any(y => y.Name == command.Name)).SubCommands.First(x => x.Name == command.Name); var method = group.Methods.First(x => x.Key == command.Options.First().Name).Value; var args = await ResloveInteractionCommandParameters(e, context, method); var classinstance = ActivatorUtilities.CreateInstance(_configuration?.Services, group.ParentClass); var task = (Task)method.Invoke(classinstance, args.ToArray()); await task; } await _executed.InvokeAsync(this, new SlashCommandExecutedEventArgs { Context = context }); } catch (Exception ex) { await _error.InvokeAsync(this, new SlashCommandErrorEventArgs { Context = context, Exception = ex }); } }); return(Task.CompletedTask); }
private Task Event_InteractionCreated(DiscordClient d, InteractionCreateEventArgs e) { d.Logger.LogDebug(BotEventId, $"{e.Interaction.User.Username} started an interaction. {e.Interaction.Data.Name}"); return(Task.CompletedTask); }