示例#1
0
        public static async Task <LoadFileOperation> LoadToJSONObject(string path)
        {
            LoadFileOperation operation = new LoadFileOperation()
            {
                Success = false,
                Result  = null
            };

            if (File.Exists(path))
            {
                string fileContent = "";
                try
                {
                    fileContent = await File.ReadAllTextAsync(path, Encoding.UTF8);

                    operation.Success = JSONContainer.TryParse(fileContent, out operation.Result, out string error);
                    return(operation);
                }
                catch (Exception e)
                {
                    await YNBBotCore.Logger(new Discord.LogMessage(Discord.LogSeverity.Critical, "Save/Load", "Failed to load " + path, e));
                }
            }
            return(operation);
        }
        public static async Task <RequestJSONResult> GetWebJSONAsync(string url)
        {
            RequestJSONResult loadresult = new RequestJSONResult();

            try
            {
                using (HttpRequestMessage requestmessage = new HttpRequestMessage(HttpMethod.Get, url))
                {
                    requestmessage.Version = new Version(1, 1);
                    using (HttpResponseMessage responsemessage = await httpClient.SendAsync(requestmessage))
                    {
                        loadresult.Status    = responsemessage.StatusCode;
                        loadresult.IsSuccess = responsemessage.IsSuccessStatusCode;
                        if (responsemessage.IsSuccessStatusCode)
                        {
                            loadresult.rawData = await responsemessage.Content.ReadAsStringAsync();

                            JSONContainer.TryParse(loadresult.rawData, out loadresult.JSON, out loadresult.jsonParseError);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                loadresult.IsException     = true;
                loadresult.ThrownException = e;
            }
            return(loadresult);
        }
示例#3
0
        protected override Task <ArgumentParseResult> ParseArgumentsGuildAsync(IGuildCommandContext context)
        {
            ArgumentContainer argOut = new ArgumentContainer();

            if (!ArgumentParsing.TryParseGuildTextChannel(context, context.Arguments.First, out argOut.channel))
            {
                return(Task.FromResult(new ArgumentParseResult(Arguments[0], "Failed to parse to a guild text channel!")));
            }

            if (context.Message.Content.Length > Identifier.Length + context.Arguments.First.Length + 2)
            {
                context.Arguments.Index++;
                string embedText = context.RemoveArgumentsFront(1).Replace("[3`]", "```");

                if (JSONContainer.TryParse(embedText, out JSONContainer json, out string errormessage))
                {
                    if (EmbedHelper.TryGetMessageFromJSONObject(json, out argOut.embed, out argOut.messageContent, out string error))
                    {
                        return(Task.FromResult(new ArgumentParseResult(argOut)));
                    }
                    else
                    {
                        return(Task.FromResult(new ArgumentParseResult(Arguments[1], error)));
                    }
                }
                else
                {
                    return(Task.FromResult(new ArgumentParseResult(Arguments[1], $"Unable to parse JSON text to a json data structure! Error: `{errormessage}`")));
                }
            }
示例#4
0
        protected override Task <ArgumentParseResult> ParseArguments(IDMCommandContext context)
        {
            if (context.Message.Content.Length > Identifier.Length + 1)
            {
                string embedText = context.ArgumentSection.Replace("[3`]", "```");

                if (JSONContainer.TryParse(embedText, out JSONContainer json, out string errormessage))
                {
                    return(Task.FromResult(EmbedHelper.TryParseEmbedFromJSONObject(json, out embed, out messageContent)));
                }
                else
                {
                    return(Task.FromResult(new ArgumentParseResult(Arguments[0], $"Unable to parse JSON text to a json data structure! Error: `{errormessage}`")));
                }
            }
示例#5
0
        public static async Task <JSONContainer> LoadJSONFile(string path)
        {
            if (File.Exists(path))
            {
                try
                {
                    string filecontent = await File.ReadAllTextAsync(path);

                    if (JSONContainer.TryParse(filecontent, out JSONContainer result, out string jsonerror))
                    {
                        return(result);
                    }
                    else
                    {
                        await BotCore.Log(new LogMessage(LogSeverity.Error, "RESOURCES", $"Couldn't load \"{path}\"! JSON parsing error: {jsonerror}"));

                        return(null);
                    }
                }
示例#6
0
        protected override async Task <ArgumentParseResult> ParseArgumentsGuildAsync(IGuildCommandContext context)
        {
            MacroIdentifier = context.Arguments.First;

            if (!StoredMessagesService.IsValidMacroName(MacroIdentifier))
            {
                return(new ArgumentParseResult(Arguments[0], "Not a valid macro name!"));
            }

            context.Arguments.Index++;

            if (context.Arguments.First.ToLower() == "remove")
            {
                Delete        = true;
                SelectedMacro = null;
                return(ArgumentParseResult.SuccessfullParse);
            }
            else
            {
                Delete = false;
                JSONContainer json;
                if (context.Arguments.First.StartsWith("http"))
                {
                    string[] argSections = context.Arguments.First.Split("/", StringSplitOptions.RemoveEmptyEntries);
                    if (argSections.Length < 3)
                    {
                        return(new ArgumentParseResult(Arguments[1]));
                    }

                    if (!(ulong.TryParse(argSections[argSections.Length - 3], out ulong guildId) && ulong.TryParse(argSections[argSections.Length - 2], out ulong channelId) && ulong.TryParse(argSections[argSections.Length - 1], out ulong messageId)))
                    {
                        return(new ArgumentParseResult(Arguments[1]));
                    }

                    SocketGuild guild = BotCore.Client.GetGuild(guildId);
                    if (guild == null)
                    {
                        return(new ArgumentParseResult(Arguments[1]));
                    }

                    SocketTextChannel channel = guild.GetTextChannel(channelId);
                    if (channel == null)
                    {
                        return(new ArgumentParseResult(Arguments[1]));
                    }

                    IMessage message = await channel.GetMessageAsync(messageId);

                    if (message == null)
                    {
                        return(new ArgumentParseResult(Arguments[1]));
                    }

                    EmbedHelper.GetJSONFromUserMessage(message, out json);
                    SelectedMacro = new Macro(MacroIdentifier, json);
                }
                else
                {
                    string embedText = context.RemoveArgumentsFront(1).Replace("[3`]", "```");
                    if (!JSONContainer.TryParse(embedText, out json, out string error))
                    {
                        return(new ArgumentParseResult(Arguments[1]));
                    }

                    SelectedMacro = new Macro(MacroIdentifier, json);
                    if (!SelectedMacro.Build(out _, out _, out error))
                    {
                        return(new ArgumentParseResult(Arguments[1], error));
                    }
                }

                return(ArgumentParseResult.SuccessfullParse);
            }
        }
示例#7
0
        protected override Task <ArgumentParseResult> ParseArguments(IDMCommandContext context)
        {
            if (context.Arguments.TotalCount == 0)
            {
                mode = CommandMode.listall;
                return(Task.FromResult(ArgumentParseResult.DefaultNoArguments));
            }

            if (context.Arguments.First.ToLower() == "save")
            {
                mode = CommandMode.save;
                return(Task.FromResult(ArgumentParseResult.SuccessfullParse));
            }

            BotVarId = context.Arguments.First;
            BotVarManager.TryGetBotVar(BotVarId, out BotVar);

            if (context.Arguments.TotalCount == 1)
            {
                mode = CommandMode.get;
                if (BotVar.IsDefined)
                {
                    return(Task.FromResult(ArgumentParseResult.SuccessfullParse));
                }
                else
                {
                    return(Task.FromResult(new ArgumentParseResult(Arguments[0], $"Couldn't locate a config variable named `{BotVarId}`!")));
                }
            }
            else
            {
                mode = CommandMode.set;
            }

            context.Arguments.Index++;

            if (context.Arguments.First.ToLower() == "delete")
            {
                mode = CommandMode.delete;
                if (!BotVar.IsDefined)
                {
                    return(Task.FromResult(new ArgumentParseResult(Arguments[0], $"Couldn't locate a config variable named `{BotVarId}`!")));
                }
                else
                {
                    return(Task.FromResult(ArgumentParseResult.SuccessfullParse));
                }
            }

            if (context.Arguments.TotalCount == 2)
            {
                return(Task.FromResult(new ArgumentParseResult(Arguments[2], "Cannot assign an empty value!")));
            }

            if (!Enum.TryParse(context.Arguments.First, true, out assignType))
            {
                return(Task.FromResult(new ArgumentParseResult(Arguments[1])));
            }

            if (assignType == BotVarType.Undefined)
            {
                return(Task.FromResult(new ArgumentParseResult(Arguments[1])));
            }

            context.Arguments.Index++;

            value = context.Arguments.First;

            switch (assignType)
            {
            case BotVarType.UInt64:
                if (ulong.TryParse(value, out ulong uint64Val))
                {
                    BotVar = new BotVar(BotVarId, uint64Val);
                }
                else
                {
                    return(Task.FromResult(new ArgumentParseResult(Arguments[2])));
                }
                break;

            case BotVarType.Int64:
                if (long.TryParse(value, out long int64Val))
                {
                    BotVar = new BotVar(BotVarId, int64Val);
                }
                else
                {
                    return(Task.FromResult(new ArgumentParseResult(Arguments[2])));
                }
                break;

            case BotVarType.Float64:
                if (double.TryParse(value, out double float64Val))
                {
                    BotVar = new BotVar(BotVarId, float64Val);
                }
                else
                {
                    return(Task.FromResult(new ArgumentParseResult(Arguments[2])));
                }
                break;

            case BotVarType.String:
                BotVar = new BotVar(BotVarId, value);
                break;

            case BotVarType.Bool:
                if (bool.TryParse(value, out bool boolVal))
                {
                    BotVar = new BotVar(BotVarId, boolVal);
                }
                else
                {
                    return(Task.FromResult(new ArgumentParseResult(Arguments[2])));
                }
                break;

            case BotVarType.Generic:
                if (JSONContainer.TryParse(value, out JSONContainer json, out string error))
                {
                    BotVar = new BotVar(BotVarId, json);
                }
                else
                {
                    return(Task.FromResult(new ArgumentParseResult(Arguments[2], error)));
                }
                break;
示例#8
0
        protected override async Task <ArgumentParseResult> ParseArgumentsGuildAsync(IGuildCommandContext context)
        {
            ArgumentContainer argOut = new ArgumentContainer();

            if (!context.Arguments.First.StartsWith("https://discordapp.com/channels/") || context.Arguments.First.Length < 40)
            {
                return(new ArgumentParseResult(Arguments[0], "Not a valid message link! Failed Startswith or length test"));
            }

            string[] messageIdentifiers = context.Arguments.First.Substring(32).Split('/');

            if (messageIdentifiers.Length != 3)
            {
                return(new ArgumentParseResult(Arguments[0], "Not a valid message link! Failed split test"));
            }

            if (!ulong.TryParse(messageIdentifiers[0], out ulong guildId) || !ulong.TryParse(messageIdentifiers[1], out ulong channelId) || !ulong.TryParse(messageIdentifiers[2], out ulong messageId))
            {
                return(new ArgumentParseResult(Arguments[0], "Not a valid message link! Failed id parse test"));
            }

            SocketGuild guild = BotCore.Client.GetGuild(guildId);

            if (guild != null)
            {
                SocketTextChannel channel = guild.GetTextChannel(channelId);

                if (channel != null)
                {
                    argOut.message = await channel.GetMessageAsync(messageId) as IUserMessage;

                    if (argOut.message == null)
                    {
                        return(new ArgumentParseResult(Arguments[0], "Found correct guild and correct channel, but not correct message! Has the message been deleted?"));
                    }
                    else if (argOut.message.Author.Id != BotCore.Client.CurrentUser.Id)
                    {
                        return(new ArgumentParseResult(Arguments[0], "Can not edit a message the bot didn't post itself"));
                    }
                }
                else
                {
                    return(new ArgumentParseResult(Arguments[0], "Found correct guild, but not the channel!"));
                }
            }
            else
            {
                return(new ArgumentParseResult(Arguments[0], "Could not find the correct guild!"));
            }

            if (context.Message.Content.Length > Identifier.Length + context.Arguments.First.Length + 2)
            {
                context.Arguments.Index++;
                string embedText = context.RemoveArgumentsFront(1).Replace("[3`]", "```");

                if (JSONContainer.TryParse(embedText, out JSONContainer json, out string errormessage))
                {
                    if (EmbedHelper.TryGetMessageFromJSONObject(json, out argOut.embed, out argOut.messageContent, out string error))
                    {
                        return(new ArgumentParseResult(argOut));
                    }
                    else
                    {
                        return(new ArgumentParseResult(Arguments[1], error));
                    }
                }
                else
                {
                    return(new ArgumentParseResult(Arguments[1], $"Unable to parse JSON text to a json data structure! Error: `{errormessage}`"));
                }
            }
示例#9
0
        private bool parseArgument_Value(IDMCommandContext context, ArgumentContainer argOut, out ArgumentParseResult failedParse)
        {
            argOut.value = context.Arguments.First;

            switch (argOut.assignType)
            {
            case BotVarType.UInt64:
                if (ulong.TryParse(argOut.value, out ulong uint64Val))
                {
                    argOut.BotVar = new BotVar(argOut.BotVarId, uint64Val);
                }
                else
                {
                    failedParse = new ArgumentParseResult(Arguments[3]);
                    return(false);
                }
                break;

            case BotVarType.Int64:
                if (long.TryParse(argOut.value, out long int64Val))
                {
                    argOut.BotVar = new BotVar(argOut.BotVarId, int64Val);
                }
                else
                {
                    failedParse = new ArgumentParseResult(Arguments[3]);
                    return(false);
                }
                break;

            case BotVarType.Float64:
                if (double.TryParse(argOut.value, out double float64Val))
                {
                    argOut.BotVar = new BotVar(argOut.BotVarId, float64Val);
                }
                else
                {
                    failedParse = new ArgumentParseResult(Arguments[3]);
                    return(false);
                }
                break;

            case BotVarType.String:
                argOut.BotVar = new BotVar(argOut.BotVarId, argOut.value);
                break;

            case BotVarType.Bool:
                if (bool.TryParse(argOut.value, out bool boolVal))
                {
                    argOut.BotVar = new BotVar(argOut.BotVarId, boolVal);
                }
                else
                {
                    failedParse = new ArgumentParseResult(Arguments[3]);
                    return(false);
                }
                break;

            case BotVarType.Generic:
                string json_str = context.RemoveArgumentsFront(3);
                if (JSONContainer.TryParse(json_str, out JSONContainer json, out string error))
                {
                    argOut.BotVar = new BotVar(argOut.BotVarId, json);
                }
                else
                {
                    failedParse = new ArgumentParseResult(Arguments[3], error);
                    return(false);
                }
                break;