Skip to content

Commit

Permalink
Redone the bday announcement msg
Browse files Browse the repository at this point in the history
  • Loading branch information
Realmlist committed Jul 17, 2023
1 parent 6f55de5 commit ad007ba
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
34 changes: 20 additions & 14 deletions BirthdayCheckerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public void Start()
await Task.Delay(nextCheck - now, _cts.Token);
await CheckBirthdaysAsync();
}
});
}
Expand All @@ -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)
Expand All @@ -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)
{
Expand All @@ -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<string>
{
Expand All @@ -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);

}
}
}
16 changes: 14 additions & 2 deletions Functions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 <day> <month> [year]`");
.WithDescription("Register your birthday with\r\n" +
"`/birthday add <day> <month> [year]`");

var groupedBirthdays = birthdays
.OrderBy(b => b.birthday.Month)
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -103,5 +106,14 @@ public static async Task CheckConfig(InteractionContext ctx)
}

}

public static async Task<Stream> DownloadFile(string url)
{
var _httpClient = new HttpClient();

var response = await _httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStreamAsync();
}
}
}
1 change: 1 addition & 0 deletions Maintenance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit ad007ba

Please sign in to comment.