mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-04-20 00:43:36 +03:00
Merge branch 'master' into move-event-update
This commit is contained in:
commit
06ebc2b342
3 changed files with 6 additions and 51 deletions
|
@ -1,33 +0,0 @@
|
||||||
using JetBrains.Annotations;
|
|
||||||
using Octobot.Data;
|
|
||||||
using Octobot.Services;
|
|
||||||
using Remora.Discord.API.Abstractions.Gateway.Events;
|
|
||||||
using Remora.Discord.Gateway.Responders;
|
|
||||||
using Remora.Results;
|
|
||||||
|
|
||||||
namespace Octobot.Responders;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Handles updating <see cref="MemberData.Roles" /> when a guild member is updated.
|
|
||||||
/// </summary>
|
|
||||||
[UsedImplicitly]
|
|
||||||
public class GuildMemberUpdateResponder : IResponder<IGuildMemberUpdate>
|
|
||||||
{
|
|
||||||
private readonly GuildDataService _guildData;
|
|
||||||
|
|
||||||
public GuildMemberUpdateResponder(GuildDataService guildData)
|
|
||||||
{
|
|
||||||
_guildData = guildData;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<Result> RespondAsync(IGuildMemberUpdate gatewayEvent, CancellationToken ct = default)
|
|
||||||
{
|
|
||||||
var memberData = await _guildData.GetMemberData(gatewayEvent.GuildID, gatewayEvent.User.ID, ct);
|
|
||||||
if (memberData.MutedUntil is null)
|
|
||||||
{
|
|
||||||
memberData.Roles = gatewayEvent.Roles.ToList().ConvertAll(r => r.Value);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Result.FromSuccess();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -4,7 +4,6 @@ using System.Text.Json.Nodes;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Octobot.Data;
|
using Octobot.Data;
|
||||||
using Remora.Discord.API.Abstractions.Rest;
|
|
||||||
using Remora.Rest.Core;
|
using Remora.Rest.Core;
|
||||||
|
|
||||||
namespace Octobot.Services;
|
namespace Octobot.Services;
|
||||||
|
@ -15,14 +14,12 @@ namespace Octobot.Services;
|
||||||
public sealed class GuildDataService : IHostedService
|
public sealed class GuildDataService : IHostedService
|
||||||
{
|
{
|
||||||
private readonly ConcurrentDictionary<Snowflake, GuildData> _datas = new();
|
private readonly ConcurrentDictionary<Snowflake, GuildData> _datas = new();
|
||||||
private readonly IDiscordRestGuildAPI _guildApi;
|
|
||||||
private readonly ILogger<GuildDataService> _logger;
|
private readonly ILogger<GuildDataService> _logger;
|
||||||
|
|
||||||
// https://github.com/dotnet/aspnetcore/issues/39139
|
// https://github.com/dotnet/aspnetcore/issues/39139
|
||||||
public GuildDataService(
|
public GuildDataService(
|
||||||
IHostApplicationLifetime lifetime, IDiscordRestGuildAPI guildApi, ILogger<GuildDataService> logger)
|
IHostApplicationLifetime lifetime, ILogger<GuildDataService> logger)
|
||||||
{
|
{
|
||||||
_guildApi = guildApi;
|
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
lifetime.ApplicationStopping.Register(ApplicationStopping);
|
lifetime.ApplicationStopping.Register(ApplicationStopping);
|
||||||
}
|
}
|
||||||
|
@ -110,15 +107,6 @@ public sealed class GuildDataService : IHostedService
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.MutedUntil is null)
|
|
||||||
{
|
|
||||||
var memberResult = await _guildApi.GetGuildMemberAsync(guildId, data.Id.ToSnowflake(), ct);
|
|
||||||
if (memberResult.IsSuccess)
|
|
||||||
{
|
|
||||||
data.Roles = memberResult.Entity.Roles.ToList().ConvertAll(r => r.Value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
memberData.Add(data.Id, data);
|
memberData.Add(data.Id, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -150,11 +138,6 @@ public sealed class GuildDataService : IHostedService
|
||||||
return (await GetData(guildId, ct)).Settings;
|
return (await GetData(guildId, ct)).Settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<MemberData> GetMemberData(Snowflake guildId, Snowflake userId, CancellationToken ct = default)
|
|
||||||
{
|
|
||||||
return (await GetData(guildId, ct)).GetOrCreateMemberData(userId);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ICollection<Snowflake> GetGuildIds()
|
public ICollection<Snowflake> GetGuildIds()
|
||||||
{
|
{
|
||||||
return _datas.Keys;
|
return _datas.Keys;
|
||||||
|
|
|
@ -89,6 +89,11 @@ public sealed partial class MemberUpdateService : BackgroundService
|
||||||
return failedResults.AggregateErrors();
|
return failedResults.AggregateErrors();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (data.MutedUntil is null)
|
||||||
|
{
|
||||||
|
data.Roles = guildMember.Roles.ToList().ConvertAll(r => r.Value);
|
||||||
|
}
|
||||||
|
|
||||||
var autoUnmuteResult = await TryAutoUnmuteAsync(guildId, id, data, ct);
|
var autoUnmuteResult = await TryAutoUnmuteAsync(guildId, id, data, ct);
|
||||||
failedResults.AddIfFailed(autoUnmuteResult);
|
failedResults.AddIfFailed(autoUnmuteResult);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue