コード例 #1
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);
        }
コード例 #2
0
        private async Task MessageReceived(SocketMessage arg)
        {
            SocketRole infectedRole = null;

            if (arg is SocketUserMessage message && !message.Author.IsBot && !message.Author.IsWebhook && message.Author is SocketGuildUser user1 && message.Channel is SocketGuildChannel channel)
            {
                if (!configs.Guilds.ContainsKey(user1.Guild.Id.ToString()))
                {
                    return;
                }
                if (!databases.Guilds.ContainsKey(user1.Guild.Id.ToString()))
                {
                    databases.Guilds.Add(user1.Guild.Id.ToString(), new GuildDatabase());
                }
                GuildConfig config = (configs.Guilds[user1.Guild.Id.ToString()]);
                if (infectedRole == null)
                {
                    infectedRole = user1.Guild.GetRole(config.InfectedRoleId);
                }
                SocketGuild guild = user1.Guild;
                if (infectedRole != null && !config.SafeChannelIds.Contains(message.Channel.Id) && !config.SuperSafeChannelIds.Contains(message.Channel.Id) && message.Content.ToLower().Contains("*cough*") && user1.Roles.Contains(infectedRole))
                {
                    bool found = false;
                    foreach (var item in config.SafeChannelIds)
                    {
                        var cat = guild.GetCategoryChannel(item);
                        if (cat == null || !cat.Channels.Contains(channel))
                        {
                            continue;
                        }
                        found = true;
                        break;
                    }
                    if (!found)
                    {
                        foreach (var item in config.SuperSafeChannelIds)
                        {
                            var cat = guild.GetCategoryChannel(item);
                            if (cat == null || !cat.Channels.Contains(channel))
                            {
                                continue;
                            }
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                    {
                        var msgsa = message.Channel.GetMessagesAsync(config.InfectMessageLimit);
                        var msgs  = await msgsa.FlattenAsync();

                        // if (msgs.Where(p => p.Content.Contains("*cough*") && p.Author.Id == user1.Id).Count() == 0)
                        {
                            IMessage[] array = msgs.Where(p => !p.Author.IsBot && !p.Author.IsWebhook && p.Author.Id != user1.Id && p.Timestamp.UtcDateTime.Add(TimeSpan.FromSeconds(config.SafeTimeSeconds)) >= DateTime.UtcNow && p.Author is SocketGuildUser user && !user.Roles.Contains(infectedRole)).ToArray();
                            if (array.Length != 0)
                            {
                                IMessage msg2 = array[rng.Next(array.Length)];
                                if (msg2 is RestUserMessage message2 && message2.Author is SocketGuildUser user2)
                                {
                                    await InfectUser(user2, infectedRole, user1);
                                    await SaveData();

                                    string name = string.IsNullOrWhiteSpace(user1.Nickname) ? user1.Username : user1.Nickname;
                                    await message2.ReplyAsync($"{name} infected you with {config.VirusName}!");
                                }
                            }
                        }
                    }
                }
                if (infectedRole != null && !user1.Roles.Contains(infectedRole) && !config.SuperSafeChannelIds.Contains(message.Channel.Id) && rng.Next(100) < config.AutoInfectPercent)
                {
                    bool found = false;
                    foreach (var item in config.SuperSafeChannelIds)
                    {
                        var cat = guild.GetCategoryChannel(item);
                        if (cat == null || !cat.Channels.Contains(channel))
                        {
                            continue;
                        }
                        found = true;
                        break;
                    }
                    if (!found)
                    {
                        await InfectUser(user1, infectedRole);
                        await SaveData();

                        await message.ReplyAsync($"Somehow, you were infected with {config.VirusName}!");
                    }
                }
                if (infectedRole != null && message.Content.ToLower().StartsWith(config.StatsCommand.ToLower()))
                {
                    string[] infected = infectedRole.Members.OrderByDescending(p =>
                    {
                        if (databases.Guilds[user1.Guild.Id.ToString()].InfectedTimestamps.ContainsKey(p.Id.ToString()))
                        {
                            return(databases.Guilds[user1.Guild.Id.ToString()].InfectedTimestamps[p.Id.ToString()]);
                        }
                        return(long.MinValue);
                    }).Select(p =>
                    {
                        string time = "N/A";
                        if (databases.Guilds[user1.Guild.Id.ToString()].InfectedTimestamps.ContainsKey(p.Id.ToString()))
                        {
                            time = DateTime.MinValue.AddTicks(databases.Guilds[user1.Guild.Id.ToString()].InfectedTimestamps[p.Id.ToString()]).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 message.ReplyAsync(embed : emb.Build());

                    // string path = Path.GetTempFileName();
                    string path = Path.GetRandomFileName() + ".png";
                    StatsDraw.Draw(path, user1.Guild, databases.Guilds[user1.Guild.Id.ToString()].InfectedWho, config.StatsMaxInfectedListings);
                    await message.Channel.SendFileAsync(path);

                    File.Delete(path);
                }
                if (user1.GuildPermissions.ManageRoles && message.Content.ToLower().StartsWith(config.ResetCommand.ToLower()))
                {
                    if (infectedRole != null)
                    {
                        await infectedRole.DeleteAsync();
                    }
                    RestRole role = await user1.Guild.CreateRoleAsync(config.InfectedRoleName, GuildPermissions.None, new Discord.Color(config.InfectedRoleColorRed, config.InfectedRoleColorGreen, config.InfectedRoleColorBlue), false, false);

                    configs.Guilds[user1.Guild.Id.ToString()].InfectedRoleId = role.Id;
                    databases.Guilds[user1.Guild.Id.ToString()].InfectedTimestamps.Clear();
                    databases.Guilds[user1.Guild.Id.ToString()].InfectedWho.Clear();
                    await SaveData();

                    await message.ReplyAsync($"{config.VirusName} has been contained.");
                }
            }
        }