/// <summary> /// Fills in the data fields from some backup. Previously we did that on Bot() initialization, /// we do that in a separate function now. /// </summary> /// <returns></returns> public async Task DelayedConstruction() { if (_primary == null) { throw new PrimaryGuildException("DelayedConstruction() called too early, the Discord API is not ready yet."); } if (Settings.BotFirstRun) { FirstRunDelayedConstruction(); return; } if (constructionComplete) { throw new PrimaryGuildException("Construction called twice, that is not allowed."); } Console.WriteLine("Restoring guild list configuration from message backup."); BackupGuildConfiguration bgc = await RestoreGuildConfiguration(); guilds = new DiscordGuilds(bgc, client); // We check for role creation here, instead of inside the constructor of DiscordGuild. // We do this not to make the constructor async itself. It might be better to check there. foreach (DiscordGuild g in guilds.byID.Values) { await g.RolePresenceCheckAsync(); } Console.WriteLine("Populating data from the message backup."); BackupData recoverData = await RestoreDataStructures(); _data = new BotDataStructure(recoverData); if (Settings.UsingExtensionBanTracking) { // Temporary: if the bds structure was not parsed from the backup, because it didn't exist, just create a new empty one. if (recoverData.bds == null) { Console.WriteLine("Unable to restore the old ban data structure, creating an empty one."); recoverData.bds = new Extensions.BanDataStructure(); } bt = new Extensions.BanTracking(guilds, recoverData.bds); } if (Settings.UsingExtensionRoleHighlights) { _highlighter = new Extensions.MainHighlighter(guilds); } Console.WriteLine("Loaded " + _data.DiscordUplay.Count + " discord -- uplay connections."); Console.WriteLine("Loaded " + _data.QuietPlayers.Count + " players who wish not to be pinged."); Console.WriteLine("Loaded " + _data.DiscordRanks.Count + " current player ranks."); constructionComplete = true; }
public static void Run() { var gc = new BackupGuildConfiguration(); var controlCenter = new SingleGuildConfig(); controlCenter.id = Settings.ControlGuild; controlCenter.reportChannel = "reports"; controlCenter.roleHighlightChannels = new List <string> { "looking-for-teammates" }; controlCenter.commandChannels = new List <string> { "rank-bot" }; var chillServer = new SingleGuildConfig(); chillServer.id = 620608384227606528; chillServer.commandChannels = new List <string> { "🦾rank-bot", "rank-bot-admin" }; chillServer.reportChannel = "🦾rank-bot"; chillServer.roleHighlightChannels = new List <string> { "🔍hledám-spoluhráče" }; gc.guildList.Add(controlCenter); gc.guildList.Add(chillServer); Console.WriteLine(gc.BackupToString()); }
public static BackupGuildConfiguration RestoreFromString(string content) { BackupGuildConfiguration ret = null; TextReader stringr = new StringReader(content); JsonSerializer serializer = new JsonSerializer(); ret = (BackupGuildConfiguration)serializer.Deserialize(stringr, typeof(BackupGuildConfiguration)); return(ret); }
public DiscordGuilds(BackupGuildConfiguration guildConfs, DiscordSocketClient sc) { guildList = new List <DiscordGuild>(); byName = new Dictionary <string, DiscordGuild>(); byID = new Dictionary <ulong, DiscordGuild>(); foreach (var gc in guildConfs.guildList) { DiscordGuild guild = new DiscordGuild(gc, sc); guildList.Add(guild); byName.Add(guild.GetName(), guild); byID.Add(guild._socket.Id, guild); Console.WriteLine($"Connected to server {guild.GetName()}."); } }
public static BackupGuildConfiguration RestoreFromFile(string fileName) { BackupGuildConfiguration ret = null; if (File.Exists(fileName)) { JsonSerializer serializer = new JsonSerializer(); StreamReader fileStream = File.OpenText(fileName); JsonTextReader file = new JsonTextReader(fileStream); ret = (BackupGuildConfiguration)serializer.Deserialize(file, typeof(BackupGuildConfiguration)); file.Close(); } return(ret); }
public async Task <BackupGuildConfiguration> RestoreGuildConfiguration() { // First, check that the primary guild is already loaded. if (!_primaryServerLoaded) { throw new PrimaryGuildException("Primary guild (Discord server) did not load and yet RestoreGuildConfiguration() is called."); } BackupGuildConfiguration gc = null; // First, attempt to restore the configuration from a message. var primaryChannel = _primary._socket.TextChannels.Single(ch => ch.Name == Settings.PrimaryConfigurationChannel); if (primaryChannel == null) { throw new PrimaryGuildException("Unable to find the primary configuration channel. Even if you are trying to restore from a file, create this channel first."); } var messages = primaryChannel.GetMessagesAsync().Flatten(); var msgarray = await messages.ToArrayAsync(); if (msgarray.Count() != 1) { Console.WriteLine($"Restoration expects exactly one message, found {msgarray.Count()}."); } else { var client = new HttpClient(); var dataString = await client.GetStringAsync(msgarray[0].Attachments.First().Url); // TODO: exception handling here. gc = BackupGuildConfiguration.RestoreFromString(dataString); } // If the above fails, attempt to read it from a backup file. if (gc == null) { gc = BackupGuildConfiguration.RestoreFromFile(Settings.PrimaryConfigurationFile); if (gc == null) { throw new PrimaryGuildException("Unable to restore guild configuration from any backup."); } } return(gc); }