private static string getFormCheckedImageField(JSONContainer json, string identifier, string errorstring)
 {
     if (json.TryGetObjectField(identifier, out JSONContainer imageJSON))
     {
         return(getFormCheckedURLField(imageJSON, URL, errorstring));
     }
     return(null);
 }
 public bool ApplyJSON(JSONContainer json)
 {
     EventLogChannels.Clear();
     UserModLogChannels.Clear();
     ChannelModLogChannels.Clear();
     if (json.TryGetObjectField("EventLogChannels", out JSONContainer eventChannelsJSON))
     {
         foreach (JSONField field in eventChannelsJSON.Fields)
         {
             if (Enum.TryParse(field.Identifier, out DiscordEventType type))
             {
                 EventLogChannels.Add(type, field.Unsigned_Int64);
             }
         }
     }
     if (json.TryGetObjectField("UserModLogChannels", out JSONContainer modChannelsJSON))
     {
         foreach (JSONField field in modChannelsJSON.Fields)
         {
             if (Enum.TryParse(field.Identifier, out ModerationType type))
             {
                 UserModLogChannels.Add(type, field.Unsigned_Int64);
             }
         }
     }
     if (json.TryGetObjectField("ChannelModLogChannels", out JSONContainer channelsLogJSON))
     {
         foreach (JSONField field in channelsLogJSON.Fields)
         {
             if (Enum.TryParse(field.Identifier, out ChannelModerationType type))
             {
                 ChannelModLogChannels.Add(type, field.Unsigned_Int64);
             }
         }
     }
     return(true);
 }
Exemple #3
0
            public void FromJSON(JSONContainer json)
            {
                json.TryGetField("name", out Name, Name);
                json.TryGetField("id", out Id);
                if (json.TryGetField("requirePermit", out RequirePermit))
                {
                    if (RequirePermit == true)
                    {
                        json.TryGetField("permitName", out PermitName, "Unknown Permit");
                    }
                }

                if (json.TryGetObjectField("information", out JSONContainer infoJSON))
                {
                    json.TryGetField("security", out Security, "Anarchy");
                    json.TryGetField("faction", out ControllingFaction);
                }
            }
        /// <summary>
        /// Retrieves messageContent and embed from JSON. Throws <see cref="EmbedParseException"/> if invalid formatting is found.
        /// </summary>
        /// <param name="json">JSONContainer object to parse from</param>
        /// <param name="embed">embed result. Null if not specified in <paramref name="json"/></param>
        /// <param name="messageContent">Message content result. Null if not specified in <paramref name="json"/></param>
        public static void GetMessageFromJSONObject(JSONContainer json, out EmbedBuilder embed, out string messageContent)
        {
            messageContent = getLengthCheckedStringField(json, MESSAGECONTENT, MESSAGECONTENT_MAX, "The message content may not exceed {0} characters in length!");
            if (json.TryGetObjectField(EMBED, out JSONContainer embedJSON))
            {
                embed = getEmbed(embedJSON);
            }
            else
            {
                embed = null;
            }

            // Sanity check for errors

            if (string.IsNullOrEmpty(messageContent) && embedJSON == null)
            {
                throw new EmbedParseException("Message is empty, neither embed nor messagecontent were specified!");
            }
        }
Exemple #5
0
        private EmbedBuilder GetInaraCMDREmbed(JSONContainer json, string backupName)
        {
            EmbedBuilder result;

            if (json.TryGetArrayField("events", out JSONContainer arrayJSON))
            {
                if (arrayJSON.Array.Count >= 1 && arrayJSON.Array[0].IsObject)
                {
                    JSONContainer eventJSON = arrayJSON.Array[0].Container;
                    if (eventJSON.TryGetField("eventStatus", out uint eventStatusId))
                    {
                        eventJSON.TryGetField("eventStatusText", out string eventStatusText);
                        if (eventStatusId == 204)
                        {
                            result = new EmbedBuilder()
                            {
                                Author = new EmbedAuthorBuilder()
                                {
                                    Name = backupName
                                },
                                Color       = BotCore.ErrorColor,
                                Description = eventStatusText
                            };
                        }
                        else if (eventJSON.TryGetObjectField("eventData", out JSONContainer cmdrJSON))
                        {
                            if (cmdrJSON.TryGetField("userName", out string user_name))
                            {
                                if (eventStatusId == 202 && !user_name.Equals(backupName, StringComparison.OrdinalIgnoreCase) && cmdrJSON.TryGetArrayField("otherNamesFound", out JSONContainer otherNamesArray))
                                {
                                    result = new EmbedBuilder()
                                    {
                                        Title       = "Multiple Results found!",
                                        Description = $"{user_name}\n{otherNamesArray.Array.OperationJoinReadonly("\n", field => { return field.String; })}"
                                    };
                                }
                                else
                                {
                                    cmdrJSON.TryGetField("inaraURL", out string cmdr_url);
                                    result = new EmbedBuilder()
                                    {
                                        Author = new EmbedAuthorBuilder()
                                        {
                                            Name = $"{user_name}'s Inara Profile",
                                            Url  = cmdr_url
                                        },
                                        Color = BotCore.EmbedColor
                                    };
                                    if (cmdrJSON.TryGetField("preferredGameRole", out string cmdr_gamerole))
                                    {
                                        result.AddField("Game Role", cmdr_gamerole, true);
                                    }
                                    if (cmdrJSON.TryGetField("preferredAllegianceName", out string cmdr_allegiance))
                                    {
                                        result.AddField("Allegiance", cmdr_allegiance, true);
                                    }
                                    if (cmdrJSON.TryGetObjectField("commanderSquadron", out JSONContainer squadronJSON))
                                    {
                                        squadronJSON.TryGetField("squadronName", out string squadron_name);
                                        squadronJSON.TryGetField("squadronMemberRank", out string squadron_rank);
                                        squadronJSON.TryGetField("squadronMembersCount", out uint squadron_membercount);
                                        if (squadronJSON.TryGetField("inaraURL", out string squadron_link))
                                        {
                                            result.AddField("Squadron", $"[{squadron_name}]({squadron_link}) ({squadron_membercount} Members): Rank `{squadron_rank}`", true);
                                        }
                                        else
                                        {
                                            result.AddField("Squadron", $"{squadron_name} ({squadron_membercount} Members): Rank `{squadron_rank}`", true);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                result = InaraErrorEmbed(backupName);
                            }
                        }
                        else
                        {
                            result = InaraErrorEmbed(backupName);
                        }
                    }
                    else
                    {
                        result = InaraErrorEmbed(backupName);
                    }
                }
                else
                {
                    result = InaraErrorEmbed(backupName);
                }
            }
            else
            {
                result = InaraErrorEmbed(backupName);
            }

            return(result);
        }
Exemple #6
0
        private EmbedBuilder GetSystemInfoEmbed(JSONContainer systemJSON, JSONContainer stationsJSON, JSONContainer trafficJSON, JSONContainer deathsJSON, out List <EmbedFieldBuilder> allStations)
        {
            EmbedBuilder systemembed = new EmbedBuilder();

            allStations = new List <EmbedFieldBuilder>();
            bool stationsFound = false;

            if (systemJSON.TryGetField("name", out string system_name, string.Empty) && systemJSON.TryGetField("id", out ulong system_id, 0))
            {
                // Gathering Information

                // Gathering General System information
                string system_name_link = system_name.Replace(' ', '+');
                string security         = "Unknown";
                if (systemJSON.TryGetObjectField("information", out JSONContainer system_information))
                {
                    system_information.TryGetField("security", out security, "Anarchy (Unpopulated)");
                }
                string startype = "Not found";
                if (systemJSON.TryGetObjectField("primaryStar", out JSONContainer primaryStar))
                {
                    primaryStar.TryGetField("type", out startype, startype);
                    if (systemJSON.TryGetField("isScoopable", out bool scoopable))
                    {
                        if (!scoopable)
                        {
                            startype += " **(Unscoopable)**";
                        }
                    }
                }
                systemJSON.TryGetField("requirePermit", out bool system_requirepermit, false);
                systemJSON.TryGetField("permitName", out string system_permit, "Unknown Permit");

                // Gathering information about stations
                StationInfo        bestOrbitalLarge  = null;
                StationInfo        bestOrbitalMedium = null;
                StationInfo        bestPlanetary     = null;
                List <StationInfo> stationInfos      = new List <StationInfo>();
                if ((stationsJSON != null) && stationsJSON.TryGetArrayField("stations", out stationsJSON))
                {
                    stationsFound = true;
                    foreach (JSONField station in stationsJSON.Array)
                    {
                        StationInfo info = new StationInfo();
                        if (info.FromJSON(station.Container))
                        {
                            stationInfos.Add(info);
                            if (info.HasLargePadOrbital)
                            {
                                if (bestOrbitalLarge == null)
                                {
                                    bestOrbitalLarge = info;
                                }
                                else
                                if (info.Distance < bestOrbitalLarge.Distance)
                                {
                                    bestOrbitalLarge = info;
                                }
                            }
                            if (info.HasMedPadOrbital)
                            {
                                if (bestOrbitalMedium == null)
                                {
                                    bestOrbitalMedium = info;
                                }
                                else
                                if (info.Distance < bestOrbitalMedium.Distance)
                                {
                                    bestOrbitalMedium = info;
                                }
                            }
                            if (info.IsPlanetary)
                            {
                                if (bestPlanetary == null)
                                {
                                    bestPlanetary = info;
                                }
                                else if (info.Distance < bestPlanetary.Distance)
                                {
                                    bestPlanetary = info;
                                }
                            }
                        }
                    }
                }

                // Getting Information about traffic
                int traffic_week = -1;
                int traffic_day  = -1;

                if ((trafficJSON != null) && trafficJSON.TryGetObjectField("traffic", out trafficJSON))
                {
                    trafficJSON.TryGetField("week", out traffic_week, -1);
                    trafficJSON.TryGetField("day", out traffic_day, -1);
                }

                // Getting Information about CMDR deaths
                int deaths_week = -1;
                int deaths_day  = -1;

                if ((deathsJSON != null) && deathsJSON.TryGetObjectField("deaths", out deathsJSON))
                {
                    deathsJSON.TryGetField("week", out deaths_week, -1);
                    deathsJSON.TryGetField("day", out deaths_day, -1);
                }

                // Constructing message
                systemembed.Color = BotCore.EmbedColor;
                systemembed.Title = $"__**System Info for {system_name}**__";
                systemembed.Url   = $"https://www.edsm.net/en/system/id/{system_id}/name/{system_name_link}";
                systemembed.AddField("General Info", $"{(system_requirepermit ? string.Format("**Requires Permit**: {0}\n", system_permit) : string.Empty)}Star Type: {startype}\nSecurity: {security}");

                bool provideInfoOnLarge     = bestOrbitalLarge != null;
                bool provideInfoOnMedium    = (!provideInfoOnLarge && bestOrbitalMedium != null) || (provideInfoOnLarge && bestOrbitalMedium != null && bestOrbitalLarge.Distance > bestOrbitalMedium.Distance);
                bool provideInfoOnPlanetary = (!provideInfoOnLarge && bestPlanetary != null) || (provideInfoOnLarge && bestPlanetary != null && bestOrbitalLarge.Distance > bestPlanetary.Distance);
                if (provideInfoOnLarge)
                {
                    systemembed.AddField("Closest Orbital Large Pad", bestOrbitalLarge.ToString());
                }
                if (provideInfoOnMedium)
                {
                    systemembed.AddField("Closest Orbital Medium Pad", bestOrbitalMedium.ToString());
                }
                if (provideInfoOnPlanetary)
                {
                    systemembed.AddField("Closest Planetary Large Pad", bestPlanetary.ToString());
                }
                if (!provideInfoOnLarge && !provideInfoOnMedium && !provideInfoOnPlanetary)
                {
                    systemembed.AddField("No Stations in this System!", "- / -");
                }
                if (traffic_day != -1 && traffic_week != -1)
                {
                    systemembed.AddField("Traffic Report", string.Format("Last 7 days: {0} CMDRs, last 24 hours: {1} CMDRs", traffic_week, traffic_day));
                }
                else
                {
                    systemembed.AddField("No Traffic Report Available", "- / -");
                }
                if (deaths_day != -1 && deaths_week != -1)
                {
                    systemembed.AddField("CMDR Deaths Report", string.Format("Last 7 days: {0} CMDRs, last 24 hours: {1} CMDRs", deaths_week, deaths_day));
                }
                else
                {
                    systemembed.AddField("No CMDR Deaths Report Available", "- / -");
                }

                if (stationsFound)
                {
                    foreach (StationInfo stationInfo in stationInfos)
                    {
                        allStations.Add(Macros.EmbedField(stationInfo.Title_NoLink, stationInfo.Services_Link));
                    }
                }
            }