From b1855c525794760c071f024f5f87e6a2cae8b677 Mon Sep 17 00:00:00 2001 From: Octol1ttle Date: Fri, 30 Jun 2023 14:51:02 +0500 Subject: [PATCH] Bring back listening statuses Signed-off-by: Octol1ttle --- Boyfriend.cs | 12 +++------ Services/Data/GuildDataService.cs | 2 +- Services/GuildUpdateService.cs | 41 +++++++++++++++++++++++++++---- 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/Boyfriend.cs b/Boyfriend.cs index 21e1b02..1a3e2a6 100644 --- a/Boyfriend.cs +++ b/Boyfriend.cs @@ -8,7 +8,6 @@ using Microsoft.Extensions.Logging; using Remora.Commands.Extensions; using Remora.Discord.API.Abstractions.Gateway.Commands; using Remora.Discord.API.Abstractions.Objects; -using Remora.Discord.API.Gateway.Commands; using Remora.Discord.API.Objects; using Remora.Discord.Caching.Extensions; using Remora.Discord.Caching.Services; @@ -54,14 +53,9 @@ public class Boyfriend { ).ConfigureServices( (_, services) => { services.Configure( - options => { - options.Intents |= GatewayIntents.MessageContents - | GatewayIntents.GuildMembers - | GatewayIntents.GuildScheduledEvents; - options.Presence = new UpdatePresence( - UserStatus.Online, false, DateTimeOffset.UtcNow, - new[] { new Activity("with Remora.Discord", ActivityType.Game) }); - }); + options => options.Intents |= GatewayIntents.MessageContents + | GatewayIntents.GuildMembers + | GatewayIntents.GuildScheduledEvents); services.Configure( settings => { settings.SetDefaultAbsoluteExpiration(TimeSpan.FromHours(1)); diff --git a/Services/Data/GuildDataService.cs b/Services/Data/GuildDataService.cs index b3160a1..bd8ff7e 100644 --- a/Services/Data/GuildDataService.cs +++ b/Services/Data/GuildDataService.cs @@ -103,7 +103,7 @@ public class GuildDataService : IHostedService { return (await GetData(guildId, ct)).GetMemberData(userId); } - public IEnumerable GetGuildIds() { + public ICollection GetGuildIds() { return _datas.Keys; } } diff --git a/Services/GuildUpdateService.cs b/Services/GuildUpdateService.cs index ea880f5..4db9e1f 100644 --- a/Services/GuildUpdateService.cs +++ b/Services/GuildUpdateService.cs @@ -1,12 +1,15 @@ using Boyfriend.Data; using Boyfriend.Services.Data; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Remora.Discord.API.Abstractions.Objects; using Remora.Discord.API.Abstractions.Rest; +using Remora.Discord.API.Gateway.Commands; using Remora.Discord.API.Objects; using Remora.Discord.Extensions.Embeds; using Remora.Discord.Extensions.Formatting; +using Remora.Discord.Gateway; using Remora.Discord.Gateway.Responders; using Remora.Discord.Interactivity; using Remora.Rest.Core; @@ -18,23 +21,38 @@ namespace Boyfriend.Services; /// Handles executing guild updates (also called "ticks") once per second. /// public class GuildUpdateService : BackgroundService { + private static readonly (string Name, TimeSpan Duration)[] SongList = { + ("UNDEAD CORPORATION - The Empress", new TimeSpan(0, 4, 34)), + ("UNDEAD CORPORATION - Everything will freeze", new TimeSpan(0, 3, 17)), + ("Splatoon 3 - Rockagilly Blues (Yoko & the Gold Bazookas)", new TimeSpan(0, 3, 37)), + ("Splatoon 3 - Seep and Destroy", new TimeSpan(0, 2, 42)), + ("IA - A Tale of Six Trillion Years and a Night", new TimeSpan(0, 3, 40)), + ("Manuel - Gas Gas Gas", new TimeSpan(0, 3, 17)) + }; + + private readonly List _activityList = new(1) { new Activity("with Remora.Discord", ActivityType.Game) }; + private readonly IDiscordRestChannelAPI _channelApi; private readonly GuildDataService _dataService; private readonly IDiscordRestGuildScheduledEventAPI _eventApi; private readonly IDiscordRestGuildAPI _guildApi; private readonly ILogger _logger; + private readonly IServiceProvider _provider; private readonly IDiscordRestUserAPI _userApi; private readonly UtilityService _utility; + private DateTimeOffset _nextSongAt = DateTimeOffset.MinValue; + private uint _nextSongIndex; public GuildUpdateService( - IDiscordRestChannelAPI channelApi, GuildDataService dataService, IDiscordRestGuildAPI guildApi, - IDiscordRestGuildScheduledEventAPI eventApi, ILogger logger, IDiscordRestUserAPI userApi, - UtilityService utility) { + IDiscordRestChannelAPI channelApi, GuildDataService dataService, IDiscordRestGuildScheduledEventAPI eventApi, + IDiscordRestGuildAPI guildApi, ILogger logger, IServiceProvider provider, + IDiscordRestUserAPI userApi, UtilityService utility) { _channelApi = channelApi; _dataService = dataService; - _guildApi = guildApi; _eventApi = eventApi; + _guildApi = guildApi; _logger = logger; + _provider = provider; _userApi = userApi; _utility = utility; } @@ -49,7 +67,20 @@ public class GuildUpdateService : BackgroundService { var tasks = new List(); while (await timer.WaitForNextTickAsync(ct)) { - tasks.AddRange(_dataService.GetGuildIds().Select(id => TickGuildAsync(id, ct))); + var guildIds = _dataService.GetGuildIds(); + if (guildIds.Count > 0 && DateTimeOffset.UtcNow >= _nextSongAt) { + var nextSong = SongList[_nextSongIndex]; + _activityList[0] = new Activity(nextSong.Name, ActivityType.Listening); + var client = _provider.GetRequiredService(); + client.SubmitCommand( + new UpdatePresence( + UserStatus.Online, false, DateTimeOffset.UtcNow, _activityList)); + _nextSongAt = DateTimeOffset.UtcNow.Add(nextSong.Duration); + _nextSongIndex++; + if (_nextSongIndex >= SongList.Length) _nextSongIndex = 0; + } + + tasks.AddRange(guildIds.Select(id => TickGuildAsync(id, ct))); await Task.WhenAll(tasks); tasks.Clear();