private async Task LoadMissions(SocketGuild guild) { var signups = _services.GetService <SignupsData>(); var channels = guild.CategoryChannels.Single(x => x.Id == _config.SignupsCategory); Console.WriteLine($"[{DateTime.Now.ToString()}] Loading missions"); foreach (var channel in channels.Channels.Where(x => x.Id != _config.SignupsArchive && x.Id != _config.CreateMissionChannel && x.Id != _config.HallOfShameChannel).Reverse()) { if (signups.Missions.Any(x => x.SignupChannel == channel.Id)) { continue; } var mission = new ArmaforcesMissionBotSharedClasses.Mission(); var textChannel = channel as SocketTextChannel; var messages = textChannel.GetMessagesAsync(); List <IMessage> messagesNormal = new List <IMessage>(); await messages.ForEachAsync(async x => { foreach (var it in x) { messagesNormal.Add(it); } }); mission.SignupChannel = channel.Id; foreach (var message in messagesNormal) { if (message.Embeds.Count == 0) { continue; } if (message.Author.Id != _client.CurrentUser.Id) { continue; } var embed = message.Embeds.Single(); if (embed.Author == null) { var pattern = ""; if (embed.Footer.HasValue) { pattern = embed.Footer.Value.Text; } else { pattern = embed.Title; } MatchCollection matches = Helpers.MiscHelper.GetSlotMatchesFromText(pattern); if (matches.Count > 0) { var team = new ArmaforcesMissionBotSharedClasses.Mission.Team(); team.Name = embed.Title; pattern = ""; foreach (Match match in matches.Reverse()) { var icon = match.Groups[1].Value; if (icon[0] == ':') { var emotes = _client.GetGuild(_config.AFGuild).Emotes; var foundEmote = emotes.Single(x => x.Name == icon.Substring(1, icon.Length - 2)); var animated = foundEmote.Animated ? "a" : ""; icon = $"<{animated}:{foundEmote.Name}:{foundEmote.Id}>"; pattern = pattern.Replace(match.Groups[1].Value, icon); } var count = match.Groups[2].Value; var name = match.Groups[3].Success ? match.Groups[3].Value : ""; var slot = new ArmaforcesMissionBotSharedClasses.Mission.Team.Slot( name, icon, int.Parse(count.Substring(1, count.Length - 2))); if (!embed.Footer.HasValue) { team.Name = team.Name.Replace(match.Groups[0].Value, ""); } pattern += $"{match.Groups[0]} "; team.Slots.Add(slot); Console.WriteLine($"New slot {slot.Emoji} [{slot.Count}] {slot.Name}"); } team.Name = team.Name.Replace("|", ""); if (embed.Description != null) { try { string signedPattern = @"(.+)(?:\(.*\))?-\<\@\!([0-9]+)\>"; MatchCollection signedMatches = Regex.Matches(embed.Description, signedPattern, RegexOptions.IgnoreCase | RegexOptions.RightToLeft); foreach (Match match in signedMatches.Reverse()) { var signedID = ulong.Parse(match.Groups[2].Value); mission.SignedUsers.Add(signedID); Console.WriteLine($"{match.Groups[1].Value} : {match.Groups[2].Value} ({signedID})"); team.Slots.Single(x => x.Emoji == match.Groups[1].Value).Signed.Add(signedID); } } catch (Exception e) { Console.WriteLine($"Failed loading team {team.Name} : {e.Message}"); } } team.TeamMsg = message.Id; team.Pattern = pattern; mission.Teams.Add(team); } } else { mission.Title = embed.Title; mission.Description = embed.Description; mission.Owner = ulong.Parse(embed.Author.Value.Url.Substring(BotConstants.DISCORD_USER_URL_PREFIX.Length)); // Do I need author id again? mission.Attachment = embed.Image.HasValue ? embed.Image.Value.Url : null; foreach (var field in embed.Fields) { switch (field.Name) { case "Data:": mission.Date = DateTime.ParseExact(field.Value, "dd.MM.yyyy HH:mm:ss", CultureInfo.InvariantCulture); break; case "Modlista:": mission.Modlist = mission.ModlistUrl = field.Value; mission.ModlistName = GetModsetNameFromUnknownUrl(mission.ModlistUrl); break; case "Zamknięcie zapisów:": uint timeDifference; if (!uint.TryParse(field.Value, out timeDifference)) { mission.CloseTime = DateTime.ParseExact(field.Value, "dd.MM.yyyy HH:mm:ss", CultureInfo.InvariantCulture); } else { mission.CloseTime = mission.Date.AddMinutes(-timeDifference); } break; } } } } mission.Teams.Reverse(); // As teams were read backwards due to reading messages backwards signups.Missions.Add(mission); } // Sort channels by date signups.Missions.Sort((x, y) => { return(x.Date.CompareTo(y.Date)); }); }
public ArmaforcesMissionBotSharedClasses.Mission ToShared(ulong owner) { var shared = new ArmaforcesMissionBotSharedClasses.Mission(); shared.Title = Name; shared.Date = Date; shared.CloseTime = Close; shared.Description = Description; //Attachment handle shared.Modlist = "https://modlist.armaforces.com/#/download/" + Modlist; shared.Owner = owner; foreach (var team in Teams) { var outTeam = new ArmaforcesMissionBotSharedClasses.Mission.Team(); outTeam.Name = team.Name; outTeam.Pattern = ""; foreach (var slot in team.Slots) { var outSlot = new ArmaforcesMissionBotSharedClasses.Mission.Team.Slot(); outSlot.Name = slot.Name; outSlot.Emoji = (slot.Icon[0] == ':' || slot.Icon[0] == 'a') ? $"<{slot.Icon}>" : slot.Icon; outSlot.Count = slot.Count; if (slot.Signed != null) { foreach (var prebeton in slot.Signed) { outSlot.Signed.Add(prebeton); shared.SignedUsers.Add(prebeton); } } if (outTeam.Pattern.Length > 0) { outTeam.Pattern += "| "; } outTeam.Pattern += $"{outSlot.Emoji} [{outSlot.Count}] {outSlot.Name} "; outTeam.Slots.Add(outSlot); } shared.Teams.Add(outTeam); } if (Picture != null) { shared.AttachmentBytes = new byte[Picture.Length]; using (MemoryStream ms = new MemoryStream()) { Picture.CopyTo(ms); shared.AttachmentBytes = ms.ToArray(); shared.FileName = Picture.FileName; } } else { shared.AttachmentBytes = null; } return(shared); }