public Task <ConditionResult> VerifyAsync(ICommandContextEx context) { // Verify that a tag has been supplied where necessary and that if (RequireBotTag && !context.IsBotUserTagged) { return(Task.FromResult(ConditionResult.FromError("Invalid use of command, @tag required.", true))); } // Tagged commands without a prefix value. else if (!context.IsProperCommand && !AcceptBotTag && context.IsBotUserTagged) { return(Task.FromResult(ConditionResult.FromError("Invalid use of command, @tag is not accepted.", true))); } var valid = false; if (!valid && SourceLevel.HasFlag(CommandSourceLevel.Guild)) { valid = context.Channel is IGuildChannel; } if (!valid && SourceLevel.HasFlag(CommandSourceLevel.Group)) { valid = context.Channel is IGroupChannel; } if (!valid && SourceLevel.HasFlag(CommandSourceLevel.DM)) { valid = context.Channel is IDMChannel; } return(Task.FromResult(valid ? ConditionResult.FromSuccess() : ConditionResult.FromError($"Invalid context for command; accepted contexts: {SourceLevel}", true))); }
/// <summary> /// Validate lua code from memory /// </summary> public static bool Validate(string luaCode, ICommandContextEx context) { try { if (CreateLuaScript(out LuaScript luaScript, null, null, context)) { var value = luaScript.Script.LoadString(luaCode); return(true); } } catch (Exception) { } return(false); }
public static Task <bool> CanBotManageRoles(ICommandContextEx context) => CanBotManageRoles(context?.Guild);
/// <summary> /// Determines wether the bot user has the manage messages permission. /// </summary> public static Task <bool> CanBotManageMessages(ICommandContextEx context) => CanBotManageMessages(context.TextChannel);
public static bool CanManageMessages(ICommandContextEx context) => CanManageMessages(context.GuildUser, context.TextChannel);
/// <summary> /// Determines wether the user has administrator permissions or is the owner of the bot. /// </summary> public static bool IsAdministratorOrBotOwner(ICommandContextEx context) => IsAdministrator(context) || IsBotOwner(context);
public static bool IsAdministrator(ICommandContextEx context) => IsAdministrator(context.GuildUser);
public static bool IsBotOwner(ICommandContextEx context) => IsBotOwner(context.User);
//public static DynValue Run(string luaCode, ICommandContextEx context) //{ // return Run(CreateLuaScript(luaCode, null, context)); //} //public static Task<DynValue> RunAsync(string luaCode, ICommandContextEx context) //{ // return Task.Run(() => Run(luaCode, context)); //} public static bool CreateLuaScript(out LuaScript luaScript, string luaCode, string fileName, ICommandContextEx context) { luaScript = new LuaScript { Guild = context?.Guild, Code = null, //Code = CleanupCode(luaCode), //Script = new Script(CoreModules.Preset_HardSandbox), Script = new Script(), //Channel = context?.Channel as ITextChannel, FileName = fileName, Lua = new LuaDiscord() { User = context?.User, Channel = context?.Channel, Role = null } }; SetScriptVariables(luaScript); luaCode = CleanupCode(luaCode); if (fileName != null) { //luaScript.Script.Options.ScriptLoader = new FileSystemScriptLoader(); // Add data to our code luaCode = $@"function Initialise() ChannelId = '{luaScript?.Lua?.Channel?.Id ?? 0}'; end {luaCode}"; // Validate our code if (!Validate(luaCode, context)) { return(false); } // Create and write to file Directory.CreateDirectory(Path.GetDirectoryName(luaScript.FilePath)); if (File.Exists(luaScript.FilePath)) { File.Delete(luaScript.FilePath); } using (var sw = new StreamWriter(File.Open(luaScript.FilePath, FileMode.OpenOrCreate), Encoding.UTF8)) { sw.Write(luaCode); } } else { luaScript.Code = luaCode; } return(true); }
/// <summary> /// Validate lua code from memory /// </summary> public static Task <bool> ValidateAsync(string luaCode, ICommandContextEx context) { return(Task.Run(() => Validate(luaCode, context))); }
/// <summary> /// Parses commands that accept the provided <paramref name="input"/> string as the method name and arguments. /// </summary> /// <returns>A sorted parse result list based on priority and successful parameters.</returns> public async Task <IEnumerable <ParseResult> > ParseMethodsAsync(ICommandContextEx context, string input, bool validateInput = true) { var list = new List <ParseResult>(); foreach (var module in (context.Guild == null ? CommandHandler.Modules[0] : CommandHandler.Modules[context.Guild.Id])) { var parseResults = ParseMethodsInternal(module, input, ParsingState.BASE); foreach (var parseResult in parseResults) { if (parseResult != null && parseResult.Method != null && parseResult.InputMessage != null) { var score = new Tuple <int?, List <object[]> >(null, null); string errorMessage = null; try { var commandName = parseResult.InputMessage; //if (parseResult.Method.MethodInfo?.Name == "_") if (parseResult.Method.Accessibility.Has(CommandAccessLevel.Global)) { var firstCommand = commandName.Split(' ').FirstOrDefault(); if (firstCommand != null && firstCommand.Length > 0) { var firstAlias = parseResult.Method.Aliases.Where(a => a != "_").FirstOrDefault(e => e.Equals(firstCommand, StringComparison.CurrentCultureIgnoreCase)); if (firstAlias == null) { // add underscore commandName = "_ " + commandName; } } } score = await CommandScorer.GetMethodScoreAndParametersAsync( context, parseResult.Method.MethodInfo, commandName ).ConfigureAwait(false); } catch (Exception ex) { errorMessage = ex.Message; } // Validate command - part 1: if guild == null, verify that the command accepts non-guild sources if (validateInput) { if (context.Guild == null && !(parseResult.Method.Source.Has(CommandSourceLevel.DM) || parseResult.Method.Source.Has(CommandSourceLevel.Group))) { continue; } // Validate command - part 2, check the command settings try { if ((await(parseResult.Method.MethodInfo.GetCustomAttribute(typeof(DiscordCommandAttribute)) as DiscordCommandAttribute).VerifyAsync(context)).HasError) { throw new Exception(); } } catch { continue; } } list.Add(new ParseResult() { InputMessage = parseResult.InputMessage, Method = parseResult.Method, Module = parseResult.Module, Parameters = score.Item2, Score = score.Item1 ?? -1, Priority = parseResult.Priority, ErrorMessage = errorMessage }); } } } return(GetSortedResults(list)); }