Exemplo n.º 1
0
        private async Task <UserModel> TryToBornDragon(UserModel dbUser, string respawnPropName, string bornerPropName)
        {
            try
            {
                var respawnTime = ReflectionExtension.GetPropValue <List <DateTimeOffset> >(dbUser, respawnPropName);
                var borner      = ReflectionExtension.GetPropValue <uint>(dbUser, bornerPropName);
                if (respawnTime == null)
                {
                    respawnTime = new List <DateTimeOffset>();
                    ReflectionExtension.SetValue(dbUser, respawnPropName, respawnTime);
                }
                uint nonExistTimes = 0;
                nonExistTimes = (uint)Math.Max(0, (int)borner - respawnTime.Count);
                if (nonExistTimes > 0)
                {
                    for (int j = 0; j < nonExistTimes; j++)
                    {
                        if (bornerPropName == nameof(dbUser.IncubatorCount))
                        {
                            respawnTime.Add(DateTimeOffset.UtcNow.AddHours(24));
                        }
                        else
                        {
                            respawnTime.Add(DateTimeOffset.UtcNow.AddHours(18));
                        }
                        ReflectionExtension.SetValue(dbUser, respawnPropName, respawnTime);
                    }
                }
                var removedCount = respawnTime.RemoveAll(x => (x - DateTimeOffset.UtcNow).TotalSeconds < 1);
                if (removedCount > 0)
                {
                    borner = (uint)Math.Max(0, (int)borner - removedCount);
                    ReflectionExtension.SetValue(dbUser, bornerPropName, borner);
                    var embed = new DiscordEmbedBuilder
                    {
                        Author = new DiscordEmbedBuilder.EmbedAuthor
                        {
                            Name = "🐲 Рождение дракона"
                        },
                        Color = DiscordColor.Blurple
                    };
                    var    originDragonCount = dbUser.DragonCount;
                    string prizeText         = default;
                    for (int j = 0; j < removedCount; j++)
                    {
                        var chance = Bot.random.Next(0, 100);
                        if (bornerPropName == nameof(dbUser.IncubatorCount))
                        {
                            switch (chance)
                            {
                            case var _ when chance < 10:
                                dbUser.DragonCount += 3;
                                prizeText           = "3 🐲";
                                break;

                            case var _ when chance < 35:
                                dbUser.DragonCount += 2;
                                prizeText           = "2 🐲";
                                break;

                            default:
                                dbUser.DragonCount += 1;
                                prizeText           = "1 🐲";
                                break;
                            }
                        }
                        else
                        {
                            if (chance < 35)
                            {
                                dbUser.DragonCount += 1;
                                prizeText           = "1 🐲";
                            }
                        }
                    }
                    var member        = targetGuild.Members.FirstOrDefault(x => x.Id == dbUser.Id);
                    var resultUnicode = bornerPropName == nameof(dbUser.DragonEggCount) ? DiscordEmoji.FromUnicode(client, "🥚") : DiscordEmoji.FromName(client, ":incubator:");
                    if (originDragonCount == dbUser.DragonCount)
                    {
                        embed.Description = member != null ? $"{member.Mention} К сожалению, процесс создания 🐲 из {resultUnicode} завершился неудачей" : $"{dbUser.Id} К сожалению, процесс создания 🐲 из {resultUnicode} завершился неудачей";
                        await client.SendMessageAsync(targetGuild.Channels.FirstOrDefault(x => x.Id == 408802925289406475), embed : embed);
                    }
                    else
                    {
                        embed.Description = member != null ? $"{member.Mention} Вы получили {prizeText} из {removedCount} {resultUnicode}" : $"{dbUser.Id} Вы получили {prizeText} из {removedCount} {resultUnicode}";
                        await client.SendMessageAsync(targetGuild.Channels.FirstOrDefault(x => x.Id == 408802925289406475), embed : embed);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.ToString());
                return(dbUser);
            }
            return(dbUser);
        }
Exemplo n.º 2
0
        private async Task <UserModel> SendTimeGifts(UserModel dbUser)
        {
            try
            {
                var currentGiftCount = dbUser.GiftCount;
                if (dbUser.VoiceOnlineTime.TotalHours >= 10 && dbUser.VoiceOnlineTime.TotalHours < 20 && !dbUser.Hours10Prize)
                {
                    dbUser.Hours10Prize = true;
                    dbUser.GiftCount++;
                }
                if (dbUser.VoiceOnlineTime.TotalHours >= 20 && dbUser.VoiceOnlineTime.TotalHours < 40 && !dbUser.Hours20Prize)
                {
                    dbUser.Hours20Prize = true;
                    dbUser.GiftCount++;
                }
                if (dbUser.VoiceOnlineTime.TotalHours >= 40 && dbUser.VoiceOnlineTime.TotalHours < 60 && !dbUser.Hours40Prize)
                {
                    dbUser.Hours40Prize = true;
                    dbUser.GiftCount++;
                }
                if (dbUser.VoiceOnlineTime.TotalHours >= 60 && dbUser.VoiceOnlineTime.TotalHours < 90 && !dbUser.Hours60Prize)
                {
                    dbUser.Hours60Prize = true;
                    dbUser.GiftCount++;
                }
                if (dbUser.VoiceOnlineTime.TotalHours >= 90 && dbUser.VoiceOnlineTime.TotalHours < 120 && !dbUser.Hours90Prize)
                {
                    dbUser.Hours90Prize = true;
                    dbUser.GiftCount   += 2;
                }
                if (dbUser.VoiceOnlineTime.TotalHours >= 120 && dbUser.VoiceOnlineTime.TotalHours < 150 && !dbUser.Hours120Prize)
                {
                    dbUser.Hours120Prize = true;
                    dbUser.GiftCount    += 2;
                }
                if (dbUser.VoiceOnlineTime.TotalHours >= 150 && !dbUser.Hours150Prize)
                {
                    dbUser.Hours150Prize = true;
                    dbUser.GiftCount    += 3;
                }
                if (dbUser.GiftCount > currentGiftCount)
                {
                    var embed = new DiscordEmbedBuilder
                    {
                        Author = new DiscordEmbedBuilder.EmbedAuthor
                        {
                            Name = "🎁 Награда",
                        },
                        Description = $"<@{dbUser.Id}> Вы получили {dbUser.GiftCount - currentGiftCount} 🎁 за {(int)(Math.Floor(dbUser.VoiceOnlineTime.TotalHours / 10.0d) * 10)} ч. голосового онлайна",
                        Color       = new DiscordColor("#329EFB"),
                        Timestamp   = DateTime.Now,
                        Footer      = new DiscordEmbedBuilder.EmbedFooter
                        {
                            Text = "http://194.67.207.194",
                        }
                    };
                    var msgChannel = await client.GetChannelAsync(426193823492997121);

                    if (msgChannel == null)
                    {
                        return(dbUser);
                    }
                    await msgChannel.SendMessageAsync(embed : embed);
                }
                return(dbUser);
            }
            catch (Exception ex)
            {
                Log.Error(ex.ToString());
                return(dbUser);
            }
        }
Exemplo n.º 3
0
        private async Task Client_VoiceStateUpdated(VoiceStateUpdateEventArgs e)
        {
            try
            {
                if (e.Guild.Id == botConfig.GuildID && !e.User.IsBot)
                {
                    targetGuild = e.Guild;
                    var currentUser = e.User as DiscordMember;
                    using (var db = new LiteDatabase($"filename={botConfig.GuildID}_voiceStat.db; journal=false;"))
                    {
                        var col = db.GetCollection <UserModel>("Users");
                        col.EnsureIndex(x => x.Id, true);
                        var dbUser = col.FindOne(x => x.Id == currentUser.Id);
                        if (dbUser == null)
                        {
                            var userData = new UserModel
                            {
                                Id                = currentUser.Id,
                                Username          = currentUser.Username,
                                IsConnected       = currentUser.VoiceState != null && currentUser.VoiceState.Channel != null,
                                LastConnectedTime = currentUser.VoiceState != null && currentUser.VoiceState.Channel != null ? (DateTimeOffset?)DateTimeOffset.UtcNow : null,
                                LastUpdateTime    = currentUser.VoiceState != null && currentUser.VoiceState.Channel != null ? (DateTimeOffset?)DateTimeOffset.UtcNow : null,
                                VoiceOnlineTime   = TimeSpan.Zero
                            };
                            col.Insert(userData);
                            dbUser = col.FindOne(x => x.Id == currentUser.Id);
                        }
                        dbUser = await SendTimeGifts(dbUser);

                        if (e.Channel != null)
                        {
                            if (!dbUser.IsConnected)
                            {
                                dbUser.Username          = currentUser.Username;
                                dbUser.IsConnected       = true;
                                dbUser.LastConnectedTime = DateTimeOffset.UtcNow;
                                dbUser.LastUpdateTime    = DateTimeOffset.UtcNow;
                                col.Update(dbUser);
                            }
                        }
                        else
                        {
                            dbUser.Username    = currentUser.Username;
                            dbUser.IsConnected = false;
                            if (dbUser.LastConnectedTime != null)
                            {
                                dbUser.LastSessionTime   = DateTimeOffset.UtcNow - dbUser.LastConnectedTime.Value;
                                dbUser.VoiceOnlineTime  += DateTimeOffset.UtcNow - dbUser.LastUpdateTime.Value;
                                dbUser.LastUpdateTime    = DateTimeOffset.UtcNow;
                                dbUser.LastConnectedTime = null;
                            }
                            col.Update(dbUser);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.ToString());
            }
        }
Exemplo n.º 4
0
        private async Task Client_GuildAvailable(GuildCreateEventArgs e)
        {
            if (TasksIsRunning)
            {
                return;
            }
            try
            {
                if (e.Guild.Id == botConfig.GuildID)
                {
                    Log.Logger.Information($"Handling tasks for {e.Guild.Name} guild");
                    dragonCategory     = e.Guild.Channels.FirstOrDefault(x => x.Id == 408807213553942550);
                    dragonGiftsChannel = e.Guild.Channels.FirstOrDefault(x => x.Id == 408802925289406475);
                    if (botConfig.NadekoDBPath == null || !File.Exists(botConfig.NadekoDBPath))
                    {
                        throw new Exception("Nadeko database file is not found");
                    }
                    prizeRole = e.Guild.Roles.FirstOrDefault(x => x.Name == botConfig.PrizeRoleName);
                    if (prizeRole == null)
                    {
                        throw new Exception("Prize role is not found");
                    }
                    targetGuild = e.Guild;
                    var members = targetGuild.Members.Where(x => !x.IsBot).ToArray();
                    using (var db = new LiteDatabase($"filename={botConfig.GuildID}_voiceStat.db; journal=false;"))
                    {
                        var newUsers     = new List <UserModel>();
                        var currentUsers = ImmutableList.CreateBuilder <UserModel>();
                        var col          = db.GetCollection <UserModel>("Users");
                        col.EnsureIndex(x => x.Id, true);
                        for (int i = 0; i < members.Length; i++)
                        {
                            try
                            {
                                var dbUser = col.FindOne(x => x.Id == members[i].Id);
                                if (dbUser != null)
                                {
                                    if (dbUser.EggRespawnTimes != null && dbUser.EggRespawnTimes.Count >= int.MaxValue - 100)
                                    {
                                        dbUser.EggRespawnTimes.RemoveRange(1, dbUser.EggRespawnTimes.Count - 2);
                                        col.Update(dbUser);
                                    }

                                    if (dbUser.IncubatorRespawnTimes != null && dbUser.IncubatorRespawnTimes.Count >= int.MaxValue - 100)
                                    {
                                        dbUser.IncubatorRespawnTimes.RemoveRange(1, dbUser.IncubatorRespawnTimes.Count - 2);
                                        col.Update(dbUser);
                                    }

                                    if (dbUser.DragonCount > uint.MaxValue - 100)
                                    {
                                        dbUser.DragonCount = uint.MaxValue - dbUser.DragonCount;
                                        col.Update(dbUser);
                                    }
                                }
                                if (dbUser == null)
                                {
                                    var userData = new UserModel
                                    {
                                        Id                = members[i].Id,
                                        Username          = members[i].Username,
                                        IsConnected       = members[i].VoiceState != null && members[i].VoiceState.Channel != null,
                                        LastConnectedTime = members[i].VoiceState != null && members[i].VoiceState.Channel != null ? (DateTimeOffset?)DateTimeOffset.UtcNow : null,
                                        LastUpdateTime    = members[i].VoiceState != null && members[i].VoiceState.Channel != null ? (DateTimeOffset?)DateTimeOffset.UtcNow : null,
                                        VoiceOnlineTime   = TimeSpan.Zero
                                    };
                                    newUsers.Add(userData);
                                }
                                else
                                if (members[i].VoiceState != null && members[i].VoiceState.Channel != null)
                                {
                                    if (!dbUser.IsConnected)
                                    {
                                        dbUser.IsConnected       = true;
                                        dbUser.Username          = members[i].Username;
                                        dbUser.LastConnectedTime = DateTimeOffset.UtcNow;
                                        dbUser.LastUpdateTime    = DateTimeOffset.UtcNow;
                                        currentUsers.Add(dbUser);
                                    }
                                }
                                else
                                {
                                    dbUser.Username          = members[i].Username;
                                    dbUser.IsConnected       = false;
                                    dbUser.LastConnectedTime = null;
                                    dbUser.LastUpdateTime    = DateTimeOffset.UtcNow;
                                    currentUsers.Add(dbUser);
                                }
                            }
                            catch (Exception ex)
                            {
                                Log.Error(ex.ToString());
                                i++;
                            }
                        }
                        col.InsertBulk(newUsers);
                        col.Update(currentUsers);
                    }
                    UpdateUsersOnlineStat();
                    DragonGift();
                    DragonBorn();
                    TasksIsRunning = true;
                    guildRoles     = e.Guild.Roles.Where(x => x.Name == "Dota 2" || x.Name == "CS:GO" || x.Name == "MMORPG" || x.Name == "PUBG" || x.Name == "GTAV" || x.Name == "osu!" || x.Name == "WoT" || x.Name == "Hearthstone" || x.Name == "Paladins" || x.Name == "Overwatch" || x.Name == "LoL" || x.Name == "Dead by Daylight" || x.Name == "уч. Мафии" || x.Name == "Minecraft" || x.Name == "left4dead" || x.Name == "Warcraft 3").ToList();
                    await Task.Delay(3000);

                    SendRoleChooser(415444284305702912);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.ToString());
                await client.DisconnectAsync();
            }
        }