Skip to content

Commit

Permalink
enable role syncer and begin dry runs
Browse files Browse the repository at this point in the history
  • Loading branch information
loukylor committed Dec 17, 2024
1 parent 9d59a8a commit 03c05ce
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
12 changes: 6 additions & 6 deletions TrickFireDiscordBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ static async Task Main(string[] args)
await bot.Start();

// Start notion client
//NotionClient notionClient = NotionClientFactory.Create(new ClientOptions()
//{
// AuthToken = lines[1]
//});
NotionClient notionClient = NotionClientFactory.Create(new ClientOptions()
{
AuthToken = lines[1]
});

// Start the webhook listener

Expand All @@ -35,8 +35,8 @@ static async Task Main(string[] args)
webhookListener.Start();

// Start role syncer
//RoleSyncer syncer = new(bot.Client.Logger, null, bot, webhookListener);
//await syncer.Start();
RoleSyncer syncer = new(bot.Client.Logger, notionClient, bot, webhookListener);
await syncer.Start();

// Hang the process forever so it doesn't quit after the bot
// connects
Expand Down
44 changes: 28 additions & 16 deletions TrickFireDiscordBot/RoleSyncer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void Stop()
WebhookListener.OnWebhookReceived -= OnWebhook;
}

private async void OnWebhook(HttpListenerRequest request)
private void OnWebhook(HttpListenerRequest request)
{
if (request.RawUrl == null || !request.RawUrl.StartsWith(WebhookEndpoint))
{
Expand All @@ -56,14 +56,12 @@ private async void OnWebhook(HttpListenerRequest request)
return;
}

await SyncRoles(page);

Console.WriteLine(JsonConvert.SerializeObject(page, Formatting.Indented));
SyncRoles(page).GetAwaiter().GetResult();
}

private async Task SyncRoles(Page notionPage)
{
DiscordMember? member = GetMember(notionPage);
DiscordMember? member = await GetMember(notionPage);
if (member is null)
{
return;
Expand All @@ -76,7 +74,7 @@ private async Task SyncRoles(Page notionPage)
}
}

private DiscordMember? GetMember(Page notionPage)
private async Task<DiscordMember?> GetMember(Page notionPage)
{
if (_trickFireGuild is null)
{
Expand All @@ -90,7 +88,15 @@ private async Task SyncRoles(Page notionPage)

if (member is null)
{
Logger.LogWarning("Could not find member: {}", username);
IReadOnlyList<DiscordMember> search = await _trickFireGuild.SearchMembersAsync(username);
if (search.Count == 0 || search[0].Username != username)
{
Logger.LogWarning("Could not find member: {}", username);
}
else
{
member = search[0];
}
}
return member;
}
Expand All @@ -110,7 +116,13 @@ private async Task<IEnumerable<DiscordRole>> GetRoles(Page notionPage)
MultiSelectPropertyValue positions = (notionPage.Properties["Club Position(s)"] as MultiSelectPropertyValue)!;
foreach (SelectOption item in positions.MultiSelect)
{
DiscordRole? role = GetRoleOrDefault(item.Name);
if (item.Name == "Individual Contributor")
{
continue;
}

// Remove team suffix to make roles a little easier to read
DiscordRole? role = GetRoleOrDefault(item.Name.Replace(" Team", ""));
if (role is not null)
{
roles.Add(role);
Expand All @@ -123,7 +135,7 @@ private async Task<IEnumerable<DiscordRole>> GetRoles(Page notionPage)
private DiscordRole? GetActiveRole(Page notionPage)
{
string activeValue = (notionPage.Properties["Active?"] as SelectPropertyValue)!.Select.Name;
if (activeValue == "Inactive")
if (activeValue == "Active")
{
return null;
}
Expand All @@ -140,8 +152,9 @@ private async Task<IEnumerable<DiscordRole>> GetTeams(Page notionPage)
foreach (ObjectId id in teamIds)
{
string teamName = await GetTeamName(id);

DiscordRole? role = GetRoleOrDefault(teamName);

// Remove team suffix to make roles a little easier to read
DiscordRole? role = GetRoleOrDefault(teamName.Replace(" Team", ""));
if (role is not null)
{
roles.Add(role);
Expand All @@ -154,17 +167,16 @@ private async Task<IEnumerable<DiscordRole>> GetTeams(Page notionPage)
private async Task<string> GetTeamName(ObjectId id)
{
// Try using our cached property id if it exists
TitlePropertyValue nameValue;
if (_teamPageNamePropertyId is not null)
{
try
{
nameValue = (await NotionClient.Pages.RetrievePagePropertyItemAsync(new RetrievePropertyItemParameters()
ListPropertyItem item = (await NotionClient.Pages.RetrievePagePropertyItemAsync(new RetrievePropertyItemParameters()
{
PageId = id.Id,
PropertyId = _teamPageNamePropertyId
}) as TitlePropertyValue)!;
return nameValue.Title[0].PlainText;
}) as ListPropertyItem)!;
return (item.Results.First() as TitlePropertyItem)!.Title.PlainText;
}
catch (NotionApiException ex)
{
Expand All @@ -180,7 +192,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);
nameValue = (teamPage.Properties["Name"] as TitlePropertyValue)!;
TitlePropertyValue nameValue = (teamPage.Properties["Team name"] as TitlePropertyValue)!;
_teamPageNamePropertyId = nameValue.Id;
return nameValue.Title[0].PlainText;
}
Expand Down

0 comments on commit 03c05ce

Please sign in to comment.