1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-04-20 00:43:36 +03:00

Bring back listening statuses

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2023-06-30 14:51:02 +05:00
parent b46c366e5e
commit b1855c5257
Signed by: Octol1ttle
GPG key ID: B77C34313AEE1FFF
3 changed files with 40 additions and 15 deletions

View file

@ -8,7 +8,6 @@ using Microsoft.Extensions.Logging;
using Remora.Commands.Extensions; using Remora.Commands.Extensions;
using Remora.Discord.API.Abstractions.Gateway.Commands; using Remora.Discord.API.Abstractions.Gateway.Commands;
using Remora.Discord.API.Abstractions.Objects; using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.API.Gateway.Commands;
using Remora.Discord.API.Objects; using Remora.Discord.API.Objects;
using Remora.Discord.Caching.Extensions; using Remora.Discord.Caching.Extensions;
using Remora.Discord.Caching.Services; using Remora.Discord.Caching.Services;
@ -54,14 +53,9 @@ public class Boyfriend {
).ConfigureServices( ).ConfigureServices(
(_, services) => { (_, services) => {
services.Configure<DiscordGatewayClientOptions>( services.Configure<DiscordGatewayClientOptions>(
options => { options => options.Intents |= GatewayIntents.MessageContents
options.Intents |= GatewayIntents.MessageContents | GatewayIntents.GuildMembers
| GatewayIntents.GuildMembers | GatewayIntents.GuildScheduledEvents);
| GatewayIntents.GuildScheduledEvents;
options.Presence = new UpdatePresence(
UserStatus.Online, false, DateTimeOffset.UtcNow,
new[] { new Activity("with Remora.Discord", ActivityType.Game) });
});
services.Configure<CacheSettings>( services.Configure<CacheSettings>(
settings => { settings => {
settings.SetDefaultAbsoluteExpiration(TimeSpan.FromHours(1)); settings.SetDefaultAbsoluteExpiration(TimeSpan.FromHours(1));

View file

@ -103,7 +103,7 @@ public class GuildDataService : IHostedService {
return (await GetData(guildId, ct)).GetMemberData(userId); return (await GetData(guildId, ct)).GetMemberData(userId);
} }
public IEnumerable<Snowflake> GetGuildIds() { public ICollection<Snowflake> GetGuildIds() {
return _datas.Keys; return _datas.Keys;
} }
} }

View file

@ -1,12 +1,15 @@
using Boyfriend.Data; using Boyfriend.Data;
using Boyfriend.Services.Data; using Boyfriend.Services.Data;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Remora.Discord.API.Abstractions.Objects; using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.API.Abstractions.Rest; using Remora.Discord.API.Abstractions.Rest;
using Remora.Discord.API.Gateway.Commands;
using Remora.Discord.API.Objects; using Remora.Discord.API.Objects;
using Remora.Discord.Extensions.Embeds; using Remora.Discord.Extensions.Embeds;
using Remora.Discord.Extensions.Formatting; using Remora.Discord.Extensions.Formatting;
using Remora.Discord.Gateway;
using Remora.Discord.Gateway.Responders; using Remora.Discord.Gateway.Responders;
using Remora.Discord.Interactivity; using Remora.Discord.Interactivity;
using Remora.Rest.Core; using Remora.Rest.Core;
@ -18,23 +21,38 @@ namespace Boyfriend.Services;
/// Handles executing guild updates (also called "ticks") once per second. /// Handles executing guild updates (also called "ticks") once per second.
/// </summary> /// </summary>
public class GuildUpdateService : BackgroundService { 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<Activity> _activityList = new(1) { new Activity("with Remora.Discord", ActivityType.Game) };
private readonly IDiscordRestChannelAPI _channelApi; private readonly IDiscordRestChannelAPI _channelApi;
private readonly GuildDataService _dataService; private readonly GuildDataService _dataService;
private readonly IDiscordRestGuildScheduledEventAPI _eventApi; private readonly IDiscordRestGuildScheduledEventAPI _eventApi;
private readonly IDiscordRestGuildAPI _guildApi; private readonly IDiscordRestGuildAPI _guildApi;
private readonly ILogger<GuildUpdateService> _logger; private readonly ILogger<GuildUpdateService> _logger;
private readonly IServiceProvider _provider;
private readonly IDiscordRestUserAPI _userApi; private readonly IDiscordRestUserAPI _userApi;
private readonly UtilityService _utility; private readonly UtilityService _utility;
private DateTimeOffset _nextSongAt = DateTimeOffset.MinValue;
private uint _nextSongIndex;
public GuildUpdateService( public GuildUpdateService(
IDiscordRestChannelAPI channelApi, GuildDataService dataService, IDiscordRestGuildAPI guildApi, IDiscordRestChannelAPI channelApi, GuildDataService dataService, IDiscordRestGuildScheduledEventAPI eventApi,
IDiscordRestGuildScheduledEventAPI eventApi, ILogger<GuildUpdateService> logger, IDiscordRestUserAPI userApi, IDiscordRestGuildAPI guildApi, ILogger<GuildUpdateService> logger, IServiceProvider provider,
UtilityService utility) { IDiscordRestUserAPI userApi, UtilityService utility) {
_channelApi = channelApi; _channelApi = channelApi;
_dataService = dataService; _dataService = dataService;
_guildApi = guildApi;
_eventApi = eventApi; _eventApi = eventApi;
_guildApi = guildApi;
_logger = logger; _logger = logger;
_provider = provider;
_userApi = userApi; _userApi = userApi;
_utility = utility; _utility = utility;
} }
@ -49,7 +67,20 @@ public class GuildUpdateService : BackgroundService {
var tasks = new List<Task>(); var tasks = new List<Task>();
while (await timer.WaitForNextTickAsync(ct)) { 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<DiscordGatewayClient>();
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); await Task.WhenAll(tasks);
tasks.Clear(); tasks.Clear();