From ad007bafc56462119656735157571fa37c8f3f04 Mon Sep 17 00:00:00 2001 From: Realmlist Date: Mon, 17 Jul 2023 19:19:31 +0200 Subject: [PATCH] Redone the bday announcement msg --- BirthdayCheckerService.cs | 34 ++++++++++++++++++++-------------- Functions.cs | 16 ++++++++++++++-- Maintenance.cs | 1 + 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/BirthdayCheckerService.cs b/BirthdayCheckerService.cs index 6adac6d..7e2c5a0 100644 --- a/BirthdayCheckerService.cs +++ b/BirthdayCheckerService.cs @@ -35,6 +35,7 @@ public void Start() await Task.Delay(nextCheck - now, _cts.Token); await CheckBirthdaysAsync(); + } }); } @@ -55,12 +56,9 @@ public async Task CheckBirthdaysAsync() DiscordMember member = await guild.GetMemberAsync(userId); var roleId = await Database.GetRoleId(guildId); - - if (birthday.Month == today.Month && birthday.Day == today.Day) { - var user = await _client.GetUserAsync(userId); int? age = null; if (birthday.Year > 1) @@ -80,7 +78,10 @@ public async Task CheckBirthdaysAsync() await member.GrantRoleAsync(bdayRole, "It's their birthday today!"); } - }else{ + await Functions.UpdateListEmbeds(_client, guild); + + } + else{ if (roleId.HasValue) { @@ -102,20 +103,16 @@ public async Task CheckBirthdaysAsync() public async Task HBDBuilder(ulong guildId, DiscordMember member, int? age) { var channelId = await Database.GetChannelId(guildId); + //For testing purposes only: + //var channelId = await Database.GetChannelId(702106468849156127); if (!channelId.HasValue) return; var channel = await _client.GetChannelAsync(channelId.Value); if (channel == null) return; - var ageExtra = age.HasValue && age.Value > 1 ? $"They are {age.Value} years old today!" : ""; - DiscordEmbedBuilder embed = new DiscordEmbedBuilder - { - Color = DiscordColor.Gold, - Title = $"Birthday Announcement!", - Description = $"**Please wish {member.Mention} a happy birthday! 🎂**\r\n{ageExtra}", - }; + var ageExtra = age.HasValue && age.Value > 1 ? $"*They are {age.Value} years old today!*\r\n" : "\r\n"; var videos = new List { @@ -124,11 +121,20 @@ public async Task HBDBuilder(ulong guildId, DiscordMember member, int? age) }; var random = new Random(); int randomVid = random.Next(videos.Count); + string attachmentUrl = videos[randomVid]; + + string contentMsg = $"# Birthday Announcement!\r\n" + + $"**Please wish {member.Mention} a happy birthday! 🎂**\r\n" + + $"{ageExtra}" + + $"\r\n|| @everyone ||"; - await channel.SendMessageAsync(embed: embed); - await channel.SendMessageAsync(videos[randomVid]); + var messageBuilder = new DiscordMessageBuilder() + .WithContent(contentMsg) + .AddFile("video.mp4", await Functions.DownloadFile(attachmentUrl)) + .WithAllowedMentions(new IMention[] { new UserMention(member), new EveryoneMention() }); - + await channel.SendMessageAsync(messageBuilder); + } } } diff --git a/Functions.cs b/Functions.cs index b820410..f66dc59 100644 --- a/Functions.cs +++ b/Functions.cs @@ -6,6 +6,8 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; +using System.Net; +using System.Net.Http; using System.Text; using System.Threading.Tasks; @@ -46,7 +48,8 @@ public static DiscordEmbed BuildListEmbed(List<(ulong guildId, ulong userId, Dat var embedBuilder = new DiscordEmbedBuilder() .WithTitle($"Birthday List - {guild.Name}") .WithColor(DiscordColor.Gold) - .WithDescription("Register your birthday with\r\n `/birthday add [year]`"); + .WithDescription("Register your birthday with\r\n" + + "`/birthday add [year]`"); var groupedBirthdays = birthdays .OrderBy(b => b.birthday.Month) @@ -74,7 +77,7 @@ public static DiscordEmbed BuildListEmbed(List<(ulong guildId, ulong userId, Dat monthFieldContent.AppendLine($"{user.Mention} - {birthday.birthday:dd MMMM}{(age.HasValue && age.Value > 1 ? $" ({age.Value} years old)" : "")}"); } - + embedBuilder.WithThumbnail(guild.GetIconUrl(ImageFormat.Auto, 1024)); embedBuilder.AddField(monthName, monthFieldContent.ToString(), false); } @@ -103,5 +106,14 @@ public static async Task CheckConfig(InteractionContext ctx) } } + + public static async Task DownloadFile(string url) + { + var _httpClient = new HttpClient(); + + var response = await _httpClient.GetAsync(url); + response.EnsureSuccessStatusCode(); + return await response.Content.ReadAsStreamAsync(); + } } } diff --git a/Maintenance.cs b/Maintenance.cs index 1f38df3..b63fc3f 100644 --- a/Maintenance.cs +++ b/Maintenance.cs @@ -27,6 +27,7 @@ private static async Task OnMemberLeave(DiscordClient client, GuildMemberRemoveE ulong leftMemberId = e.Member.Id; ulong guildId = e.Guild.Id; await Database.RemoveBirthday(guildId, leftMemberId); + await Functions.UpdateListEmbeds(client, e.Guild); } } } \ No newline at end of file