public static async Task SyncAvatarAsync(IDiscordUser user) { PutObjectRequest request = new PutObjectRequest(); request.BucketName = "miki-cdn"; request.Key = $"avatars/{user.Id}.png"; request.ContentType = "image/png"; request.CannedACL = new S3CannedACL("public-read"); string avatarUrl = user.GetAvatarUrl(); using (var client = new Rest.RestClient(avatarUrl, true)) { request.InputStream = await client.GetStreamAsync(); } var response = await Global.CdnClient.PutObjectAsync(request); if (response.HttpStatusCode != System.Net.HttpStatusCode.OK) { throw new AvatarSyncException(); } await MikiApp.Instance.GetService <BunnyCDNClient>() .PurgeCacheAsync($"https://mikido.b-cdn.net/avatars/{user.Id}.png"); using (var context = new MikiContext()) { User u = await User.GetAsync(context, user.Id, user.Username); u.AvatarUrl = u.Id.ToString(); await context.SaveChangesAsync(); } }
public static async Task SyncAvatarAsync(IDiscordUser user) { PutObjectRequest request = new PutObjectRequest(); request.BucketName = "miki-cdn"; request.Key = $"avatars/{user.Id}.png"; request.ContentType = "image/png"; request.CannedACL = new S3CannedACL("public-read"); string avatarUrl = user.GetAvatarUrl(); using (var client = new Rest.RestClient(avatarUrl, true)) { request.InputStream = await client.GetStreamAsync(); } var response = await Global.CdnClient.PutObjectAsync(request); if (response.HttpStatusCode != System.Net.HttpStatusCode.OK) { throw new AvatarSyncException(); } using (var context = new MikiContext()) { User u = await User.GetAsync(context, user); u.HeaderUrl = u.Id.ToString(); await context.SaveChangesAsync(); } await Global.RedisClient.UpsertAsync($"avatar:{user.Id}:synced", true); }
public async Task AchievementsAsync(EventContext e) { using (var context = new MikiContext()) { long id = (long)e.Author.Id; ArgObject arg = e.Arguments.FirstOrDefault(); if (arg != null) { IDiscordUser user = await arg.TakeUntilEnd() .GetUserAsync(e.Guild); if (user != null) { id = (long)user.Id; } } IDiscordUser discordUser = await e.Guild.GetUserAsync(id.FromDbLong()); User u = await User.GetAsync(context, discordUser); List <Achievement> achievements = await context.Achievements .Where(x => x.Id == id) .ToListAsync(); EmbedBuilder embed = Utils.Embed .SetAuthor($"{u.Name} | " + "Achievements", discordUser.GetAvatarUrl(), "https://miki.ai/profiles/ID/achievements"); embed.SetColor(255, 255, 255); StringBuilder leftBuilder = new StringBuilder(); int totalScore = 0; foreach (var a in achievements) { BaseAchievement metadata = AchievementManager.Instance.GetContainerById(a.Name).Achievements[a.Rank]; leftBuilder.AppendLine(metadata.Icon + " | `" + metadata.Name.PadRight(15) + $"{metadata.Points.ToString().PadLeft(3)} pts` | 📅 {a.UnlockedAt.ToShortDateString()}"); totalScore += metadata.Points; } if (string.IsNullOrEmpty(leftBuilder.ToString())) { embed.AddInlineField("Total Pts: " + totalScore, "None, yet."); } else { embed.AddInlineField("Total Pts: " + totalScore, leftBuilder.ToString()); } embed.ToEmbed().QueueToChannel(e.Channel); } }
public async Task AchievementsAsync(CommandContext e) { var context = e.GetService <MikiDbContext>(); long id = (long)e.Author.Id; if (e.Arguments.Take(out string arg)) { IDiscordUser user = await DiscordExtensions.GetUserAsync(arg, e.Guild); if (user != null) { id = (long)user.Id; } } IDiscordUser discordUser = await e.Guild.GetMemberAsync(id.FromDbLong()); User u = await User.GetAsync(context, discordUser.Id, discordUser.Username); List <Achievement> achievements = await context.Achievements .Where(x => x.UserId == id) .ToListAsync(); EmbedBuilder embed = new EmbedBuilder() .SetAuthor($"{u.Name} | " + "Achievements", discordUser.GetAvatarUrl(), "https://miki.ai/profiles/ID/achievements"); embed.SetColor(255, 255, 255); StringBuilder leftBuilder = new StringBuilder(); int totalScore = 0; foreach (var a in achievements) { IAchievement metadata = AchievementManager.Instance.GetContainerById(a.Name).Achievements[a.Rank]; leftBuilder.AppendLine(metadata.Icon + " | `" + metadata.Name.PadRight(15) + $"{metadata.Points.ToString().PadLeft(3)} pts` | 📅 {a.UnlockedAt.ToShortDateString()}"); totalScore += metadata.Points; } if (string.IsNullOrEmpty(leftBuilder.ToString())) { embed.AddInlineField("Total Pts: " + totalScore.ToFormattedString(), "None, yet."); } else { embed.AddInlineField("Total Pts: " + totalScore.ToFormattedString(), leftBuilder.ToString()); } await embed.ToEmbed().QueueToChannelAsync(e.Channel); }
public GamblingModule() { if (!Global.Config.IsMainBot) { lotteryId = taskScheduler.AddTask(0, (s) => { long size = (Global.RedisClient as StackExchangeCacheClient).Client.GetDatabase(0).ListLength(lotteryKey); if (size == 0) { return; } string value = (Global.RedisClient as StackExchangeCacheClient).Client.GetDatabase(0).ListGetByIndex(lotteryKey, MikiRandom.Next(size)); ulong winnerId = ulong.Parse(value); int wonAmount = (int)Math.Round(size * 100 * 0.75); IDiscordUser user = null; //Bot.Instance.ChatClient.GetUser(winnerId); using (var context = new MikiContext()) { long id = winnerId.ToDbLong(); User profileUser = context.Users.Find(id); if (user != null) { IDiscordChannel channel = user.GetDMChannel().Result; EmbedBuilder embed = new EmbedBuilder() { Author = new EmbedAuthor() { Name = "Winner winner chicken dinner", IconUrl = user.GetAvatarUrl() }, Description = $"Wow! You won the lottery and gained {wonAmount} mekos!" }; profileUser.AddCurrencyAsync(wonAmount, channel); embed.ToEmbed().QueueToChannel(channel); context.SaveChanges(); Global.RedisClient.RemoveAsync(lotteryKey); Global.RedisClient.UpsertAsync("lottery:winner", profileUser.Name ?? "unknown"); lotteryDict.ClearAsync(); var lotteryAchievement = AchievementManager.Instance.GetContainerById("lottery"); if (wonAmount > 100000) { lotteryAchievement.Achievements[0].UnlockAsync(channel, user, 0); } if (wonAmount > 10000000) { lotteryAchievement.Achievements[1].UnlockAsync(channel, user, 1); } if (wonAmount > 250000000) { lotteryAchievement.Achievements[2].UnlockAsync(channel, user, 1); } } } }, "", new TimeSpan(0, 1, 0, 0), true); } }
/// <summary> /// Add or update the warning message in Discord. /// </summary> /// <param name="context">The message context.</param> /// <param name="warning">The warning to update.</param> /// <param name="user">The warned user.</param> /// <param name="moderator">The moderator.</param> /// <returns>The message builder.</returns> public async Task UpdateWarningMessageAsync( MessageContext context, UserWarning warning, IDiscordUser user, IDiscordUser moderator) { var title = _localizer["Moderation.Warn.Title"] .WithToken("user", user) .WithToken("id", warning.Id); var footer = _localizer["Moderation.Warn.Footer"] .WithToken("moderator", moderator); string displayReason; if (!string.IsNullOrEmpty(warning.Reason)) { displayReason = warning.Reason; } else { displayReason = _localizer["Moderation.Warn.NoReason"] .WithToken("id", warning.Id) .WithToken("prefix", ">") // TODO: Get the current prefix. .ToString(); } bool updateEntity; MessageBuilder builder; if (warning.ConsoleChannelId.HasValue && warning.ConsoleMessageId.HasValue) { builder = context.Response.Edit(warning.ConsoleChannelId.Value, warning.ConsoleMessageId.Value); updateEntity = false; } else { var guildId = context.Request.GuildId ?? throw new InvalidOperationException("This method can only be executed in guilds"); var channelId = await GetChannelId(guildId); if (channelId == 0) { context.Response.AddError(_localizer["Moderation.Warnings.NoConsole"]); return; } builder = context.Response.AddEmbed(channelId: channelId); updateEntity = true; } builder .SetEmbedAuthor(title, user.GetAvatarUrl(size: ImageSize.x32)) .SetEmbedColor(255, 235, 59) .SetEmbedFooter(footer, moderator.GetAvatarUrl(size: ImageSize.x32)) .AddEmbedField(_localizer["Moderation.Warn.User"], $"{user.Username}#{user.Discriminator} ({user.Mention})", true) .AddEmbedField(_localizer["Moderation.Warn.Date"], warning.Created, true) .AddEmbedField(_localizer["Moderation.Warn.UserId"], user.Id, true) .AddEmbedField(_localizer["Moderation.Warn.Reason"], displayReason) .Catch(args => { context.Response.AddError(_localizer["Moderation.Warn.ConsoleNotAvailable"]); }); if (updateEntity) { builder.Then(async args => { var warningRepo = context.RequestServices.GetRequiredService <IUserWarningRepository>(); warning.ConsoleMessageId = args.Message.Id; warning.ConsoleChannelId = args.Message.ChannelId; await warningRepo.UpdateAsync(warning); }); } }