示例#1
0
 public Commands(InteractionCommandContext context) : base(context)
 {
     Webhooks = Program.Services.GetRequiredService <WebhookService>();
 }
示例#2
0
        void executeCommand(Interaction interaction)
        {
            var context = new InteractionCommandContext(interaction);

            context.Channel = (IMessageChannel)Program.Client.GetChannel(interaction.ChannelId);
            context.Guild   = Program.Client.GetGuild(interaction.GuildId);
            context.User    = Program.Client.GetUser(interaction.Member.User.Id);
            context.BotUser = Program.GetUser(context.User);

            var type        = typeof(InteractionBase);
            var moduleTypes = Assembly.GetAssembly(type).GetTypes()
                              .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(type));
            MethodInfo method = null;

            foreach (var module in moduleTypes)
            {
                var commands = module.GetMethods()
                               .Where(x => x.ReturnType == typeof(Task));
                foreach (var cmd in commands)
                {
                    var ids = cmd.GetCustomAttributes <IdAttribute>();
                    if (ids != null && ids.Any(id => id.Id == interaction.Data.Id))
                    {
                        Program.LogMsg($"Found cmd: {cmd.Name}");
                        method = cmd;
                        break;
                    }
                }
                if (method != null)
                {
                    break;
                }
            }
            if (method == null)
            {
                return;
            }
            var obj = Activator.CreateInstance(method.DeclaringType, new object[1] {
                context
            });
            var options    = interaction.Data.Options ?? new ApplicationCommandInteractionDataOption[0];
            var args       = new List <object>();
            var paramaters = method.GetParameters();

            foreach (var param in paramaters)
            {
                var    option = options.FirstOrDefault(x => x.Name == param.Name);
                object value  = option?.Value ?? null;
                Program.LogMsg($"For {param.Name}: {value.GetType().Name} {value}", LogSeverity.Verbose);
                if (value == null && param.IsOptional == false)
                {
                    throw new InvalidOperationException($"No argument specified for required item {param.Name}");
                }
                if (value == null)
                {
                    Program.LogMsg($"For {param.Name}: Adding default value", LogSeverity.Verbose);
                    args.Add(param.DefaultValue);
                    continue;
                }
                if (param.ParameterType == value.GetType())
                {
                    Program.LogMsg($"For {param.Name}: Types match", LogSeverity.Verbose);
                    args.Add(value);
                }
                else
                {
                    var typeResult = Program.AttemptParseInput($"{value}", param.ParameterType);
                    if (typeResult.IsSuccess)
                    {
                        args.Add(typeResult.BestMatch);
                    }
                    else
                    {
                        throw new InvalidOperationException($"Could not parse value for {param.Name} as {param.ParameterType.Name}: {typeResult.ErrorReason}");
                    }
                }
            }
            Program.LogMsg($"Invoking cmd with {args.Count} args");
            try
            {
                method.Invoke(obj, args.ToArray());
            }
            catch (TargetInvocationException outer)
            {
                Exception ex = outer.InnerException;
                try
                {
                    RespondRaw(Program.Serialise(
                                   new InteractionResponse(InteractionResponseType.ChannelMessage, "Error: " + ex.Message)));
                }
                catch { }
                Program.LogMsg(ex, "CmdInteraction");
            }
            catch (Exception ex)
            {
                try
                {
                    RespondRaw(Program.Serialise(
                                   new InteractionResponse(InteractionResponseType.ChannelMessage, "Error: " + ex.Message)));
                }
                catch { }
                Program.LogMsg(ex, "ExCmdInt");
            }
        }
 public InteractionBase(InteractionCommandContext context)
 {
     Context = context;
 }