public object Parse(AdaCommandContext ctx, string value, Type type) { var dlg = this.GetDelegate(type); var dft = type.GetTypeInfo().IsValueType ? Activator.CreateInstance(type) : null; if (dlg == null) { throw new ArgumentException("Invalid value specified."); } var mtd = dlg.GetMethodInfo(); var arg = (object[])null; if (dlg.GetType().GetGenericTypeDefinition() == typeof(TryParseDelegate <>)) { arg = new object[] { value, dft } } ; else if (dlg.GetType().GetGenericTypeDefinition() == typeof(ContextParseDelegate <>)) { arg = new object[] { ctx, value, dft } } ; if (!(bool)mtd.Invoke(null, arg)) { throw new ArgumentException("Invalid value specified."); } return(arg[arg.Length - 1]); }
private static bool TryParseRole(AdaCommandContext ctx, string value, out IRole result) { result = null; if (value.StartsWith("<@&") && value.EndsWith(">")) { var rlid = value.Substring(3, value.Length - 4); if (!rlid.All(xc => Char.IsNumber(xc))) { return(false); } var id = ulong.Parse(rlid); result = ctx.Guild.GetRole(id); return(true); } else { var rl = ctx.Guild.Roles.FirstOrDefault(xr => xr.Name == value); result = rl; if (rl == null) { return(false); } return(true); } }
private static bool TryParseChannel(AdaCommandContext ctx, string value, out ITextChannel result) { result = null; if (!value.StartsWith("<#") || !value.EndsWith(">")) { return(false); } var chnid = value.Substring(2, value.Length - 3); if (!chnid.All(xc => Char.IsNumber(xc))) { return(false); } var id = ulong.Parse(chnid); var chn = ctx.Guild.GetChannelAsync(id).GetAwaiter().GetResult() as ITextChannel; result = chn; if (chn == null) { return(false); } return(true); }
private object[] PrepareArguments(AdaCommandContext ctx) { var prms = this.Parameters.Where(xp => xp.IsFunctionArgument).OrderBy(xp => xp.Order); var args = new object[prms.Count() + 1]; args[0] = ctx; foreach (var prm in prms) { if (prm.IsCatchAll) { if (!prm.ParameterType.IsArray) { throw new InvalidOperationException("Parameter is catchall but not an array."); } var ags = ctx.RawArguments .Skip(prm.Order) .Select(xa => AdaBotCore.CommandManager.ParameterParser.Parse(ctx, xa, prm.ParameterType.GetElementType())) .ToArray(); var agt = Array.CreateInstance(prm.ParameterType.GetElementType(), ags.Length); Array.Copy(ags, agt, agt.Length); args[prm.Order + 1] = agt; break; } else { if (prm.ParameterType.IsArray) { throw new InvalidOperationException("Parameter is not catchall but an array."); } if (prm.IsRequired && ctx.RawArguments.Count < prm.Order + 1) { throw new ArgumentException(string.Concat("Parameter ", prm.Name, " is required.")); } else if (!prm.IsRequired && ctx.RawArguments.Count < prm.Order + 1) { break; } var arg = ctx.RawArguments[prm.Order]; var val = AdaBotCore.CommandManager.ParameterParser.Parse(ctx, arg, prm.ParameterType); args[prm.Order + 1] = val; } } return(args); }
public T Parse <T>(AdaCommandContext ctx, string value) { var dlg = this.GetDelegate <T>(); if (dlg == null) { throw new ArgumentException("Invalid value specified."); } var rtv = default(T); if (!dlg(value, out rtv)) { throw new ArgumentException("Invalid value specified."); } return(rtv); }
private static bool TryParseUser(AdaCommandContext ctx, string value, out IUser result) { result = null; if (!value.StartsWith("<@") || !value.EndsWith(">")) { return(false); } var shift = value.StartsWith("<@!") ? 1 : 0; var usrid = value.Substring(2 + shift, value.Length - (3 + shift)); if (!usrid.All(xc => Char.IsNumber(xc))) { return(false); } var id = ulong.Parse(usrid); result = ctx.Guild.GetUserAsync(id).GetAwaiter().GetResult(); return(true); }
internal async Task Execute(AdaCommandContext context) { var error = (string)null; var canrun = false; if (this.Checker == null) { canrun = true; } else { canrun = this.Checker.CanRun(this, context.User, context.Message, context.Channel, context.Guild, out error); } if (canrun) { await(Task) this.Function.DynamicInvoke(PrepareArguments(context)); } else { throw new UnauthorizedAccessException(error); } }
private void CommandExecuted(AdaCommandContext ctx) { L.W("DSC CMD", "User '{0}#{1}' executed command '{2}' on server '{3}' ({4})", ctx.User.Username, ctx.User.Discriminator, ctx.Command.Name, ctx.Guild.Name, ctx.Guild.Id); }
private async Task HandleCommand(SocketMessage arg) { await Task.Delay(1); var msg = arg as SocketUserMessage; if (msg == null || msg.Author == null || msg.Author.IsBot) { return; } var chn = msg.Channel as SocketTextChannel; if (chn == null) { return; } var gld = chn.Guild; if (gld == null) { return; } var client = AdaBotCore.AdaClient.DiscordClient; var argpos = 0; var gconf = AdaBotCore.ConfigManager.GetGuildConfig(gld.Id); var cprefix = "/"; if (client.CurrentUser.Id != 207900508562653186u) { cprefix = "?"; } if (gconf != null && gconf.CommandPrefix != null) { cprefix = gconf.CommandPrefix; } if (msg.HasStringPrefix(cprefix, ref argpos) || msg.HasMentionPrefix(client.CurrentUser, ref argpos)) { var cmdn = msg.Content.Substring(argpos); var argi = cmdn.IndexOf(' '); if (argi == -1) { argi = cmdn.Length; } var args = cmdn.Substring(argi).Trim(); cmdn = cmdn.Substring(0, argi); var cmd = this.GetCommand(cmdn); if (cmd == null) { return; } var ctx = new AdaCommandContext(); ctx.Message = msg; ctx.Command = cmd; ctx.RawArguments = this.ParseArgumentList(args); var t = Task.Run(async() => { try { if (gconf.DeleteCommands != null && gconf.DeleteCommands.Value) { await msg.DeleteAsync(); } await cmd.Execute(ctx); this.CommandExecuted(ctx); } catch (Exception ex) { this.CommandError(new AdaCommandErrorContext { Context = ctx, Exception = ex }); } }); } }