-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBirthdayCheckerService.cs
131 lines (103 loc) · 4.58 KB
/
BirthdayCheckerService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using DSharpPlus;
using DSharpPlus.Entities;
namespace BirthdayBot
{
public class BirthdayCheckerService
{
private readonly DiscordClient _client;
private readonly CancellationTokenSource _cts;
public BirthdayCheckerService(DiscordClient client)
{
_client = client;
_cts = new CancellationTokenSource();
}
public void Start()
{
Task.Run(async () =>
{
while (!_cts.IsCancellationRequested)
{
DateTime now = DateTime.UtcNow;
DateTime nextCheck = now.Date.AddDays(1);
await Task.Delay(nextCheck - now, _cts.Token);
await CheckBirthdaysAsync();
}
});
}
public void Stop()
{
_cts.Cancel();
}
public async Task CheckBirthdaysAsync()
{
List<(ulong guildId, ulong userId, DateTime birthday)> birthdays = await Database.GetBirthdays(0);
DateTime today = DateTime.UtcNow.Date;
foreach (var (guildId, userId, birthday) in birthdays)
{
DiscordGuild guild = await _client.GetGuildAsync(guildId);
DiscordMember member = await guild.GetMemberAsync(userId);
ulong? roleId = await Database.GetRoleId(guildId);
if (birthday.Month == today.Month && birthday.Day == today.Day)
{
int? age = null;
if (birthday.Year != 0001)
{
DateTime nextBirthday = new(today.Year, birthday.Month, birthday.Day);
age = today.Year - birthday.Year;
if (today < nextBirthday)
age--;
}
await HBDBuilder(guildId, member, age);
if (roleId.HasValue)
{
ulong rId = roleId.Value;
DiscordRole bdayRole = guild.GetRole(rId);
await member.GrantRoleAsync(bdayRole, "It's their birthday today!");
}
await Functions.UpdateListEmbeds(guild, _client);
}
else{
if (roleId.HasValue)
{
IEnumerable<DiscordRole> roles = member.Roles;
ulong rId = roleId.Value;
DiscordRole bdayRole = guild.GetRole(rId);
if (roles.Contains(bdayRole))
{
await member.RevokeRoleAsync(bdayRole, "It is no longer their birthday.");
}
}
}
}
}
public async Task HBDBuilder(ulong guildId, DiscordMember member, int? age)
{
ulong? channelId = await Database.GetChannelId(guildId);
//For testing purposes only:
//ulong? channelId = await Database.GetChannelId(702106468849156127);
if (!channelId.HasValue)
return;
DiscordChannel channel = await _client.GetChannelAsync(channelId.Value);
if (channel == null)
return;
string ageExtra = age.HasValue && age.Value > 1 ? $"*They are {age.Value} years old today!*\r\n" : "\r\n";
List<string> videos = new()
{
"https://cdn.discordapp.com/attachments/1106239191559454893/1106239377547473057/Today_is_Birthday_in_VRChat.mp4",
"https://cdn.discordapp.com/attachments/1106239191559454893/1106239378046591068/Today_is_my_birthday.mp4"
};
Random random = new();
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 ||";
DiscordMessageBuilder messageBuilder = new DiscordMessageBuilder()
.WithContent(contentMsg)
.AddFile("video.mp4", await Functions.DownloadFile(attachmentUrl))
.WithAllowedMentions(new IMention[] { new UserMention(member), new EveryoneMention() });
await channel.SendMessageAsync(messageBuilder);
}
}
}