Ejemplo n.º 1
0
        public async Task UnmuteTargetUser(RabbotContext db, IUser user, SocketCommandContext context)
        {
            var mute = db.MutedUsers.AsQueryable().Where(p => p.GuildId == context.Guild.Id && p.UserId == user.Id);

            if (!mute.Any())
            {
                await SendError($"{user.Mention} ist nicht gemuted.", context);

                return;
            }
            else
            {
                var dcGuild      = db.Guilds.FirstOrDefault(p => p.GuildId == context.Guild.Id);
                var dcTargetUser = user as SocketGuildUser;
                var mutedRole    = dcTargetUser.Roles.FirstOrDefault(p => p.Name == "Muted");
                if (mutedRole != null)
                {
                    db.MutedUsers.Remove(mute.FirstOrDefault());
                    await dcTargetUser.RemoveRoleAsync(mutedRole);

                    var oldRoles = mute.FirstOrDefault().Roles.Split('|');
                    await db.SaveChangesAsync();

                    foreach (var oldRole in oldRoles)
                    {
                        var role = context.Guild.Roles.FirstOrDefault(p => p.Name == oldRole);
                        if (role != null)
                        {
                            await dcTargetUser.AddRoleAsync(role);
                        }
                    }
                }
                await Logging.Unmuted(context, user);
            }
        }
Ejemplo n.º 2
0
        public async Task CheckWarnings(RabbotContext db)
        {
            if (!db.Warnings.Any())
            {
                return;
            }

            var warnings = db.Warnings.ToList();

            foreach (var warn in warnings)
            {
                if (warn.Until < DateTime.Now)
                {
                    db.Warnings.Remove(warn);
                    await db.SaveChangesAsync();

                    continue;
                }
                if (warn.Counter >= 3)
                {
                    var dcGuild      = _client.Guilds.FirstOrDefault(p => p.Id == warn.GuildId);
                    var dcTargetUser = dcGuild.Users.FirstOrDefault(p => p.Id == warn.UserId);
                    var dbUser       = db.Features.FirstOrDefault(p => p.GuildId == warn.GuildId && p.UserId == warn.UserId);
                    await _muteService.MuteWarnedUser(db, dcTargetUser, dcGuild);

                    if (dbUser != null)
                    {
                        if (dbUser.Goats >= 100)
                        {
                            dbUser.Goats -= 100;
                        }
                        else
                        {
                            dbUser.Goats = 0;
                        }
                    }
                    db.Warnings.Remove(warn);
                    await db.SaveChangesAsync();

                    continue;
                }
            }
        }
Ejemplo n.º 3
0
        public async Task MuteWarnedUser(RabbotContext db, SocketGuildUser user, SocketGuild guild)
        {
            var mutedRole = guild.Roles.FirstOrDefault(p => p.Name == "Muted");

            if (mutedRole == null)
            {
                return;
            }

            var targetPosition = GetTargetRolePosition(user);
            var botPosition    = GetBotRolePosition(guild.CurrentUser);

            if (!(mutedRole.Position > targetPosition && guild.CurrentUser.GuildPermissions.ManageRoles))
            {
                return;
            }
            if (targetPosition > botPosition)
            {
                return;
            }

            DateTime date     = DateTime.Now;
            DateTime banUntil = date.AddHours(1);

            if (!db.MutedUsers.AsQueryable().Where(p => p.GuildId == guild.Id && p.UserId == user.Id).Any())
            {
                string userRoles = "";
                foreach (var role in user.Roles)
                {
                    if (!role.IsEveryone && !role.IsManaged)
                    {
                        userRoles += role.Name + "|";
                    }
                }
                userRoles = userRoles.TrimEnd('|');
                await db.MutedUsers.AddAsync(new MutedUserEntity { GuildId = guild.Id, UserId = user.Id, Duration = banUntil, Roles = userRoles });
            }
            else
            {
                var ban = db.MutedUsers.FirstOrDefault(p => p.GuildId == guild.Id && p.UserId == user.Id);
                ban.Duration = banUntil;
            }
            await SendPrivate(guild, banUntil, "1 Stunde", user);

            await Logging.WarningMute((SocketGuildUser)user);

            await db.SaveChangesAsync();
        }
Ejemplo n.º 4
0
        public async Task Warn(RabbotContext db, IUser user, SocketCommandContext Context)
        {
            var warn = db.Warnings.FirstOrDefault(p => p.UserId == user.Id && p.GuildId == Context.Guild.Id) ?? db.Warnings.AddAsync(new WarningEntity {
                GuildId = Context.Guild.Id, UserId = user.Id, Until = DateTime.Now.AddHours(1), Counter = 0
            }).Result.Entity;

            warn.Counter++;
            if (warn.Counter > 3)
            {
                return;
            }
            await Context.Message.Channel.SendMessageAsync($"**{user.Mention} du wurdest für schlechtes Benehmen verwarnt. Warnung {warn.Counter}/3**");

            await Logging.Warn(user, Context);

            await db.SaveChangesAsync();
        }
Ejemplo n.º 5
0
        public async Task CheckAttacks(RabbotContext db)
        {
            if (!db.Attacks.Any())
            {
                return;
            }

            var attacks = db.Attacks.ToList();

            foreach (var attack in attacks)
            {
                if (attack.EndTime < DateTime.Now)
                {
                    await AttackResult(attack, db);

                    db.Attacks.Remove(attack);
                    await db.SaveChangesAsync();

                    continue;
                }
            }
        }
Ejemplo n.º 6
0
        public async Task CheckMutes(RabbotContext db)
        {
            if (!db.MutedUsers.Any() || !_client.Guilds.Any())
            {
                return;
            }
            var muteUsers = db.MutedUsers.ToList();

            foreach (var mute in muteUsers)
            {
                var dcGuild = _client.Guilds.FirstOrDefault(p => p.Id == mute.GuildId);
                if (dcGuild == null)
                {
                    db.MutedUsers.Remove(mute);
                    await db.SaveChangesAsync();

                    continue;
                }

                var mutedRole = dcGuild.Roles.FirstOrDefault(p => p.Name == "Muted");
                if (mutedRole == null)
                {
                    continue;
                }
                if (dcGuild.CurrentUser == null)
                {
                    continue;
                }
                int position     = GetBotRolePosition(dcGuild.CurrentUser);
                var dcTargetUser = dcGuild.Users.FirstOrDefault(p => p.Id == mute.UserId);
                if (dcTargetUser == null)
                {
                    if (mute.Duration < DateTime.Now)
                    {
                        db.MutedUsers.Remove(mute);
                        await db.SaveChangesAsync();
                    }
                    continue;
                }

                if (dcGuild.CurrentUser.GuildPermissions.ManageRoles == true && position > mutedRole.Position)
                {
                    if (mute.Duration < DateTime.Now)
                    {
                        db.MutedUsers.Remove(mute);
                        try
                        {
                            await dcTargetUser.RemoveRoleAsync(mutedRole);

                            var oldRoles = mute.Roles.Split('|');
                            foreach (var oldRole in oldRoles)
                            {
                                var role = dcGuild.Roles.FirstOrDefault(p => p.Name == oldRole);
                                if (role != null)
                                {
                                    await dcTargetUser.AddRoleAsync(role);
                                }
                            }
                            await Logging.Unmuted(dcTargetUser);
                        }
                        catch (Exception e)
                        {
                            _logger.Error(e, $"Error while adding roles");
                        }
                    }
                    else
                    {
                        if (!dcTargetUser.Roles.Where(p => p.Id == mutedRole.Id).Any())
                        {
                            try
                            {
                                var oldRoles = mute.Roles.Split('|');
                                foreach (var oldRole in oldRoles)
                                {
                                    if (!oldRole.Contains("everyone"))
                                    {
                                        var role = dcGuild.Roles.FirstOrDefault(p => p.Name == oldRole);
                                        if (role != null)
                                        {
                                            await dcTargetUser.RemoveRoleAsync(role);
                                        }
                                    }
                                }
                                await dcTargetUser.AddRoleAsync(mutedRole);
                            }
                            catch (Exception e)
                            {
                                _logger.Error(e, $"Error while removing roles");
                            }
                        }
                    }
                }
            }
            await db.SaveChangesAsync();
        }
Ejemplo n.º 7
0
        public async Task MuteTargetUser(RabbotContext db, IUser user, string duration, SocketCommandContext context)
        {
            var mutedRole = context.Guild.Roles.FirstOrDefault(p => p.Name == "Muted");

            if (mutedRole == null)
            {
                await SendError($"Es existiert keine Muted Rolle!", context);

                return;
            }
            var dcTargetUser   = user as SocketGuildUser;
            var dcGuild        = context.Guild;
            var targetPosition = GetTargetRolePosition(dcTargetUser);
            var botPosition    = GetBotRolePosition(dcGuild.CurrentUser);

            if (!(mutedRole.Position > targetPosition && dcGuild.CurrentUser.GuildPermissions.ManageRoles))
            {
                await SendError($"Mindestens eine meiner Rollen muss in der Reihenfolge über der Muted Rolle stehen!", context);

                return;
            }
            if (targetPosition > botPosition)
            {
                await SendError($"Es fehlen die Berechtigungen um {dcTargetUser.Mention} zu muten!", context);

                return;
            }
            if (context.User.Id == user.Id)
            {
                await SendError($"{user.Mention} du Trottel kannst dich nicht selber muten!", context);

                return;
            }

            DateTime date = DateTime.Now;
            DateTime banUntil;

            if (duration.Contains('s'))
            {
                banUntil = date.AddSeconds(Convert.ToDouble(duration.Trim('s')));
            }
            else if (duration.Contains('m'))
            {
                banUntil = date.AddMinutes(Convert.ToDouble(duration.Trim('m')));
            }
            else if (duration.Contains('h'))
            {
                banUntil = date.AddHours(Convert.ToDouble(duration.Trim('h')));
            }
            else if (duration.Contains('d'))
            {
                banUntil = date.AddDays(Convert.ToDouble(duration.Trim('d')));
            }
            else
            {
                return;
            }

            if (!db.MutedUsers.AsQueryable().Where(p => p.GuildId == context.Guild.Id && p.UserId == user.Id).Any())
            {
                string userRoles = "";
                foreach (var role in dcTargetUser.Roles)
                {
                    if (!role.IsEveryone && !role.IsManaged)
                    {
                        userRoles += role.Name + "|";
                    }
                }
                userRoles = userRoles.TrimEnd('|');
                await db.MutedUsers.AddAsync(new MutedUserEntity { GuildId = context.Guild.Id, UserId = user.Id, Duration = banUntil, Roles = userRoles });
            }
            else
            {
                var ban = db.MutedUsers.FirstOrDefault(p => p.GuildId == context.Guild.Id && p.UserId == user.Id);
                ban.Duration = banUntil;
            }
            await SendPrivate(dcGuild, banUntil, duration, dcTargetUser);

            await Logging.Mute(context, user, duration);

            await db.SaveChangesAsync();
        }
Ejemplo n.º 8
0
        private async Task AttackResult(AttackEntity attack, RabbotContext db)
        {
            var dcServer = _client.Guilds.FirstOrDefault(p => p.Id == attack.GuildId);

            if (dcServer == null)
            {
                return;
            }
            var dcTarget  = dcServer.Users.FirstOrDefault(p => p.Id == attack.TargetId);
            var dcUser    = dcServer.Users.FirstOrDefault(p => p.Id == attack.UserId);
            var dcChannel = dcServer.Channels.FirstOrDefault(p => p.Id == attack.ChannelId) as ISocketMessageChannel;

            var dbTarget = db.Features.FirstOrDefault(p => p.GuildId == attack.GuildId && p.UserId == attack.TargetId) ?? db.Features.AddAsync(new FeatureEntity {
                Guild = attack.Guild, UserId = attack.TargetId, Exp = 0, Goats = 0
            }).Result.Entity;
            var dbUser = db.Features.FirstOrDefault(p => p.GuildId == attack.GuildId && p.UserId == attack.UserId) ?? db.Features.AddAsync(new FeatureEntity {
                GuildId = attack.GuildId, UserId = attack.UserId, Exp = 0, Goats = 0
            }).Result.Entity;
            var  targetStallBefore = Helper.GetStall(dbTarget.Wins);
            var  userStallBefore   = Helper.GetStall(dbUser.Wins);
            var  inventoryUser     = db.Inventorys.AsQueryable().Join(db.Items, id => id.ItemId, item => item.Id, (Inventory, Item) => new { Inventory, Item }).Where(p => p.Inventory.FeatureId == dbUser.Id);
            var  inventoryTarget   = db.Inventorys.AsQueryable().Join(db.Items, id => id.ItemId, item => item.Id, (Inventory, Item) => new { Inventory, Item }).Where(p => p.Inventory.FeatureId == dbTarget.Id);
            var  atkUser           = userStallBefore.Attack;
            var  defTarget         = targetStallBefore.Defense;
            bool hirtenstab        = false;
            bool zaun = false;

            var dcMessage = dcChannel.CachedMessages.FirstOrDefault(p => p.Id == attack.MessageId) as SocketUserMessage;

            if (dcMessage != null)
            {
                foreach (var reaction in dcMessage.Reactions)
                {
                    var emote = reaction.Key as Emote;

                    if (emote.Id == Constants.Shield.Id)
                    {
                        if (reaction.Value.ReactionCount >= 2)
                        {
                            zaun = true;
                        }
                    }
                    else if (emote.Id == Constants.Sword.Id)
                    {
                        if (reaction.Value.ReactionCount >= 2)
                        {
                            hirtenstab = true;
                        }
                    }
                }
            }

            if (hirtenstab)
            {
                if (inventoryUser.Count() != 0)
                {
                    foreach (var item in inventoryUser)
                    {
                        atkUser += item.Item.Atk;
                        if (item.Inventory.ItemId == 1)
                        {
                            item.Inventory.Durability--;
                            if (item.Inventory.Durability <= 0)
                            {
                                db.Inventorys.Remove(item.Inventory);
                            }
                        }
                    }
                }
            }

            if (zaun)
            {
                if (inventoryTarget.Count() != 0)
                {
                    foreach (var item in inventoryTarget)
                    {
                        defTarget += item.Item.Def;
                        if (item.Inventory.ItemId == 2)
                        {
                            item.Inventory.Durability--;
                            if (item.Inventory.Durability <= 0)
                            {
                                db.Inventorys.Remove(item.Inventory);
                            }
                        }
                    }
                }
            }

            Random rnd = new Random();

            var          sum        = atkUser + defTarget;
            var          winChance  = ((double)atkUser / (double)sum) * 100;
            var          chance     = rnd.Next(1, 101);
            EmbedBuilder embed      = new EmbedBuilder();
            var          rabbotUser = db.Features.FirstOrDefault(p => p.GuildId == dcServer.Id && p.UserId == _client.CurrentUser.Id) ?? db.AddAsync(new FeatureEntity {
                GuildId = dcServer.Id, UserId = _client.CurrentUser.Id, Goats = 0, Exp = 0
            }).Result.Entity;

            if (chance <= winChance)
            {
                int amount = rnd.Next(40, targetStallBefore.MaxOutput + 1);
                if (amount >= dbTarget.Goats)
                {
                    amount = dbTarget.Goats;
                }

                if (!(dcTarget == null || dcUser == null))
                {
                    if (dcChannel != null)
                    {
                        embed.Color = Color.Green;
                        if (!Helper.IsFull(dbUser.Goats + amount, dbUser.Wins))
                        {
                            embed.Description = $"{dcUser.Mention} du hast den **Angriff** gegen {dcTarget.Mention} **gewonnen** und **{amount} Ziegen** erbeutet!";
                        }
                        else
                        {
                            embed.Description = $"{dcUser.Mention} du hast den **Angriff** gegen {dcTarget.Mention} **gewonnen** und **{amount} Ziegen** erbeutet!\nLeider ist **dein Stall voll**. Deswegen sind **{(dbUser.Goats + amount) - Helper.GetStall(dbUser.Wins).Capacity} Ziegen** zu Rabbot geflüchtet.";
                            rabbotUser.Goats += (dbUser.Goats + amount) - Helper.GetStall(dbUser.Wins).Capacity;
                        }
                        await dcChannel.SendMessageAsync(null, false, embed.Build());
                    }
                }

                dbUser.Wins++;
                if (Helper.IsFull(dbUser.Goats + amount, dbUser.Wins))
                {
                    dbUser.Goats = Helper.GetStall(dbUser.Wins).Capacity;
                }
                else
                {
                    dbUser.Goats += amount;
                }
                dbTarget.Goats -= amount;
                dbTarget.Loses++;
            }
            else
            {
                int amount = rnd.Next(40, userStallBefore.MaxOutput + 1);
                if (amount >= dbUser.Goats)
                {
                    amount = dbUser.Goats;
                }
                if (!(dcTarget == null || dcUser == null))
                {
                    if (dcChannel != null)
                    {
                        embed.Color = Color.Red;
                        if (!Helper.IsFull(dbTarget.Goats + amount, dbTarget.Wins))
                        {
                            embed.Description = $"{dcUser.Mention} du hast den **Angriff** gegen {dcTarget.Mention} **verloren** und ihm/ihr **{amount} Ziegen** überlassen..";
                        }
                        else
                        {
                            embed.Description = $"{dcUser.Mention} du hast den **Angriff** gegen {dcTarget.Mention} **verloren** und ihm/ihr **{amount} Ziegen** überlassen..\nLeider ist {dcTarget.Nickname ?? dcTarget.Username}'s **Stall voll**. Deswegen sind **{(dbTarget.Goats + amount) - Helper.GetStall(dbTarget.Wins).Capacity} Ziegen** zu Rabbot geflüchtet.";
                            rabbotUser.Goats += (dbTarget.Goats + amount) - Helper.GetStall(dbTarget.Wins).Capacity;
                        }
                        await dcChannel.SendMessageAsync(null, false, embed.Build());
                    }
                }

                dbTarget.Wins++;
                if (Helper.IsFull(dbTarget.Goats + amount, dbTarget.Wins))
                {
                    dbTarget.Goats = Helper.GetStall(dbTarget.Wins).Capacity;
                }
                else
                {
                    dbTarget.Goats += amount;
                }
                dbUser.Goats -= amount;
                dbUser.Loses++;
            }

            dbUser.Locked   = false;
            dbTarget.Locked = false;
            await db.SaveChangesAsync();

            var targetStallAfter = Helper.GetStall(dbTarget.Wins);
            var userStallAfter   = Helper.GetStall(dbUser.Wins);

            if ((targetStallAfter != targetStallBefore) && dcChannel != null)
            {
                embed.Color       = Color.Green;
                embed.Description = $"{dcTarget.Mention} durch deinen **Sieg** hat sich dein Stall vergrößert! (Neuer Stall: **{targetStallAfter.Name}**)";
                await dcChannel.SendMessageAsync(null, false, embed.Build());
            }
            if ((userStallAfter != userStallBefore) && dcChannel != null)
            {
                embed.Color       = Color.Green;
                embed.Description = $"{dcUser.Mention} durch deinen **Sieg** hat sich dein Stall vergrößert! (Neuer Stall: **{userStallAfter.Name}**)";
                await dcChannel.SendMessageAsync(null, false, embed.Build());
            }
        }