예제 #1
0
 public Task UserJoined(SocketGuildUser arg)
 {
     //Import the joined user to the GuildUserDatabase
     GuildUserDatabase.ImportUserToDB(arg.Guild.Id, arg);
     CoreProgram._errorHandler._client_Log(new Discord.LogMessage(Discord.LogSeverity.Info, "UserJoined", $"The User '{arg.Nickname}' has joined the Guild '{arg.Guild.Name}'"));
     return(Task.CompletedTask);
 }
예제 #2
0
 public Task UserLeft(SocketGuildUser arg)
 {
     //Delete the joined user to the GuildUserDatabase
     GuildUserDatabase.DelteUserFromDB(arg.Guild.Id, arg);
     CoreProgram._errorHandler._client_Log(new Discord.LogMessage(Discord.LogSeverity.Info, "UserLeft", $"The User '{arg.Nickname}' has left the Guild '{arg.Guild.Name}'"));
     return(Task.CompletedTask);
 }
예제 #3
0
        public async Task InfectUser(SocketGuildUser user1, SocketRole infectedRole, SocketGuildUser infector = null)
        {
            if (infectedRole == null)
            {
                return;
            }
            await user1.AddRoleAsync(infectedRole);

            if (databases.Guilds[user1.Guild.Id.ToString()].GuildUsers.FirstOrDefault(p => p.Id == user1.Id) == null)
            {
                databases.Guilds[user1.Guild.Id.ToString()].GuildUsers.Add(new GuildUserDatabase()
                {
                    Id = user1.Id
                });
            }
            GuildUserDatabase gu1 = databases.Guilds[user1.Guild.Id.ToString()].GuildUsers.FirstOrDefault(p => p.Id == user1.Id);

            gu1.InfectedTimestamp = DateTime.UtcNow.Ticks;
            gu1.Infection         = 1f; // TODO: proper infection system

            if (infector != null)
            {
                if (databases.Guilds[user1.Guild.Id.ToString()].GuildUsers.FirstOrDefault(p => p.Id == infector.Id) == null)
                {
                    databases.Guilds[user1.Guild.Id.ToString()].GuildUsers.Add(new GuildUserDatabase()
                    {
                        Id = infector.Id
                    });
                }
                GuildUserDatabase gu2 = databases.Guilds[user1.Guild.Id.ToString()].GuildUsers.FirstOrDefault(p => p.Id == infector.Id);
                gu2.InfectedWho.Add(user1.Id);
                // TODO: reduce infection for infector and make use of it properly
            }
            await SaveData();
        }
예제 #4
0
        public static async Task SaveGuildDatabase()
        {
            //create a new GuildDatabase object
            GuildUserDatabase gdb = new GuildUserDatabase();

            //implement all setting/cache lists in here!

            gdb.GuildUserDatabaseDictionary = Databases.GuildUserDatabase.GuildSettingsList;
            gdb.LastUpdate = DateTime.Now;

            //string serialization with json
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(gdb);
            //leave this path alone cause it reflects the current working dictionary
            string path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + @"/GuildUserDatabase.json";

            //Check if the file exist
            if (!File.Exists(path))
            {
                await CreateDatabaseFile(path);
            }
            using (StreamWriter sw = new StreamWriter(path, false))
            {
                //Write data async to the file
                await sw.WriteAsync(json);

                //don´t forget to close writers, too
                sw.Close();
            }
            //set changesMade to false for new changes
            GuildDatabase_ChangesMade = false;
        }
예제 #5
0
        public async Task StatsAsync()
        {
            Program.GuildConfig   config   = Program.program.GetGuildConfig(Context.Guild);
            Program.GuildDatabase database = Program.program.GetGuildDatabase(Context.Guild);
            if (config == null || database == null)
            {
                await Context.Message.ReplyAsync("No config/database found on server.\nDid an admin register the bot?");

                return;
            }
            SocketRole infectedRole = Context.Guild.GetRole(config.InfectedRoleId);

            string[] infected = infectedRole.Members.OrderByDescending(p =>
            {
                GuildUserDatabase dbUser = database.GuildUsers.FirstOrDefault(p2 => p2.Id == p.Id);
                if (dbUser != null && dbUser.Infection >= config.InfectionMin)
                {
                    return(dbUser.InfectedTimestamp);
                }
                return(long.MinValue);
            }).Select(p =>
            {
                string time = "N/A";
                GuildUserDatabase dbUser = database.GuildUsers.FirstOrDefault(p2 => p2.Id == p.Id);
                if (dbUser != null && dbUser.Infection >= config.InfectionMin)
                {
                    time = DateTime.MinValue.AddTicks(dbUser.InfectedTimestamp).ToString();
                }
                return($"{p.Username}#{p.Discriminator} was infected at {time}");
            }).ToArray();
            string output = "";

            for (int i = 0; i < infected.Length && i < config.StatsMaxInfectedListings; i++)
            {
                if (i != 0)
                {
                    output += "\n";
                }
                output += infected[i];
            }
            EmbedBuilder emb = new EmbedBuilder();

            emb.WithColor(config.InfectedRoleColorRed, config.InfectedRoleColorGreen, config.InfectedRoleColorBlue);
            emb.WithTitle(config.VirusName);
            emb.WithDescription(output);
            await Context.Message.ReplyAsync(embed : emb.Build());

            // string path = Path.GetTempFileName();
            string path = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".png");
            Dictionary <string, List <ulong> > stats = new Dictionary <string, List <ulong> >();

            foreach (var uitem in database.GuildUsers)
            {
                stats.Add(uitem.Id.ToString(), uitem.InfectedWho);
            }
            StatsDraw.Draw(path, Context.Guild, stats, config.StatsMaxInfectedListings);
            await Context.Channel.SendFileAsync(path);

            File.Delete(path);
        }
예제 #6
0
        public static async Task LoadDatabase()
        {
            await CoreProgram._errorHandler._client_Log(new LogMessage(LogSeverity.Debug, "LoadDatabase", "Loading databases"));

            string[] path = new string[2] {
                Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + @"/ServerDatabase.json", Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + @"/GuildUserDatabase.json"
            };
            int pathCount = 0;

            foreach (string _path in path)
            {
                //Check if the file exist
                if (!File.Exists(_path))
                {
                    await CoreProgram._errorHandler._client_Log(new LogMessage(LogSeverity.Warning, "LoadDatabase", "File not found... create file!"));
                    await CreateDatabaseFile(_path);
                }
                using (StreamReader reader = new StreamReader(_path))
                {
                    //leave this path alone cause it reflects the current working dictionary
                    string json = await reader.ReadToEndAsync();

                    if (pathCount == 0)
                    {
                        //create a new ServerDatabase from the json deserializer
                        ServerDatabase sdb = Newtonsoft.Json.JsonConvert.DeserializeObject <ServerDatabase>(json);
                        //set all setting/cache lists in here!
                        if (sdb != null)
                        {
                            ServerSettings.SettingList = sdb.ServerDatabaseDictionary; await CoreProgram._errorHandler._client_Log(new LogMessage(LogSeverity.Debug, "LoadDatabase", "Loaded: server_database..."));
                        }
                        else
                        {
                            await CoreProgram._errorHandler._client_Log(new LogMessage(LogSeverity.Critical, "LoadDatabase", "Could not load ServerDatabase", new NullReferenceException()));
                        }
                    }
                    if (pathCount == 1)
                    {
                        //create a new GuildDatabase from the json deserializer
                        GuildUserDatabase gdb = Newtonsoft.Json.JsonConvert.DeserializeObject <GuildUserDatabase>(json);
                        //set all setting/cache lists in here!
                        if (gdb != null)
                        {
                            Databases.GuildUserDatabase.GuildSettingsList = gdb.GuildUserDatabaseDictionary; await CoreProgram._errorHandler._client_Log(new LogMessage(LogSeverity.Debug, "LoadDatabase", "Loaded: guild_database..."));
                        }
                        else
                        {
                            await CoreProgram._errorHandler._client_Log(new LogMessage(LogSeverity.Critical, "LoadDatabase", "Could not load GuildUserDatabase", new NullReferenceException()));
                        }
                    }
                }
                pathCount++;
            }
        }
예제 #7
0
        public async Task CureUser(SocketGuildUser user1, SocketRole infectedRole)
        {
            if (infectedRole == null)
            {
                return;
            }
            await user1.RemoveRoleAsync(infectedRole);

            GuildUserDatabase db = databases.Guilds[user1.Guild.Id.ToString()].GuildUsers.FirstOrDefault(p => p.Id == user1.Id);

            if (db == null)
            {
                return;
            }
            databases.Guilds[user1.Guild.Id.ToString()].GuildUsers.Remove(db);
            await SaveData();
        }