private async Task ReputationInfoScreenAsync( IContext e, User giver, ReputationObject repObject) { var locale = e.GetLocale(); var pointReset = DateTime.Now.AddDays(1).Date - DateTime.Now; await new EmbedBuilder() .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("N0")) .AddInlineField( locale.GetString("miki_module_accounts_rep_reset"), pointReset.ToTimeString(locale)) .AddInlineField( locale.GetString("miki_module_accounts_rep_remaining"), repObject.ReputationPointsLeft.ToString()) .ToEmbed() .QueueAsync(e, e.GetChannel()); }
/// <inheritdoc /> public async ValueTask <ReputationObject> GetUserReputationPointsAsync(long userId) { var repObject = await cache.GetAsync <ReputationObject>($"user:{userId}:rep"); if (repObject != null) { return(repObject); } repObject = new ReputationObject { LastReputationGiven = DateTime.Now, ReputationPointsLeft = 3 }; await cache.UpsertAsync( $"user:{userId}:rep", repObject, DateTime.UtcNow.AddDays(1).Date - DateTime.UtcNow ); return(repObject); }
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(); } } }
public async Task GiveReputationAsync(CommandContext e) { var context = e.GetService <MikiDbContext>(); User giver = await context.Users.FindAsync(e.Author.Id.ToDbLong()); var cache = e.GetService <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)) { if (value > 0) { amount = (short)value; } } else if (e.Arguments.Peek(out string arg)) { if (Utils.IsAll(arg)) { amount = (short)(repObject.ReputationPointsLeft - ((short)usersMentioned.Sum(x => x.Value))); 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(); } }
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(); } }