-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIncomingUsersListener.cs
51 lines (42 loc) · 1.43 KB
/
IncomingUsersListener.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
using DSharpPlus;
using DSharpPlus.EventArgs;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using YumeChan.RoleDeck.Services;
namespace YumeChan.RoleDeck;
public sealed class IncomingUsersListener : IHostedService
{
private readonly ILogger<IncomingUsersListener> _logger;
private readonly InitialRolesService _service;
private readonly DiscordClient _client;
public IncomingUsersListener(ILogger<IncomingUsersListener> logger, InitialRolesService service, DiscordClient client)
{
_logger = logger;
_service = service;
_client = client;
}
public Task StartAsync(CancellationToken cancellationToken)
{
_client.GuildMemberAdded += OnGuildMemberAddedAsync;
_logger.LogInformation("Started IncomingUsersListener");
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
_client.GuildMemberAdded -= OnGuildMemberAddedAsync;
_logger.LogInformation("Started IncomingUsersListener");
return Task.CompletedTask;
}
private async Task OnGuildMemberAddedAsync(DiscordClient sender, GuildMemberAddEventArgs e)
{
ImmutableArray<ulong>? roles = (await _service.GetGuildRolesAsync(e.Guild.Id))?.ToImmutableArray();
if (roles is { Length: > 0 })
{
await Task.WhenAll(roles!.Value.Select(roleId => e.Member.GrantRoleAsync(e.Guild.GetRole(roleId))));
}
}
}