Exemplo n.º 1
0
 /// <summary>
 /// Called after the execution of a slash command in the module.
 /// </summary>
 /// <param name="ctx">The context.</param>
 /// <returns></returns>
 public virtual Task AfterSlashExecutionAsync(InteractionContext ctx)
 => Task.CompletedTask;
Exemplo n.º 2
0
 /// <summary>
 /// Called before the execution of a slash command in the module.
 /// </summary>
 /// <param name="ctx">The context.</param>
 /// <returns> Whether or not to execute the slash command.</returns>
 public virtual Task <bool> BeforeSlashExecutionAsync(InteractionContext ctx)
 => Task.FromResult(true);
        //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);
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        //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);
        }
Exemplo n.º 6
0
 /// <summary>
 /// Checks whether this command can be executed within the current context.
 /// </summary>
 /// <param name="ctx">The context.</param>
 /// <returns>Whether the checks passed.</returns>
 public abstract Task <bool> ExecuteChecksAsync(InteractionContext ctx);