Ejemplo n.º 1
0
        public async Task ShowMekosAsync(EventContext e)
        {
            ulong targetId = e.message.MentionedUserIds.Count > 0 ? e.message.MentionedUserIds.First() : 0;

            if (e.message.MentionedUserIds.Count > 0)
            {
                if (targetId == 0)
                {
                    e.ErrorEmbedResource("miki_module_accounts_mekos_no_user")
                    .QueueToChannel(e.Channel);
                    return;
                }
                IDiscordUser userCheck = await e.Guild.GetUserAsync(targetId);

                if (userCheck.IsBot)
                {
                    e.ErrorEmbedResource("miki_module_accounts_mekos_bot")
                    .QueueToChannel(e.Channel);
                    return;
                }
            }

            using (var context = new MikiContext())
            {
                User user = await User.GetAsync(context, await e.Guild.GetUserAsync(targetId != 0 ? targetId : e.Author.Id));

                IDiscordEmbed embed = new RuntimeEmbed(new EmbedBuilder());
                embed.Title       = "🔸 Mekos";
                embed.Description = e.GetResource("miki_user_mekos", user.Name, user.Currency);
                embed.Color       = new Common.Color(1f, 0.5f, 0.7f);

                embed.QueueToChannel(e.Channel);
                await context.SaveChangesAsync();
            }
        }
Ejemplo n.º 2
0
        public async Task GiveMekosAsync(EventContext e)
        {
            IDiscordUser user = null;

            if (e.Arguments.Take(out string userName))
            {
                user = await DiscordExtensions.GetUserAsync(userName, e.Guild);

                if (user == null)
                {
                    await e.ErrorEmbedResource("give_error_no_mention")
                    .ToEmbed().QueueToChannelAsync(e.Channel);

                    return;
                }

                if (!e.Arguments.Take(out int amount))
                {
                    await e.ErrorEmbedResource("give_error_amount_unparsable")
                    .ToEmbed().QueueToChannelAsync(e.Channel);

                    return;
                }

                using (MikiContext context = new MikiContext())
                {
                    User sender = await DatabaseHelpers.GetUserAsync(context, e.Author);

                    User receiver = await DatabaseHelpers.GetUserAsync(context, user);

                    if (amount <= sender.Currency)
                    {
                        sender.RemoveCurrency(amount);
                        await receiver.AddCurrencyAsync(amount);

                        await new EmbedBuilder()
                        {
                            Title       = "🔸 transaction",
                            Description = e.Locale.GetString("give_description", sender.Name, receiver.Name, amount.ToFormattedString()),
                            Color       = new Color(255, 140, 0),
                        }.ToEmbed().QueueToChannelAsync(e.Channel);

                        await context.SaveChangesAsync();
                    }
                    else
                    {
                        await e.ErrorEmbedResource("user_error_insufficient_mekos")
                        .ToEmbed().QueueToChannelAsync(e.Channel);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public async Task OnBlackjackNew(EventContext e)
        {
            var cache = (ICacheClient)e.Services.GetService(typeof(ICacheClient));
            int?bet   = await ValidateBetAsync(e);

            if (bet.HasValue)
            {
                if (await cache.ExistsAsync($"miki:blackjack:{e.Channel.Id}:{e.Author.Id}"))
                {
                    await e.ErrorEmbedResource(new LanguageResource("blackjack_session_exists"))
                    .ToEmbed().QueueToChannelAsync(e.Channel);

                    return;
                }

                using (var context = new MikiContext())
                {
                    var user = await context.Users.FindAsync(e.Author.Id.ToDbLong());

                    if (user == null)
                    {
                        return;
                    }

                    user.RemoveCurrency(bet.Value);

                    await context.SaveChangesAsync();
                }

                BlackjackManager manager = new BlackjackManager(bet.Value);

                CardHand dealer = manager.AddPlayer(0);
                CardHand player = manager.AddPlayer(e.Author.Id);

                manager.DealAll();
                manager.DealAll();

                dealer.Hand[1].isPublic = false;

                IDiscordMessage message = await manager.CreateEmbed(e)
                                          .ToEmbed().SendToChannel(e.Channel);

                manager.MessageId = message.Id;

                await cache.UpsertAsync($"miki:blackjack:{e.Channel.Id}:{e.Author.Id}", manager.ToContext(), TimeSpan.FromHours(24));
            }
        }
Ejemplo n.º 4
0
        public async Task SetLocale(EventContext e)
        {
            string localeName = e.Arguments.ToString() ?? "";

            if (Locale.LocaleNames.TryGetValue(localeName, out string langId))
            {
                using (var context = new MikiContext())
                    await Locale.SetLanguageAsync(context, e.Channel.Id, langId);

                e.SuccessEmbed(e.Locale.GetString("localization_set", $"`{localeName}`"))
                .QueueToChannel(e.Channel);

                return;
            }
            e.ErrorEmbedResource("error_language_invalid",
                                 localeName,
                                 await e.Prefix.GetForGuildAsync(Global.RedisClient, e.Guild.Id)
                                 ).ToEmbed().QueueToChannel(e.Channel);
        }
Ejemplo n.º 5
0
        public async Task SetLocale(EventContext e)
        {
            var cache = (ICacheClient)e.Services.GetService(typeof(ICacheClient));

            using (var context = new MikiContext())
            {
                string localeName = e.Arguments.Pack.TakeAll() ?? "";

                if (Locale.LocaleNames.TryGetValue(localeName, out string langId))
                {
                    await Locale.SetLanguageAsync(context, e.Channel.Id, langId);

                    await e.SuccessEmbed(e.Locale.GetString("localization_set", $"`{localeName}`"))
                    .QueueToChannelAsync(e.Channel);

                    return;
                }
                await e.ErrorEmbedResource("error_language_invalid",
                                           localeName,
                                           await e.Prefix.GetForGuildAsync(context, cache, e.Guild.Id)
                                           ).ToEmbed().QueueToChannelAsync(e.Channel);
            }
        }
Ejemplo n.º 6
0
        public async Task IAmAsync(EventContext e)
        {
            using (var context = new MikiContext())
            {
                string roleName = e.Arguments.ToString();

                List <IDiscordRole> roles = await GetRolesByName(e.Guild, roleName);

                IDiscordRole role = null;

                if (roles.Count > 1)
                {
                    List <LevelRole> levelRoles = await context.LevelRoles.Where(x => x.GuildId == (long)e.Guild.Id).ToListAsync();

                    if (levelRoles.Where(x => x.Role.Name.ToLower() == roleName.ToLower()).Count() > 1)
                    {
                        e.ErrorEmbed("two roles configured have the same name.")
                        .ToEmbed().QueueToChannel(e.Channel);
                        return;
                    }
                    else
                    {
                        role = levelRoles.Where(x => x.Role.Name.ToLower() == roleName.ToLower()).FirstOrDefault().Role;
                    }
                }
                else
                {
                    role = roles.FirstOrDefault();
                }

                if (role == null)
                {
                    e.ErrorEmbedResource("error_role_null")
                    .ToEmbed().QueueToChannel(e.Channel);
                    return;
                }

                IDiscordGuildUser author = await e.Guild.GetUserAsync(e.Author.Id);

                if (author.RoleIds.Contains(role.Id))
                {
                    e.ErrorEmbed(e.GetResource("error_role_already_given"))
                    .ToEmbed().QueueToChannel(e.Channel);
                    return;
                }

                LevelRole newRole = await context.LevelRoles.FindAsync(e.Guild.Id.ToDbLong(), role.Id.ToDbLong());

                User user = (await context.Users.FindAsync(e.Author.Id.ToDbLong()));

                IDiscordGuildUser discordUser = await e.Guild.GetUserAsync(user.Id.FromDbLong());

                LocalExperience localUser = await LocalExperience.GetAsync(context, e.Guild.Id.ToDbLong(), discordUser);

                if (!newRole?.Optable ?? false)
                {
                    await e.ErrorEmbed(e.GetResource("error_role_forbidden"))
                    .ToEmbed().SendToChannel(e.Channel);

                    return;
                }

                int level = User.CalculateLevel(localUser.Experience);

                if (newRole.RequiredLevel > level)
                {
                    await e.ErrorEmbed(e.GetResource("error_role_level_low", newRole.RequiredLevel - level))
                    .ToEmbed().SendToChannel(e.Channel);

                    return;
                }

                if (newRole.RequiredRole != 0 && !discordUser.RoleIds.Contains(newRole.RequiredRole.FromDbLong()))
                {
                    await e.ErrorEmbed(
                        e.GetResource(
                            "error_role_required",
                            $"**{(await e.Guild.GetRoleAsync(newRole.RequiredRole.FromDbLong())).Name}**"
                            )).ToEmbed().SendToChannel(e.Channel);

                    return;
                }

                if (newRole.Price > 0)
                {
                    if (user.Currency >= newRole.Price)
                    {
                        await e.Channel.SendMessageAsync($"Getting this role costs you {newRole.Price} mekos! type `yes` to proceed.");

                        IDiscordMessage m = await e.EventSystem.GetCommandHandler <MessageListener>().WaitForNextMessage(e.CreateSession());

                        if (m.Content.ToLower()[0] == 'y')
                        {
                            await user.AddCurrencyAsync(-newRole.Price);

                            await context.SaveChangesAsync();
                        }
                        else
                        {
                            await e.ErrorEmbed("Purchase Cancelled")
                            .ToEmbed().SendToChannel(e.Channel);

                            return;
                        }
                    }
                    else
                    {
                        await e.ErrorEmbed(e.GetResource("user_error_insufficient_mekos"))
                        .ToEmbed().SendToChannel(e.Channel);

                        return;
                    }
                }

                await author.AddRoleAsync(newRole.Role);

                Utils.Embed.SetTitle("I AM")
                .SetColor(128, 255, 128)
                .SetDescription($"You're a(n) {role.Name} now!")
                .ToEmbed().QueueToChannel(e.Channel);
            }
        }
Ejemplo n.º 7
0
        public async Task GiveMekosAsync(EventContext e)
        {
            if (e.Arguments.Count < 2)
            {
                e.ErrorEmbedResource("give_error_no_arg")
                .ToEmbed().QueueToChannel(e.Channel);
                return;
            }

            ArgObject arg = e.Arguments.FirstOrDefault();

            IDiscordUser user = null;

            if (arg != null)
            {
                user = await arg.GetUserAsync(e.Guild);
            }

            if (user == null)
            {
                e.ErrorEmbedResource("give_error_no_mention")
                .ToEmbed().QueueToChannel(e.Channel);
                return;
            }

            arg = arg.Next();

            int?amount = arg?.AsInt() ?? null;

            if (amount == null)
            {
                e.ErrorEmbedResource("give_error_amount_unparsable")
                .ToEmbed().QueueToChannel(e.Channel);
                return;
            }

            if (amount <= 0)
            {
                e.ErrorEmbedResource("give_error_min_mekos")
                .ToEmbed().QueueToChannel(e.Channel);
                return;
            }

            using (MikiContext context = new MikiContext())
            {
                User sender = await User.GetAsync(context, e.Author);

                User receiver = await User.GetAsync(context, user);

                if (amount.Value <= sender.Currency)
                {
                    await sender.AddCurrencyAsync(-amount.Value, e.Channel, sender);

                    await receiver.AddCurrencyAsync(amount.Value, e.Channel, sender);

                    new EmbedBuilder()
                    {
                        Title       = "🔸 transaction",
                        Description = e.GetResource("give_description", sender.Name, receiver.Name, amount.Value),
                        Color       = new Color(255, 140, 0),
                    }.ToEmbed().QueueToChannel(e.Channel);

                    await context.SaveChangesAsync();
                }
                else
                {
                    e.ErrorEmbedResource("user_error_insufficient_mekos")
                    .ToEmbed().QueueToChannel(e.Channel);
                }
            }
        }
Ejemplo n.º 8
0
        public async Task GiveReputationAsync(EventContext e)
        {
            using (var context = new MikiContext())
            {
                User giver = await context.Users.FindAsync(e.Author.Id.ToDbLong());

                var repObject = Global.RedisClient.Get <ReputationObject>($"user:{giver.Id}:rep");

                if (repObject == null)
                {
                    repObject = new ReputationObject()
                    {
                        LastReputationGiven  = DateTime.Now,
                        ReputationPointsLeft = 3
                    };
                    await Global.RedisClient.AddAsync($"user:{giver.Id}:rep", repObject, new DateTimeOffset(DateTime.UtcNow.AddDays(1).Date));
                }

                ArgObject arg = e.Arguments.FirstOrDefault();

                if (arg == null)
                {
                    TimeSpan pointReset = (DateTime.Now.AddDays(1).Date - DateTime.Now);

                    new EmbedBuilder()
                    {
                        Title       = (e.GetResource("miki_module_accounts_rep_header")),
                        Description = (e.GetResource("miki_module_accounts_rep_description"))
                    }.AddInlineField(e.GetResource("miki_module_accounts_rep_total_received"), giver.Reputation.ToString())
                    .AddInlineField(e.GetResource("miki_module_accounts_rep_reset"), pointReset.ToTimeString(e.Channel.Id))
                    .AddInlineField(e.GetResource("miki_module_accounts_rep_remaining"), repObject.ReputationPointsLeft)
                    .ToEmbed().QueueToChannel(e.Channel);
                    return;
                }
                else
                {
                    Dictionary <IDiscordUser, int> usersMentioned = new Dictionary <IDiscordUser, int>();

                    EmbedBuilder embed = new EmbedBuilder();

                    int  totalAmountGiven = 0;
                    bool mentionedSelf    = false;

                    while (true || totalAmountGiven <= repObject.ReputationPointsLeft)
                    {
                        if (arg == null)
                        {
                            break;
                        }

                        IDiscordUser u = await arg.GetUserAsync(e.Guild);

                        int amount = 1;

                        if (u == null)
                        {
                            break;
                        }

                        arg = arg?.Next();

                        if ((arg?.AsInt() ?? -1) != -1)
                        {
                            amount = arg.AsInt().Value;
                            arg    = arg.Next();
                        }
                        else if (Utils.IsAll(arg))
                        {
                            amount = repObject.ReputationPointsLeft;
                            arg    = arg.Next();
                        }

                        if (u.Id == e.Author.Id)
                        {
                            mentionedSelf = true;
                            continue;
                        }

                        totalAmountGiven += amount;

                        if (usersMentioned.Keys.Where(x => x.Id == u.Id).Count() > 0)
                        {
                            usersMentioned[usersMentioned.Keys.Where(x => x.Id == u.Id).First()] += amount;
                        }
                        else
                        {
                            usersMentioned.Add(u, amount);
                        }
                    }

                    if (mentionedSelf)
                    {
                        embed.Footer = new EmbedFooter()
                        {
                            Text = e.GetResource("warning_mention_self"),
                        };
                    }

                    if (usersMentioned.Count == 0)
                    {
                        return;
                    }
                    else
                    {
                        if (totalAmountGiven <= 0)
                        {
                            e.ErrorEmbedResource("miki_module_accounts_rep_error_zero")
                            .ToEmbed().QueueToChannel(e.Channel);
                            return;
                        }

                        if (usersMentioned.Sum(x => x.Value) > repObject.ReputationPointsLeft)
                        {
                            e.ErrorEmbedResource("error_rep_limit", usersMentioned.Count, usersMentioned.Sum(x => x.Value), repObject.ReputationPointsLeft)
                            .ToEmbed().QueueToChannel(e.Channel);
                            return;
                        }
                    }

                    embed.Title       = (e.GetResource("miki_module_accounts_rep_header"));
                    embed.Description = (e.GetResource("rep_success"));

                    foreach (var user in usersMentioned)
                    {
                        User receiver = await User.GetAsync(context, user.Key);

                        receiver.Reputation += user.Value;

                        embed.AddInlineField(receiver.Name, string.Format("{0} => {1} (+{2})", receiver.Reputation - user.Value, receiver.Reputation, user.Value));
                    }

                    repObject.ReputationPointsLeft -= (short)(usersMentioned.Sum(x => x.Value));

                    await Global.RedisClient.AddAsync($"user:{giver.Id}:rep", repObject, new DateTimeOffset(DateTime.UtcNow.AddDays(1).Date));

                    embed.AddInlineField(e.GetResource("miki_module_accounts_rep_points_left"), repObject.ReputationPointsLeft)
                    .ToEmbed().QueueToChannel(e.Channel);

                    await context.SaveChangesAsync();
                }
            }
        }
Ejemplo n.º 9
0
        public async Task LotteryAsync(EventContext e)
        {
            ArgObject arg = e.Arguments.FirstOrDefault();

            if (arg == null)
            {
                long totalTickets = await(Global.RedisClient as StackExchangeCacheClient).Client.GetDatabase(0).ListLengthAsync(lotteryKey);
                long yourTickets  = 0;

                string latestWinner = (Global.RedisClient as StackExchangeCacheClient).Client.GetDatabase(0).StringGet("lottery:winner");

                if (await lotteryDict.ContainsAsync(e.Author.Id))
                {
                    yourTickets = long.Parse(await lotteryDict.GetAsync(e.Author.Id));
                }

                string timeLeft = taskScheduler?.GetInstance(0, lotteryId).TimeLeft.ToTimeString(e.Channel.Id, true) ?? "1h?m?s - will be fixed soon!";

                new EmbedBuilder()
                {
                    Title       = "🍀 Lottery",
                    Description = "Make the biggest gamble, and get paid off massively if legit.",
                    Color       = new Color(119, 178, 85)
                }.AddInlineField("Tickets Owned", yourTickets)
                .AddInlineField("Drawing In", timeLeft)
                .AddInlineField("Total Tickets", totalTickets)
                .AddInlineField("Ticket price", $"{100} mekos")
                .AddInlineField("Latest Winner", latestWinner ?? "no name")
                .AddInlineField("How to buy?", ">lottery buy [amount]")
                .ToEmbed().QueueToChannel(e.Channel);
                return;
            }

            switch (arg.Argument.ToLower())
            {
            case "buy":
            {
                arg = arg.Next();
                int amount = arg?.AsInt() ?? 1;

                if (amount < 1)
                {
                    amount = 1;
                }

                using (var context = new MikiContext())
                {
                    User u = await User.GetAsync(context, e.Author);

                    if (amount * 100 > u.Currency)
                    {
                        e.ErrorEmbedResource("miki_mekos_insufficient")
                        .ToEmbed().QueueToChannel(e.Channel);
                        return;
                    }

                    await u.AddCurrencyAsync(-amount * 100, e.Channel);

                    RedisValue[] tickets = new RedisValue[amount];

                    for (int i = 0; i < amount; i++)
                    {
                        tickets[i] = e.Author.Id.ToString();
                    }

                    await(Global.RedisClient as StackExchangeCacheClient).Client.GetDatabase(0).ListRightPushAsync(lotteryKey, tickets);

                    int totalTickets = 0;

                    if (await lotteryDict.ContainsAsync(e.Author.Id.ToString()))
                    {
                        totalTickets = int.Parse(await lotteryDict.GetAsync(e.Author.Id.ToString()));
                    }

                    await lotteryDict.AddAsync(e.Author.Id, amount + totalTickets);

                    await context.SaveChangesAsync();

                    Utils.SuccessEmbed(e.Channel.Id, $"Successfully bought {amount} tickets!")
                    .QueueToChannel(e.Channel);
                }
            }
            break;
            }
        }
Ejemplo n.º 10
0
        public async Task GiveReputationAsync(EventContext e)
        {
            using (var context = new MikiContext())
            {
                User giver = await context.Users.FindAsync(e.Author.Id.ToDbLong());

                var cache = (ICacheClient)e.Services
                            .GetService(typeof(ICacheClient));


                var repObject = await cache.GetAsync <ReputationObject>($"user:{giver.Id}:rep");

                if (repObject == null)
                {
                    repObject = new ReputationObject()
                    {
                        LastReputationGiven  = DateTime.Now,
                        ReputationPointsLeft = 3
                    };

                    await cache.UpsertAsync(
                        $"user:{giver.Id}:rep",
                        repObject,
                        DateTime.UtcNow.AddDays(1).Date - DateTime.UtcNow
                        );
                }

                if (!e.Arguments.CanTake)
                {
                    TimeSpan pointReset = (DateTime.Now.AddDays(1).Date - DateTime.Now);

                    await new EmbedBuilder()
                    {
                        Title       = e.Locale.GetString("miki_module_accounts_rep_header"),
                        Description = e.Locale.GetString("miki_module_accounts_rep_description")
                    }.AddInlineField(e.Locale.GetString("miki_module_accounts_rep_total_received"), giver.Reputation.ToFormattedString())
                    .AddInlineField(e.Locale.GetString("miki_module_accounts_rep_reset"), pointReset.ToTimeString(e.Locale).ToString())
                    .AddInlineField(e.Locale.GetString("miki_module_accounts_rep_remaining"), repObject.ReputationPointsLeft.ToString())
                    .ToEmbed().QueueToChannelAsync(e.Channel);
                    return;
                }
                else
                {
                    Dictionary <IDiscordUser, short> usersMentioned = new Dictionary <IDiscordUser, short>();

                    EmbedBuilder embed = new EmbedBuilder();

                    int  totalAmountGiven = 0;
                    bool mentionedSelf    = false;

                    while (e.Arguments.CanTake && totalAmountGiven <= repObject.ReputationPointsLeft)
                    {
                        short amount = 1;

                        e.Arguments.Take(out string userName);

                        var u = await DiscordExtensions.GetUserAsync(userName, e.Guild);

                        if (u == null)
                        {
                            throw new UserNullException();
                        }

                        if (e.Arguments.Take(out int value))
                        {
                            amount = (short)value;
                        }
                        else if (Utils.IsAll(e.Arguments.Peek <string>()))
                        {
                            amount = repObject.ReputationPointsLeft;
                            e.Arguments.Skip();
                        }

                        if (u.Id == e.Author.Id)
                        {
                            mentionedSelf = true;
                            continue;
                        }

                        totalAmountGiven += amount;

                        if (usersMentioned.Keys.Where(x => x.Id == u.Id).Count() > 0)
                        {
                            usersMentioned[usersMentioned.Keys.Where(x => x.Id == u.Id).First()] += amount;
                        }
                        else
                        {
                            usersMentioned.Add(u, amount);
                        }
                    }

                    if (mentionedSelf)
                    {
                        embed.Footer = new EmbedFooter()
                        {
                            Text = e.Locale.GetString("warning_mention_self"),
                        };
                    }

                    if (usersMentioned.Count == 0)
                    {
                        return;
                    }
                    else
                    {
                        if (totalAmountGiven <= 0)
                        {
                            await e.ErrorEmbedResource("miki_module_accounts_rep_error_zero")
                            .ToEmbed().QueueToChannelAsync(e.Channel);

                            return;
                        }

                        if (usersMentioned.Sum(x => x.Value) > repObject.ReputationPointsLeft)
                        {
                            await e.ErrorEmbedResource("error_rep_limit", usersMentioned.Count, usersMentioned.Sum(x => x.Value), repObject.ReputationPointsLeft)
                            .ToEmbed().QueueToChannelAsync(e.Channel);

                            return;
                        }
                    }

                    embed.Title       = (e.Locale.GetString("miki_module_accounts_rep_header"));
                    embed.Description = (e.Locale.GetString("rep_success"));

                    foreach (var u in usersMentioned)
                    {
                        User receiver = await DatabaseHelpers.GetUserAsync(context, u.Key);

                        receiver.Reputation += u.Value;

                        embed.AddInlineField(
                            receiver.Name,
                            string.Format("{0} => {1} (+{2})", (receiver.Reputation - u.Value).ToFormattedString(), receiver.Reputation.ToFormattedString(), u.Value)
                            );
                    }

                    repObject.ReputationPointsLeft -= (short)usersMentioned.Sum(x => x.Value);

                    await cache.UpsertAsync(
                        $"user:{giver.Id}:rep",
                        repObject,
                        DateTime.UtcNow.AddDays(1).Date - DateTime.UtcNow
                        );

                    await embed.AddInlineField(e.Locale.GetString("miki_module_accounts_rep_points_left"), repObject.ReputationPointsLeft.ToString())
                    .ToEmbed().QueueToChannelAsync(e.Channel);

                    await context.SaveChangesAsync();
                }
            }
        }
Ejemplo n.º 11
0
        public async Task IAmAsync(EventContext e)
        {
            using (var context = new MikiContext())
            {
                string roleName = e.Arguments.ToString();

                List <IDiscordRole> roles = await GetRolesByName(e.Guild, roleName);

                IDiscordRole role = null;

                // checking if the role has a duplicate name.
                if (roles.Count > 1)
                {
                    List <LevelRole> levelRoles = await context.LevelRoles.Where(x => x.GuildId == (long)e.Guild.Id).ToListAsync();

                    if (levelRoles.Where(x => x.GetRoleAsync().Result.Name.ToLower() == roleName.ToLower()).Count() > 1)
                    {
                        await e.ErrorEmbed("two roles configured have the same name.")
                        .ToEmbed().QueueToChannelAsync(e.Channel);

                        return;
                    }
                    else
                    {
                        role = levelRoles.Where(x => x.GetRoleAsync().Result.Name.ToLower() == roleName.ToLower()).FirstOrDefault().GetRoleAsync().Result;
                    }
                }
                else
                {
                    role = roles.FirstOrDefault();
                }

                // checking if the role is null
                if (role == null)
                {
                    await e.ErrorEmbedResource("error_role_null")
                    .ToEmbed().QueueToChannelAsync(e.Channel);

                    return;
                }

                IDiscordGuildUser author = await e.Guild.GetMemberAsync(e.Author.Id);

                if (author.RoleIds.Contains(role.Id))
                {
                    await e.ErrorEmbed(e.Locale.GetString("error_role_already_given"))
                    .ToEmbed().QueueToChannelAsync(e.Channel);

                    return;
                }

                LevelRole newRole = await context.LevelRoles.FindAsync(e.Guild.Id.ToDbLong(), role.Id.ToDbLong());

                IDiscordRole discordRole = await newRole.GetRoleAsync();

                User user = (await context.Users.FindAsync(e.Author.Id.ToDbLong()));

                IDiscordGuildUser discordUser = await e.Guild.GetMemberAsync(user.Id.FromDbLong());

                LocalExperience localUser = await LocalExperience.GetAsync(context, e.Guild.Id.ToDbLong(), discordUser.Id.ToDbLong(), discordUser.Username);

                if (!newRole?.Optable ?? false)
                {
                    await e.ErrorEmbed(e.Locale.GetString("error_role_forbidden"))
                    .ToEmbed().SendToChannel(e.Channel);

                    return;
                }

                int level = User.CalculateLevel(localUser.Experience);

                if (newRole.RequiredLevel > level)
                {
                    await e.ErrorEmbed(e.Locale.GetString("error_role_level_low", newRole.RequiredLevel - level))
                    .ToEmbed().SendToChannel(e.Channel);

                    return;
                }

                if (newRole.RequiredRole != 0 && !discordUser.RoleIds.Contains(newRole.RequiredRole.FromDbLong()))
                {
                    var requiredRole = await e.Guild.GetRoleAsync(newRole.RequiredRole.FromDbLong());

                    await e.ErrorEmbed(
                        e.Locale.GetString(
                            "error_role_required", $"**{requiredRole.Name}**"
                            )
                        ).ToEmbed().QueueToChannelAsync(e.Channel);

                    return;
                }

                if (newRole.Price > 0)
                {
                    if (user.Currency >= newRole.Price)
                    {
                        user.RemoveCurrency(newRole.Price);
                        await context.SaveChangesAsync();
                    }
                    else
                    {
                        await e.ErrorEmbed(e.Locale.GetString("user_error_insufficient_mekos"))
                        .ToEmbed().SendToChannel(e.Channel);

                        return;
                    }
                }

                var me = await e.Guild.GetSelfAsync();

                if (!await me.HasPermissionsAsync(GuildPermission.ManageRoles))
                {
                    await e.ErrorEmbed(e.Locale.GetString("permission_error_low", "give roles")).ToEmbed()
                    .QueueToChannelAsync(e.Channel);

                    return;
                }

                if (discordRole.Position >= await me.GetHierarchyAsync())
                {
                    await e.ErrorEmbed(e.Locale.GetString("permission_error_low", "give roles")).ToEmbed()
                    .QueueToChannelAsync(e.Channel);

                    return;
                }

                await author.AddRoleAsync(discordRole);

                await new EmbedBuilder()
                .SetTitle("I AM")
                .SetColor(128, 255, 128)
                .SetDescription($"You're a(n) {role.Name} now!")
                .ToEmbed().QueueToChannelAsync(e.Channel);
            }
        }
Ejemplo n.º 12
0
        public async Task GiveMekosAsync(EventContext e)
        {
            Locale locale = new Locale(e.Guild.Id);

            string[] arguments = e.arguments.Split(' ');

            if (arguments.Length < 2)
            {
                e.ErrorEmbedResource("give_error_no_arg")
                .QueueToChannel(e.Channel);
                return;
            }

            if (e.message.MentionedUserIds.Count <= 0)
            {
                e.ErrorEmbedResource("give_error_no_mention")
                .QueueToChannel(e.Channel);
                return;
            }

            if (!int.TryParse(arguments[1], out int goldSent))
            {
                e.ErrorEmbedResource("give_error_amount_unparsable")
                .QueueToChannel(e.Channel);
                return;
            }

            if (goldSent > 999999)
            {
                e.ErrorEmbedResource("give_error_max_mekos")
                .QueueToChannel(e.Channel);
                return;
            }

            if (goldSent <= 0)
            {
                e.ErrorEmbedResource("give_error_min_mekos")
                .QueueToChannel(e.Channel);
                return;
            }

            using (MikiContext context = new MikiContext())
            {
                User sender = await User.GetAsync(context, e.Author);

                User receiver = await User.GetAsync(context, await e.Guild.GetUserAsync(e.message.MentionedUserIds.First()));

                if (goldSent <= sender.Currency)
                {
                    await sender.AddCurrencyAsync(-goldSent, e.Channel, sender);

                    IDiscordEmbed em = Utils.Embed;
                    em.Title       = "🔸 transaction";
                    em.Description = e.GetResource("give_description", sender.Name, receiver.Name, goldSent);

                    em.Color = new Miki.Common.Color(255, 140, 0);

                    em.QueueToChannel(e.Channel);
                    await context.SaveChangesAsync();
                }
                else
                {
                    e.ErrorEmbedResource("user_error_insufficient_mekos")
                    .QueueToChannel(e.Channel);
                }
            }
        }
Ejemplo n.º 13
0
        public async Task GiveReputationAsync(EventContext e)
        {
            using (var context = new MikiContext())
            {
                Locale locale = new Locale(e.Channel.Id.ToDbLong());

                User giver = await context.Users.FindAsync(e.Author.Id.ToDbLong());

                List <ulong> mentionedUsers = e.message.MentionedUserIds.ToList();
                string[]     args           = e.arguments.Split(' ');
                short        repAmount      = 1;

                bool mentionedSelf = mentionedUsers.RemoveAll(x => x == e.Author.Id) > 0;

                var repObject = Global.redisClient.Get <ReputationObject>($"user:{giver.Id}:rep");

                if (repObject == null)
                {
                    repObject = new ReputationObject()
                    {
                        LastReputationGiven  = DateTime.Now,
                        ReputationPointsLeft = 3
                    };
                }

                IDiscordEmbed embed = Utils.Embed;

                if (mentionedSelf)
                {
                    embed.SetFooter(e.GetResource("warning_mention_self"), "");
                }

                if (mentionedUsers.Count == 0)
                {
                    TimeSpan pointReset = (DateTime.Now.AddDays(1).Date - DateTime.Now);

                    embed.SetTitle(locale.GetString("miki_module_accounts_rep_header"))
                    .SetDescription(locale.GetString("miki_module_accounts_rep_description"))
                    .AddInlineField(locale.GetString("miki_module_accounts_rep_total_received"), giver.Reputation.ToString())
                    .AddInlineField(locale.GetString("miki_module_accounts_rep_reset"), pointReset.ToTimeString(e.Channel.GetLocale()))
                    .AddInlineField(locale.GetString("miki_module_accounts_rep_remaining"), repObject.ReputationPointsLeft)
                    .QueueToChannel(e.Channel);
                    return;
                }
                else
                {
                    if (args.Length > 1)
                    {
                        if (Utils.IsAll(args[args.Length - 1], e.Channel.GetLocale()))
                        {
                            repAmount = repObject.ReputationPointsLeft;
                        }
                        else if (short.TryParse(args[1], out short x))
                        {
                            repAmount = x;
                        }
                    }

                    if (repAmount <= 0)
                    {
                        e.ErrorEmbedResource("miki_module_accounts_rep_error_zero")
                        .QueueToChannel(e.Channel);
                        return;
                    }

                    if (mentionedUsers.Count * repAmount > repObject.ReputationPointsLeft)
                    {
                        e.ErrorEmbedResource("error_rep_limit", mentionedUsers.Count, repAmount, repObject.ReputationPointsLeft)
                        .QueueToChannel(e.Channel);
                        return;
                    }
                }

                embed.SetTitle(locale.GetString("miki_module_accounts_rep_header"))
                .SetDescription(locale.GetString("rep_success"));

                foreach (ulong user in mentionedUsers)
                {
                    IDiscordUser u = await e.Guild.GetUserAsync(user);

                    User receiver = await User.GetAsync(context, u);

                    receiver.Reputation += repAmount;

                    embed.AddInlineField(receiver.Name, string.Format("{0} => {1} (+{2})", receiver.Reputation - repAmount, receiver.Reputation, repAmount));
                }

                repObject.ReputationPointsLeft -= (short)(repAmount * mentionedUsers.Count);

                await Global.redisClient.AddAsync($"user:{giver.Id}:rep", repObject);

                embed.AddInlineField(locale.GetString("miki_module_accounts_rep_points_left"), repObject.ReputationPointsLeft)
                .QueueToChannel(e.Channel);

                await context.SaveChangesAsync();
            }
        }