Skip to content

Commit

Permalink
use primary constructor where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
loukylor committed Dec 21, 2024
1 parent 44b2537 commit 70d562d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 38 deletions.
48 changes: 20 additions & 28 deletions TrickFireDiscordBot/Services/RoleSyncer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ namespace TrickFireDiscordBot.Services;
public class RoleSyncer(
ILogger<RoleSyncer> logger,
INotionClient notionClient,
DiscordClient discordClient,
WebhookListener listener,
DiscordService discordService,
IOptions<RoleSyncerOptions> options)
Expand All @@ -28,22 +27,15 @@ public class RoleSyncer(

private static readonly Channel<Page> _pageQueue = Channel.CreateUnbounded<Page>();

public ILogger Logger { get; } = logger;
public INotionClient NotionClient { get; } = notionClient;
public DiscordClient DiscordClient { get; } = discordClient;
public WebhookListener WebhookListener { get; } = listener;

private readonly DiscordGuild _mainGuild = discordService.MainGuild;

private readonly Dictionary<string, DiscordRole> _discordRoleCache = [];
private string? _teamPageNamePropertyId = null;


public override Task StartAsync(CancellationToken cancellationToken)
{
WebhookListener.OnWebhookReceived += OnWebhook;
listener.OnWebhookReceived += OnWebhook;

foreach (DiscordRole role in _mainGuild.Roles.Values)
foreach (DiscordRole role in discordService.MainGuild.Roles.Values)
{
_discordRoleCache.Add(role.Name, role);
}
Expand All @@ -55,13 +47,13 @@ public override async Task StopAsync(CancellationToken cancellationToken)
{
await base.StopAsync(cancellationToken);

WebhookListener.OnWebhookReceived -= OnWebhook;
listener.OnWebhookReceived -= OnWebhook;
}

public async Task SyncAllMemberRoles(bool dryRun = true)
{
Task<PaginatedList<Page>> nextPageQuery(string? cursor) =>
NotionClient.Databases.QueryAsync(
notionClient.Databases.QueryAsync(
options.Value.MembersDatabaseId,
new DatabasesQueryParameters()
{
Expand All @@ -75,12 +67,12 @@ Task<PaginatedList<Page>> nextPageQuery(string? cursor) =>
await Task.Delay(333);
DiscordMember? member = await SyncRoles(page, dryRun);
processedMembers.Add(member);
Logger.LogInformation("\n");
logger.LogInformation("\n");
}

Logger.LogInformation("\nNo notion page users:");
logger.LogInformation("\nNo notion page users:");
DiscordRole inactiveRole = _discordRoleCache.Values.First(role => role.Id == options.Value.InactiveRoleId);
await foreach (DiscordMember member in _mainGuild.GetAllMembersAsync())
await foreach (DiscordMember member in discordService.MainGuild.GetAllMembersAsync())
{
if (processedMembers.Contains(member))
{
Expand All @@ -91,7 +83,7 @@ Task<PaginatedList<Page>> nextPageQuery(string? cursor) =>
await member.GrantRoleAsync(inactiveRole);
await Task.Delay(333);
}
Logger.LogInformation("{}, ({})", member.DisplayName, member.Username);
logger.LogInformation("{}, ({})", member.DisplayName, member.Username);
}
}

Expand All @@ -107,7 +99,7 @@ private void OnWebhook(HttpListenerRequest request)
Automation? automation = JsonConvert.DeserializeObject<Automation>(json);
if (automation == null || automation.Data is not Page page)
{
Logger.LogWarning("Could not parse automation: {}", json);
logger.LogWarning("Could not parse automation: {}", json);
return;
}

Expand All @@ -124,7 +116,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
}
catch (Exception ex)
{
Logger.LogError(ex, "Error in RoleSyncer main loop: ");
logger.LogError(ex, "Error in RoleSyncer main loop: ");
}
}
}
Expand All @@ -138,15 +130,15 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
}

IEnumerable<DiscordRole> newRoles = await GetRoles(notionPage);
Logger.LogInformation(member.DisplayName);
logger.LogInformation(member.DisplayName);
foreach (DiscordRole role in newRoles)
{
Logger.LogInformation(role!.Name);
logger.LogInformation(role!.Name);
}

if (!dryRun)
{
int highestRole = _mainGuild.CurrentMember.Roles.Max(role => role.Position);
int highestRole = discordService.MainGuild.CurrentMember.Roles.Max(role => role.Position);
await member.ModifyAsync(model =>
{
List<DiscordRole> rolesWithLeadership = new(newRoles);
Expand All @@ -172,21 +164,21 @@ await member.ModifyAsync(model =>

if (string.IsNullOrWhiteSpace(username))
{
Logger.LogWarning("User with page url {} has no discord.", notionPage.Url);
logger.LogWarning("User with page url {} has no discord.", notionPage.Url);
return null;
}

username = username.TrimStart('@').ToLower();

DiscordMember? member = _mainGuild.Members.Values
DiscordMember? member = discordService.MainGuild.Members.Values
.FirstOrDefault(member => member.Username == username);

if (member is null)
{
IReadOnlyList<DiscordMember> search = await _mainGuild.SearchMembersAsync(username);
IReadOnlyList<DiscordMember> search = await discordService.MainGuild.SearchMembersAsync(username);
if (search.Count == 0 || search[0].Username != username)
{
Logger.LogWarning("Could not find member: {}", username);
logger.LogWarning("Could not find member: {}", username);
}
else
{
Expand Down Expand Up @@ -276,7 +268,7 @@ private async Task<string> GetTeamName(ObjectId id)
{
try
{
ListPropertyItem item = (await NotionClient.Pages.RetrievePagePropertyItemAsync(new RetrievePropertyItemParameters()
ListPropertyItem item = (await notionClient.Pages.RetrievePagePropertyItemAsync(new RetrievePropertyItemParameters()
{
PageId = id.Id,
PropertyId = _teamPageNamePropertyId
Expand All @@ -296,7 +288,7 @@ private async Task<string> GetTeamName(ObjectId id)
}

// If it doesn't or fails, then recache it and get the whole page
Page teamPage = await NotionClient.Pages.RetrieveAsync(id.Id);
Page teamPage = await notionClient.Pages.RetrieveAsync(id.Id);
TitlePropertyValue nameValue = (teamPage.Properties[options.Value.TeamNamePropertyName]
as TitlePropertyValue)!;
_teamPageNamePropertyId = nameValue.Id;
Expand All @@ -307,7 +299,7 @@ private async Task<string> GetTeamName(ObjectId id)
{
if (!_discordRoleCache.TryGetValue(roleName, out DiscordRole? role))
{
Logger.LogWarning("Could not find role with name: {}", roleName);
logger.LogWarning("Could not find role with name: {}", roleName);
}

return role;
Expand Down
14 changes: 4 additions & 10 deletions TrickFireDiscordBot/Services/WebhookListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,19 @@

namespace TrickFireDiscordBot.Services;

public class WebhookListener : BackgroundService, IAutoRegisteredService
public class WebhookListener(ILogger<WebhookListener> logger, IOptions<WebhookListenerOptions> options)
: BackgroundService, IAutoRegisteredService
{
public event Action<HttpListenerRequest>? OnWebhookReceived;

public ILogger Logger { get; }

private readonly HttpListener _listener = new();

public WebhookListener(ILogger<WebhookListener> logger, IOptions<WebhookListenerOptions> options)
public override Task StartAsync(CancellationToken cancellationToken)
{
Logger = logger;
foreach (string prefix in options.Value.Prefixes)
{
_listener.Prefixes.Add(prefix);
}
}

public override Task StartAsync(CancellationToken cancellationToken)
{
_listener.Start();

return base.StartAsync(cancellationToken);
Expand Down Expand Up @@ -57,7 +51,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
}
catch (Exception ex)
{
Logger.LogError(ex, "Exception in WebhookListener main loop: ");
logger.LogError(ex, "Exception in WebhookListener main loop: ");
}
}
}
Expand Down

0 comments on commit 70d562d

Please sign in to comment.