Beispiel #1
0
        private static async Task Ready()
        {
            List <ulong> guildsInDatabase = new List <ulong>();

            await Bot.SetGameAsync(Configuration.Load().StatusText, Configuration.Load().StatusLink,
                                   (ActivityType)Configuration.Load().StatusActivity);

            await Bot.SetStatusAsync(Configuration.Load().Status);

            ModeratorModule.ActiveForDateTime = DateTime.Now;


            (MySqlDataReader dr, MySqlConnection conn)reader = DatabaseActivity.ExecuteReader("SELECT * FROM guilds;");
            while (reader.dr.Read())
            {
                ulong id = reader.dr.GetUInt64("guildID");
                guildsInDatabase.Add(id);
            }

            await new LogMessage(LogSeverity.Info, "Startup", "-----------------------------------------------------------------").PrintToConsole();
            foreach (SocketGuild g in Bot.Guilds)
            {
                Console.ResetColor();
                await new LogMessage(LogSeverity.Info, "Startup", "Attempting to load " + g.Name).PrintToConsole();

                await GuildHandler.InsertGuildToDB(g);

                guildsInDatabase.Remove(g.Id);
                await new LogMessage(LogSeverity.Info, "Startup", "-----------------------------------------------------------------").PrintToConsole();

                foreach (SocketGuildChannel c in g.Channels)
                {
                    await ChannelHandler.InsertChannelToDB(c);
                }
                await new LogMessage(LogSeverity.Info, "Startup", "-----------------------------------------------------------------").PrintToConsole();

                await ReadyAddUsersToDatabase(g).ConfigureAwait(false);

                await new LogMessage(LogSeverity.Info, "Startup", "-----------------------------------------------------------------").PrintToConsole();

                await ReadyAddBansToDatabase(g).ConfigureAwait(false);

                await new LogMessage(LogSeverity.Info, "Startup", "-----------------------------------------------------------------").PrintToConsole();
            }

            foreach (ulong id in guildsInDatabase)
            {
                await GuildHandler.RemoveGuildFromDB(id.GetGuild());

                DatabaseActivity.ExecuteNonQueryCommand("DELETE FROM channels WHERE inGuildID=" + id);
                Console.WriteLine(id + " has been removed from the database.");
            }

            if (offlineList.Any())
            {
                await new LogMessage(LogSeverity.Info, "Startup", offlineList.Count + " new users added.").PrintToConsole();
                foreach (Tuple <SocketGuildUser, SocketGuild> tupleList in offlineList)
                {
                    await new LogMessage(LogSeverity.Warning, "Startup", tupleList.Item1.Mention + " (" + tupleList.Item1.Id + ") joined " + tupleList.Item2.Name + " while the Bot was offline.").PrintToConsole();
                }
            }
            else
            {
                await new LogMessage(LogSeverity.Info, "Startup", "No new users added.").PrintToConsole();
            }

            await new LogMessage(LogSeverity.Info, "Startup", Bot.CurrentUser.Username + " loaded.").PrintToConsole();

            // Send message to log channel to announce bot is up and running.
            Version      v  = Assembly.GetExecutingAssembly().GetName().Version;
            EmbedBuilder eb = new EmbedBuilder()
                              .WithTitle("Startup Notification")
                              .WithColor(59, 212, 50)
                              .WithThumbnailUrl(Bot.CurrentUser.GetAvatarUrl())
                              .WithDescription("**" + Bot.CurrentUser.Username + "** : ready event executed.")
                              .AddField("Version", v.Major + "." + v.Minor + "." + v.Build + "." + v.Revision, true)
                              .AddField("MelissaNet", VersionInfo.Version, true)
                              .AddField("Latency", Bot.Latency + "ms", true)
                              .WithCurrentTimestamp();
            await Configuration.Load().LogChannelId.GetTextChannel().SendMessageAsync("", false, eb.Build());

            if (offlineList.Any())
            {
                foreach (Tuple <SocketGuildUser, SocketGuild> tupleList in offlineList)
                {
                    await Configuration.Load().LogChannelId.GetTextChannel().SendMessageAsync("[ALERT] While " + Bot.CurrentUser.Username + " was offline, " + tupleList.Item1.Mention + " (" + tupleList.Item1.Id + ") joined " + tupleList.Item2.Name + ". They have been added to the database.");
                }
            }
        }