1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-05-05 13:36:30 +03:00

Add a new .editorconfig and reformat code (#76)

*I'll start working on features and bugfixes after this PR, I promise*
very short summary:
- no more braceless statements
- braces are on new lines now
- `sealed` on everything that can be `sealed`
- no more awkwardly looking alignment of fields/parameters
- no more `Service` suffix on service fields. yeah.
- no more `else`s. who needs them?
- code style is now enforced by CI

---------

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2023-08-03 01:51:16 +05:00 committed by GitHub
parent 4cb39a34b5
commit 84e730838b
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
39 changed files with 2917 additions and 623 deletions

View file

@ -11,39 +11,47 @@ namespace Boyfriend.Services;
/// <summary>
/// Handles saving, loading, initializing and providing <see cref="GuildData" />.
/// </summary>
public class GuildDataService : IHostedService {
public sealed class GuildDataService : IHostedService
{
private readonly ConcurrentDictionary<Snowflake, GuildData> _datas = new();
private readonly IDiscordRestGuildAPI _guildApi;
private readonly IDiscordRestGuildAPI _guildApi;
// https://github.com/dotnet/aspnetcore/issues/39139
public GuildDataService(
IHostApplicationLifetime lifetime, IDiscordRestGuildAPI guildApi) {
IHostApplicationLifetime lifetime, IDiscordRestGuildAPI guildApi)
{
_guildApi = guildApi;
lifetime.ApplicationStopping.Register(ApplicationStopping);
}
public Task StartAsync(CancellationToken ct) {
public Task StartAsync(CancellationToken ct)
{
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken ct) {
public Task StopAsync(CancellationToken ct)
{
return Task.CompletedTask;
}
private void ApplicationStopping() {
private void ApplicationStopping()
{
SaveAsync(CancellationToken.None).GetAwaiter().GetResult();
}
private async Task SaveAsync(CancellationToken ct) {
private async Task SaveAsync(CancellationToken ct)
{
var tasks = new List<Task>();
foreach (var data in _datas.Values) {
foreach (var data in _datas.Values)
{
await using var settingsStream = File.OpenWrite(data.SettingsPath);
tasks.Add(JsonSerializer.SerializeAsync(settingsStream, data.Settings, cancellationToken: ct));
await using var eventsStream = File.OpenWrite(data.ScheduledEventsPath);
tasks.Add(JsonSerializer.SerializeAsync(eventsStream, data.ScheduledEvents, cancellationToken: ct));
foreach (var memberData in data.MemberData.Values) {
foreach (var memberData in data.MemberData.Values)
{
await using var memberDataStream = File.OpenWrite($"{data.MemberDataPath}/{memberData.Id}.json");
tasks.Add(JsonSerializer.SerializeAsync(memberDataStream, memberData, cancellationToken: ct));
}
@ -52,19 +60,36 @@ public class GuildDataService : IHostedService {
await Task.WhenAll(tasks);
}
public async Task<GuildData> GetData(Snowflake guildId, CancellationToken ct = default) {
public async Task<GuildData> GetData(Snowflake guildId, CancellationToken ct = default)
{
return _datas.TryGetValue(guildId, out var data) ? data : await InitializeData(guildId, ct);
}
private async Task<GuildData> InitializeData(Snowflake guildId, CancellationToken ct = default) {
private async Task<GuildData> InitializeData(Snowflake guildId, CancellationToken ct = default)
{
var idString = $"{guildId}";
var memberDataPath = $"{guildId}/MemberData";
var settingsPath = $"{guildId}/Settings.json";
var scheduledEventsPath = $"{guildId}/ScheduledEvents.json";
if (!Directory.Exists(idString)) Directory.CreateDirectory(idString);
if (!Directory.Exists(memberDataPath)) Directory.CreateDirectory(memberDataPath);
if (!File.Exists(settingsPath)) await File.WriteAllTextAsync(settingsPath, "{}", ct);
if (!File.Exists(scheduledEventsPath)) await File.WriteAllTextAsync(scheduledEventsPath, "{}", ct);
if (!Directory.Exists(idString))
{
Directory.CreateDirectory(idString);
}
if (!Directory.Exists(memberDataPath))
{
Directory.CreateDirectory(memberDataPath);
}
if (!File.Exists(settingsPath))
{
await File.WriteAllTextAsync(settingsPath, "{}", ct);
}
if (!File.Exists(scheduledEventsPath))
{
await File.WriteAllTextAsync(scheduledEventsPath, "{}", ct);
}
await using var settingsStream = File.OpenRead(settingsPath);
var jsonSettings
@ -76,13 +101,20 @@ public class GuildDataService : IHostedService {
eventsStream, cancellationToken: ct);
var memberData = new Dictionary<ulong, MemberData>();
foreach (var dataPath in Directory.GetFiles(memberDataPath)) {
foreach (var dataPath in Directory.GetFiles(memberDataPath))
{
await using var dataStream = File.OpenRead(dataPath);
var data = await JsonSerializer.DeserializeAsync<MemberData>(dataStream, cancellationToken: ct);
if (data is null) continue;
if (data is null)
{
continue;
}
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);
}
@ -91,19 +123,26 @@ public class GuildDataService : IHostedService {
jsonSettings ?? new JsonObject(), settingsPath,
await events ?? new Dictionary<ulong, ScheduledEventData>(), scheduledEventsPath,
memberData, memberDataPath);
while (!_datas.ContainsKey(guildId)) _datas.TryAdd(guildId, finalData);
while (!_datas.ContainsKey(guildId))
{
_datas.TryAdd(guildId, finalData);
}
return finalData;
}
public async Task<JsonNode> GetSettings(Snowflake guildId, CancellationToken ct = default) {
public async Task<JsonNode> GetSettings(Snowflake guildId, CancellationToken ct = default)
{
return (await GetData(guildId, ct)).Settings;
}
public async Task<MemberData> GetMemberData(Snowflake guildId, Snowflake userId, CancellationToken ct = default) {
public async Task<MemberData> GetMemberData(Snowflake guildId, Snowflake userId, CancellationToken ct = default)
{
return (await GetData(guildId, ct)).GetMemberData(userId);
}
public ICollection<Snowflake> GetGuildIds() {
public ICollection<Snowflake> GetGuildIds()
{
return _datas.Keys;
}
}

View file

@ -20,8 +20,10 @@ namespace Boyfriend.Services;
/// <summary>
/// Handles executing guild updates (also called "ticks") once per second.
/// </summary>
public partial class GuildUpdateService : BackgroundService {
private static readonly (string Name, TimeSpan Duration)[] SongList = {
public sealed partial 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)),
@ -36,7 +38,8 @@ public partial class GuildUpdateService : BackgroundService {
("Noisecream - Mist of Rage", new TimeSpan(0, 2, 25))
};
private static readonly string[] GenericNicknames = {
private static readonly string[] GenericNicknames =
{
"Albatross", "Alpha", "Anchor", "Banjo", "Bell", "Beta", "Blackbird", "Bulldog", "Canary",
"Cat", "Calf", "Cyclone", "Daisy", "Dalmatian", "Dart", "Delta", "Diamond", "Donkey", "Duck",
"Emu", "Eclipse", "Flamingo", "Flute", "Frog", "Goose", "Hatchet", "Heron", "Husky", "Hurricane",
@ -48,25 +51,26 @@ public partial class GuildUpdateService : BackgroundService {
private readonly List<Activity> _activityList = new(1) { new Activity("with Remora.Discord", ActivityType.Game) };
private readonly IDiscordRestChannelAPI _channelApi;
private readonly DiscordGatewayClient _client;
private readonly GuildDataService _dataService;
private readonly IDiscordRestChannelAPI _channelApi;
private readonly DiscordGatewayClient _client;
private readonly IDiscordRestGuildScheduledEventAPI _eventApi;
private readonly IDiscordRestGuildAPI _guildApi;
private readonly ILogger<GuildUpdateService> _logger;
private readonly IDiscordRestUserAPI _userApi;
private readonly UtilityService _utility;
private readonly IDiscordRestGuildAPI _guildApi;
private readonly GuildDataService _guildData;
private readonly ILogger<GuildUpdateService> _logger;
private readonly IDiscordRestUserAPI _userApi;
private readonly UtilityService _utility;
private DateTimeOffset _nextSongAt = DateTimeOffset.MinValue;
private uint _nextSongIndex;
private uint _nextSongIndex;
public GuildUpdateService(
IDiscordRestChannelAPI channelApi, DiscordGatewayClient client, GuildDataService dataService,
IDiscordRestChannelAPI channelApi, DiscordGatewayClient client, GuildDataService guildData,
IDiscordRestGuildScheduledEventAPI eventApi, IDiscordRestGuildAPI guildApi, ILogger<GuildUpdateService> logger,
IDiscordRestUserAPI userApi, UtilityService utility) {
IDiscordRestUserAPI userApi, UtilityService utility)
{
_channelApi = channelApi;
_client = client;
_dataService = dataService;
_guildData = guildData;
_eventApi = eventApi;
_guildApi = guildApi;
_logger = logger;
@ -76,17 +80,20 @@ public partial class GuildUpdateService : BackgroundService {
/// <summary>
/// Activates a periodic timer with a 1 second interval and adds guild update tasks on each timer tick.
/// Additionally, updates the current presence with songs from <see cref="SongList"/>.
/// Additionally, updates the current presence with songs from <see cref="SongList" />.
/// </summary>
/// <remarks>If update tasks take longer than 1 second, the next timer tick will be skipped.</remarks>
/// <param name="ct">The cancellation token for this operation.</param>
protected override async Task ExecuteAsync(CancellationToken ct) {
protected override async Task ExecuteAsync(CancellationToken ct)
{
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(1));
var tasks = new List<Task>();
while (await timer.WaitForNextTickAsync(ct)) {
var guildIds = _dataService.GetGuildIds();
if (guildIds.Count > 0 && DateTimeOffset.UtcNow >= _nextSongAt) {
while (await timer.WaitForNextTickAsync(ct))
{
var guildIds = _guildData.GetGuildIds();
if (guildIds.Count > 0 && DateTimeOffset.UtcNow >= _nextSongAt)
{
var nextSong = SongList[_nextSongIndex];
_activityList[0] = new Activity(nextSong.Name, ActivityType.Listening);
_client.SubmitCommand(
@ -94,7 +101,10 @@ public partial class GuildUpdateService : BackgroundService {
UserStatus.Online, false, DateTimeOffset.UtcNow, _activityList));
_nextSongAt = DateTimeOffset.UtcNow.Add(nextSong.Duration);
_nextSongIndex++;
if (_nextSongIndex >= SongList.Length) _nextSongIndex = 0;
if (_nextSongIndex >= SongList.Length)
{
_nextSongIndex = 0;
}
}
tasks.AddRange(guildIds.Select(id => TickGuildAsync(id, ct)));
@ -111,9 +121,9 @@ public partial class GuildUpdateService : BackgroundService {
/// This method does the following:
/// <list type="bullet">
/// <item>Automatically unbans users once their ban period has expired.</item>
/// <item>Automatically grants members the guild's <see cref="GuildSettings.DefaultRole"/> if one is set.</item>
/// <item>Automatically grants members the guild's <see cref="GuildSettings.DefaultRole" /> if one is set.</item>
/// <item>Sends reminders about an upcoming scheduled event.</item>
/// <item>Automatically starts scheduled events if <see cref="GuildSettings.AutoStartEvents"/> is enabled.</item>
/// <item>Automatically starts scheduled events if <see cref="GuildSettings.AutoStartEvents" /> is enabled.</item>
/// <item>Sends scheduled event start notifications.</item>
/// <item>Sends scheduled event completion notifications.</item>
/// <item>Sends reminders to members.</item>
@ -129,41 +139,62 @@ public partial class GuildUpdateService : BackgroundService {
/// </remarks>
/// <param name="guildId">The ID of the guild to update.</param>
/// <param name="ct">The cancellation token for this operation.</param>
private async Task TickGuildAsync(Snowflake guildId, CancellationToken ct = default) {
var data = await _dataService.GetData(guildId, ct);
private async Task TickGuildAsync(Snowflake guildId, CancellationToken ct = default)
{
var data = await _guildData.GetData(guildId, ct);
Messages.Culture = GuildSettings.Language.Get(data.Settings);
var defaultRole = GuildSettings.DefaultRole.Get(data.Settings);
foreach (var memberData in data.MemberData.Values) {
foreach (var memberData in data.MemberData.Values)
{
var guildMemberResult = await _guildApi.GetGuildMemberAsync(guildId, memberData.Id.ToSnowflake(), ct);
if (!guildMemberResult.IsDefined(out var guildMember)) return;
if (!guildMember.User.IsDefined(out var user)) return;
if (!guildMemberResult.IsDefined(out var guildMember))
{
return;
}
if (!guildMember.User.IsDefined(out var user))
{
return;
}
await TickMemberAsync(guildId, user, guildMember, memberData, defaultRole, data.Settings, ct);
}
var eventsResult = await _eventApi.ListScheduledEventsForGuildAsync(guildId, ct: ct);
if (!eventsResult.IsSuccess)
{
_logger.LogWarning("Error retrieving scheduled events.\n{ErrorMessage}", eventsResult.Error.Message);
else if (!GuildSettings.EventNotificationChannel.Get(data.Settings).Empty())
return;
}
if (!GuildSettings.EventNotificationChannel.Get(data.Settings).Empty())
{
await TickScheduledEventsAsync(guildId, data, eventsResult.Entity, ct);
}
}
private async Task TickScheduledEventsAsync(
Snowflake guildId, GuildData data, IEnumerable<IGuildScheduledEvent> events, CancellationToken ct) {
foreach (var scheduledEvent in events) {
Snowflake guildId, GuildData data, IEnumerable<IGuildScheduledEvent> events, CancellationToken ct)
{
foreach (var scheduledEvent in events)
{
if (!data.ScheduledEvents.ContainsKey(scheduledEvent.ID.Value))
{
data.ScheduledEvents.Add(scheduledEvent.ID.Value, new ScheduledEventData(scheduledEvent.Status));
}
var storedEvent = data.ScheduledEvents[scheduledEvent.ID.Value];
if (storedEvent.Status == scheduledEvent.Status) {
if (storedEvent.Status == scheduledEvent.Status)
{
await TickScheduledEventAsync(guildId, data, scheduledEvent, storedEvent, ct);
continue;
}
storedEvent.Status = scheduledEvent.Status;
var statusChangedResponseResult = storedEvent.Status switch {
var statusChangedResponseResult = storedEvent.Status switch
{
GuildScheduledEventStatus.Scheduled =>
await SendScheduledEventCreatedMessage(scheduledEvent, data.Settings, ct),
GuildScheduledEventStatus.Active or GuildScheduledEventStatus.Completed =>
@ -172,16 +203,20 @@ public partial class GuildUpdateService : BackgroundService {
};
if (!statusChangedResponseResult.IsSuccess)
{
_logger.LogWarning(
"Error handling scheduled event status update.\n{ErrorMessage}",
statusChangedResponseResult.Error.Message);
}
}
}
private async Task TickScheduledEventAsync(
Snowflake guildId, GuildData data, IGuildScheduledEvent scheduledEvent, ScheduledEventData eventData,
CancellationToken ct) {
if (DateTimeOffset.UtcNow >= scheduledEvent.ScheduledStartTime) {
Snowflake guildId, GuildData data, IGuildScheduledEvent scheduledEvent, ScheduledEventData eventData,
CancellationToken ct)
{
if (DateTimeOffset.UtcNow >= scheduledEvent.ScheduledStartTime)
{
await TryAutoStartEventAsync(guildId, data, scheduledEvent, ct);
return;
}
@ -190,10 +225,14 @@ public partial class GuildUpdateService : BackgroundService {
|| eventData.EarlyNotificationSent
|| DateTimeOffset.UtcNow
< scheduledEvent.ScheduledStartTime
- GuildSettings.EventEarlyNotificationOffset.Get(data.Settings)) return;
- GuildSettings.EventEarlyNotificationOffset.Get(data.Settings))
{
return;
}
var earlyResult = await SendEarlyEventNotificationAsync(scheduledEvent, data, ct);
if (earlyResult.IsSuccess) {
if (earlyResult.IsSuccess)
{
eventData.EarlyNotificationSent = true;
return;
}
@ -204,54 +243,82 @@ public partial class GuildUpdateService : BackgroundService {
}
private async Task TryAutoStartEventAsync(
Snowflake guildId, GuildData data, IGuildScheduledEvent scheduledEvent, CancellationToken ct) {
Snowflake guildId, GuildData data, IGuildScheduledEvent scheduledEvent, CancellationToken ct)
{
if (GuildSettings.AutoStartEvents.Get(data.Settings)
&& scheduledEvent.Status is not GuildScheduledEventStatus.Active) {
&& scheduledEvent.Status is not GuildScheduledEventStatus.Active)
{
var startResult = await _eventApi.ModifyGuildScheduledEventAsync(
guildId, scheduledEvent.ID,
status: GuildScheduledEventStatus.Active, ct: ct);
if (!startResult.IsSuccess)
{
_logger.LogWarning(
"Error in automatic scheduled event start request.\n{ErrorMessage}",
startResult.Error.Message);
}
}
}
private async Task TickMemberAsync(
Snowflake guildId, IUser user, IGuildMember member, MemberData memberData, Snowflake defaultRole,
JsonNode cfg, CancellationToken ct) {
Snowflake guildId, IUser user, IGuildMember member, MemberData memberData, Snowflake defaultRole,
JsonNode cfg, CancellationToken ct)
{
if (defaultRole.Value is not 0 && !memberData.Roles.Contains(defaultRole.Value))
{
_ = _guildApi.AddGuildMemberRoleAsync(
guildId, user.ID, defaultRole, ct: ct);
}
if (DateTimeOffset.UtcNow > memberData.BannedUntil) {
if (DateTimeOffset.UtcNow > memberData.BannedUntil)
{
var unbanResult = await _guildApi.RemoveGuildBanAsync(
guildId, user.ID, Messages.PunishmentExpired.EncodeHeader(), ct);
if (unbanResult.IsSuccess)
memberData.BannedUntil = null;
else
if (!unbanResult.IsSuccess)
{
_logger.LogWarning(
"Error in automatic user unban request.\n{ErrorMessage}", unbanResult.Error.Message);
return;
}
memberData.BannedUntil = null;
}
for (var i = memberData.Reminders.Count - 1; i >= 0; i--)
{
await TickReminderAsync(memberData.Reminders[i], user, memberData, ct);
if (GuildSettings.RenameHoistedUsers.Get(cfg)) await FilterNicknameAsync(guildId, user, member, ct);
}
if (GuildSettings.RenameHoistedUsers.Get(cfg))
{
await FilterNicknameAsync(guildId, user, member, ct);
}
}
private Task FilterNicknameAsync(Snowflake guildId, IUser user, IGuildMember member, CancellationToken ct) {
private Task FilterNicknameAsync(Snowflake guildId, IUser user, IGuildMember member, CancellationToken ct)
{
var currentNickname = member.Nickname.IsDefined(out var nickname)
? nickname
: user.GlobalName ?? user.Username;
var characterList = currentNickname.ToList();
var usernameChanged = false;
foreach (var character in currentNickname)
if (IllegalCharsRegex().IsMatch(character.ToString())) {
{
if (IllegalChars().IsMatch(character.ToString()))
{
characterList.Remove(character);
usernameChanged = true;
} else { break; }
continue;
}
break;
}
if (!usernameChanged)
{
return Task.CompletedTask;
}
if (!usernameChanged) return Task.CompletedTask;
var newNickname = string.Concat(characterList.ToArray());
_ = _guildApi.ModifyGuildMemberAsync(
@ -264,10 +331,14 @@ public partial class GuildUpdateService : BackgroundService {
}
[GeneratedRegex("[^0-9A-zЁА-яё]")]
private static partial Regex IllegalCharsRegex();
private static partial Regex IllegalChars();
private async Task TickReminderAsync(Reminder reminder, IUser user, MemberData memberData, CancellationToken ct) {
if (DateTimeOffset.UtcNow < reminder.At) return;
private async Task TickReminderAsync(Reminder reminder, IUser user, MemberData memberData, CancellationToken ct)
{
if (DateTimeOffset.UtcNow < reminder.At)
{
return;
}
var embed = new EmbedBuilder().WithSmallTitle(
string.Format(Messages.Reminder, user.GetTag()), user)
@ -276,13 +347,18 @@ public partial class GuildUpdateService : BackgroundService {
.WithColour(ColorsList.Magenta)
.Build();
if (!embed.IsDefined(out var built)) return;
if (!embed.IsDefined(out var built))
{
return;
}
var messageResult = await _channelApi.CreateMessageAsync(
reminder.Channel.ToSnowflake(), Mention.User(user), embeds: new[] { built }, ct: ct);
if (!messageResult.IsSuccess)
{
_logger.LogWarning(
"Error in reminder send.\n{ErrorMessage}", messageResult.Error.Message);
}
memberData.Reminders.Remove(reminder);
}
@ -298,15 +374,19 @@ public partial class GuildUpdateService : BackgroundService {
/// <param name="ct">The cancellation token for this operation.</param>
/// <returns>A notification sending result which may or may not have succeeded.</returns>
private async Task<Result> SendScheduledEventCreatedMessage(
IGuildScheduledEvent scheduledEvent, JsonNode settings, CancellationToken ct = default) {
IGuildScheduledEvent scheduledEvent, JsonNode settings, CancellationToken ct = default)
{
if (!scheduledEvent.Creator.IsDefined(out var creator))
{
return new ArgumentNullError(nameof(scheduledEvent.Creator));
}
Result<string> embedDescriptionResult;
var eventDescription = scheduledEvent.Description is { HasValue: true, Value: not null }
? scheduledEvent.Description.Value
: string.Empty;
embedDescriptionResult = scheduledEvent.EntityType switch {
embedDescriptionResult = scheduledEvent.EntityType switch
{
GuildScheduledEventEntityType.StageInstance or GuildScheduledEventEntityType.Voice =>
GetLocalEventCreatedEmbedDescription(scheduledEvent, eventDescription),
GuildScheduledEventEntityType.External => GetExternalScheduledEventCreatedEmbedDescription(
@ -315,7 +395,9 @@ public partial class GuildUpdateService : BackgroundService {
};
if (!embedDescriptionResult.IsDefined(out var embedDescription))
{
return Result.FromError(embedDescriptionResult);
}
var embed = new EmbedBuilder()
.WithSmallTitle(string.Format(Messages.EventCreatedTitle, creator.GetTag()), creator)
@ -325,7 +407,10 @@ public partial class GuildUpdateService : BackgroundService {
.WithCurrentTimestamp()
.WithColour(ColorsList.White)
.Build();
if (!embed.IsDefined(out var built)) return Result.FromError(embed);
if (!embed.IsDefined(out var built))
{
return Result.FromError(embed);
}
var roleMention = !GuildSettings.EventNotificationRole.Get(settings).Empty()
? Mention.Role(GuildSettings.EventNotificationRole.Get(settings))
@ -345,14 +430,23 @@ public partial class GuildUpdateService : BackgroundService {
}
private static Result<string> GetExternalScheduledEventCreatedEmbedDescription(
IGuildScheduledEvent scheduledEvent, string eventDescription) {
IGuildScheduledEvent scheduledEvent, string eventDescription)
{
Result<string> embedDescription;
if (!scheduledEvent.EntityMetadata.AsOptional().IsDefined(out var metadata))
{
return new ArgumentNullError(nameof(scheduledEvent.EntityMetadata));
}
if (!scheduledEvent.ScheduledEndTime.AsOptional().IsDefined(out var endTime))
{
return new ArgumentNullError(nameof(scheduledEvent.ScheduledEndTime));
}
if (!metadata.Location.IsDefined(out var location))
{
return new ArgumentNullError(nameof(metadata.Location));
}
embedDescription = $"{eventDescription}\n\n{Markdown.BlockQuote(
string.Format(
@ -365,9 +459,12 @@ public partial class GuildUpdateService : BackgroundService {
}
private static Result<string> GetLocalEventCreatedEmbedDescription(
IGuildScheduledEvent scheduledEvent, string eventDescription) {
IGuildScheduledEvent scheduledEvent, string eventDescription)
{
if (!scheduledEvent.ChannelID.AsOptional().IsDefined(out var channelId))
{
return new ArgumentNullError(nameof(scheduledEvent.ChannelID));
}
return $"{eventDescription}\n\n{Markdown.BlockQuote(
string.Format(
@ -378,7 +475,8 @@ public partial class GuildUpdateService : BackgroundService {
}
/// <summary>
/// Handles sending a notification, mentioning the <see cref="GuildSettings.EventNotificationRole"/> and event subscribers,
/// Handles sending a notification, mentioning the <see cref="GuildSettings.EventNotificationRole" /> and event
/// subscribers,
/// when a scheduled event has started or completed
/// in a guild's <see cref="GuildSettings.EventNotificationChannel" /> if one is set.
/// </summary>
@ -387,11 +485,14 @@ public partial class GuildUpdateService : BackgroundService {
/// <param name="ct">The cancellation token for this operation</param>
/// <returns>A reminder/notification sending result which may or may not have succeeded.</returns>
private async Task<Result> SendScheduledEventUpdatedMessage(
IGuildScheduledEvent scheduledEvent, GuildData data, CancellationToken ct = default) {
if (scheduledEvent.Status == GuildScheduledEventStatus.Active) {
IGuildScheduledEvent scheduledEvent, GuildData data, CancellationToken ct = default)
{
if (scheduledEvent.Status == GuildScheduledEventStatus.Active)
{
data.ScheduledEvents[scheduledEvent.ID.Value].ActualStartTime = DateTimeOffset.UtcNow;
var embedDescriptionResult = scheduledEvent.EntityType switch {
var embedDescriptionResult = scheduledEvent.EntityType switch
{
GuildScheduledEventEntityType.StageInstance or GuildScheduledEventEntityType.Voice =>
GetLocalEventStartedEmbedDescription(scheduledEvent),
GuildScheduledEventEntityType.External => GetExternalEventStartedEmbedDescription(scheduledEvent),
@ -401,9 +502,14 @@ public partial class GuildUpdateService : BackgroundService {
var contentResult = await _utility.GetEventNotificationMentions(
scheduledEvent, data.Settings, ct);
if (!contentResult.IsDefined(out var content))
{
return Result.FromError(contentResult);
}
if (!embedDescriptionResult.IsDefined(out var embedDescription))
{
return Result.FromError(embedDescriptionResult);
}
var startedEmbed = new EmbedBuilder().WithTitle(string.Format(Messages.EventStarted, scheduledEvent.Name))
.WithDescription(embedDescription)
@ -411,7 +517,10 @@ public partial class GuildUpdateService : BackgroundService {
.WithCurrentTimestamp()
.Build();
if (!startedEmbed.IsDefined(out var startedBuilt)) return Result.FromError(startedEmbed);
if (!startedEmbed.IsDefined(out var startedBuilt))
{
return Result.FromError(startedEmbed);
}
return (Result)await _channelApi.CreateMessageAsync(
GuildSettings.EventNotificationChannel.Get(data.Settings),
@ -419,7 +528,10 @@ public partial class GuildUpdateService : BackgroundService {
}
if (scheduledEvent.Status != GuildScheduledEventStatus.Completed)
{
return new ArgumentOutOfRangeError(nameof(scheduledEvent.Status));
}
data.ScheduledEvents.Remove(scheduledEvent.ID.Value);
var completedEmbed = new EmbedBuilder().WithTitle(string.Format(Messages.EventCompleted, scheduledEvent.Name))
@ -434,17 +546,22 @@ public partial class GuildUpdateService : BackgroundService {
.Build();
if (!completedEmbed.IsDefined(out var completedBuilt))
{
return Result.FromError(completedEmbed);
}
return (Result)await _channelApi.CreateMessageAsync(
GuildSettings.EventNotificationChannel.Get(data.Settings),
embeds: new[] { completedBuilt }, ct: ct);
}
private static Result<string> GetLocalEventStartedEmbedDescription(IGuildScheduledEvent scheduledEvent) {
private static Result<string> GetLocalEventStartedEmbedDescription(IGuildScheduledEvent scheduledEvent)
{
Result<string> embedDescription;
if (!scheduledEvent.ChannelID.AsOptional().IsDefined(out var channelId))
{
return new ArgumentNullError(nameof(scheduledEvent.ChannelID));
}
embedDescription = string.Format(
Messages.DescriptionLocalEventStarted,
@ -453,14 +570,23 @@ public partial class GuildUpdateService : BackgroundService {
return embedDescription;
}
private static Result<string> GetExternalEventStartedEmbedDescription(IGuildScheduledEvent scheduledEvent) {
private static Result<string> GetExternalEventStartedEmbedDescription(IGuildScheduledEvent scheduledEvent)
{
Result<string> embedDescription;
if (!scheduledEvent.EntityMetadata.AsOptional().IsDefined(out var metadata))
{
return new ArgumentNullError(nameof(scheduledEvent.EntityMetadata));
}
if (!scheduledEvent.ScheduledEndTime.AsOptional().IsDefined(out var endTime))
{
return new ArgumentNullError(nameof(scheduledEvent.ScheduledEndTime));
}
if (!metadata.Location.IsDefined(out var location))
{
return new ArgumentNullError(nameof(metadata.Location));
}
embedDescription = string.Format(
Messages.DescriptionExternalEventStarted,
@ -471,14 +597,20 @@ public partial class GuildUpdateService : BackgroundService {
}
private async Task<Result> SendEarlyEventNotificationAsync(
IGuildScheduledEvent scheduledEvent, GuildData data, CancellationToken ct) {
IGuildScheduledEvent scheduledEvent, GuildData data, CancellationToken ct)
{
var currentUserResult = await _userApi.GetCurrentUserAsync(ct);
if (!currentUserResult.IsDefined(out var currentUser)) return Result.FromError(currentUserResult);
if (!currentUserResult.IsDefined(out var currentUser))
{
return Result.FromError(currentUserResult);
}
var contentResult = await _utility.GetEventNotificationMentions(
scheduledEvent, data.Settings, ct);
if (!contentResult.IsDefined(out var content))
{
return Result.FromError(contentResult);
}
var earlyResult = new EmbedBuilder()
.WithSmallTitle(string.Format(Messages.EventEarlyNotification, scheduledEvent.Name), currentUser)
@ -486,7 +618,10 @@ public partial class GuildUpdateService : BackgroundService {
.WithCurrentTimestamp()
.Build();
if (!earlyResult.IsDefined(out var earlyBuilt)) return Result.FromError(earlyResult);
if (!earlyResult.IsDefined(out var earlyBuilt))
{
return Result.FromError(earlyResult);
}
return (Result)await _channelApi.CreateMessageAsync(
GuildSettings.EventNotificationChannel.Get(data.Settings),

View file

@ -16,26 +16,30 @@ namespace Boyfriend.Services;
/// Provides utility methods that cannot be transformed to extension methods because they require usage
/// of some Discord APIs.
/// </summary>
public class UtilityService : IHostedService {
private readonly IDiscordRestChannelAPI _channelApi;
public sealed class UtilityService : IHostedService
{
private readonly IDiscordRestChannelAPI _channelApi;
private readonly IDiscordRestGuildScheduledEventAPI _eventApi;
private readonly IDiscordRestGuildAPI _guildApi;
private readonly IDiscordRestUserAPI _userApi;
private readonly IDiscordRestGuildAPI _guildApi;
private readonly IDiscordRestUserAPI _userApi;
public UtilityService(
IDiscordRestChannelAPI channelApi, IDiscordRestGuildScheduledEventAPI eventApi, IDiscordRestGuildAPI guildApi,
IDiscordRestUserAPI userApi) {
IDiscordRestUserAPI userApi)
{
_channelApi = channelApi;
_eventApi = eventApi;
_guildApi = guildApi;
_userApi = userApi;
}
public Task StartAsync(CancellationToken ct) {
public Task StartAsync(CancellationToken ct)
{
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken ct) {
public Task StopAsync(CancellationToken ct)
{
return Task.CompletedTask;
}
@ -58,29 +62,42 @@ public class UtilityService : IHostedService {
/// </list>
/// </returns>
public async Task<Result<string?>> CheckInteractionsAsync(
Snowflake guildId, Snowflake interacterId, Snowflake targetId, string action, CancellationToken ct = default) {
Snowflake guildId, Snowflake interacterId, Snowflake targetId, string action, CancellationToken ct = default)
{
if (interacterId == targetId)
{
return Result<string?>.FromSuccess($"UserCannot{action}Themselves".Localized());
}
var currentUserResult = await _userApi.GetCurrentUserAsync(ct);
if (!currentUserResult.IsDefined(out var currentUser))
{
return Result<string?>.FromError(currentUserResult);
}
var guildResult = await _guildApi.GetGuildAsync(guildId, ct: ct);
if (!guildResult.IsDefined(out var guild))
{
return Result<string?>.FromError(guildResult);
}
var targetMemberResult = await _guildApi.GetGuildMemberAsync(guildId, targetId, ct);
if (!targetMemberResult.IsDefined(out var targetMember))
{
return Result<string?>.FromSuccess(null);
}
var currentMemberResult = await _guildApi.GetGuildMemberAsync(guildId, currentUser.ID, ct);
if (!currentMemberResult.IsDefined(out var currentMember))
{
return Result<string?>.FromError(currentMemberResult);
}
var rolesResult = await _guildApi.GetGuildRolesAsync(guildId, ct);
if (!rolesResult.IsDefined(out var roles))
{
return Result<string?>.FromError(rolesResult);
}
var interacterResult = await _guildApi.GetGuildMemberAsync(guildId, interacterId, ct);
return interacterResult.IsDefined(out var interacter)
@ -90,26 +107,41 @@ public class UtilityService : IHostedService {
private static Result<string?> CheckInteractions(
string action, IGuild guild, IReadOnlyList<IRole> roles, IGuildMember targetMember, IGuildMember currentMember,
IGuildMember interacter) {
IGuildMember interacter)
{
if (!targetMember.User.IsDefined(out var targetUser))
{
return new ArgumentNullError(nameof(targetMember.User));
}
if (!interacter.User.IsDefined(out var interacterUser))
{
return new ArgumentNullError(nameof(interacter.User));
}
if (currentMember.User == targetMember.User)
{
return Result<string?>.FromSuccess($"UserCannot{action}Bot".Localized());
}
if (targetUser.ID == guild.OwnerID) return Result<string?>.FromSuccess($"UserCannot{action}Owner".Localized());
if (targetUser.ID == guild.OwnerID)
{
return Result<string?>.FromSuccess($"UserCannot{action}Owner".Localized());
}
var targetRoles = roles.Where(r => targetMember.Roles.Contains(r.ID)).ToList();
var botRoles = roles.Where(r => currentMember.Roles.Contains(r.ID));
var targetBotRoleDiff = targetRoles.MaxOrDefault(r => r.Position) - botRoles.MaxOrDefault(r => r.Position);
if (targetBotRoleDiff >= 0)
{
return Result<string?>.FromSuccess($"BotCannot{action}Target".Localized());
}
if (interacterUser.ID == guild.OwnerID)
{
return Result<string?>.FromSuccess(null);
}
var interacterRoles = roles.Where(r => interacter.Roles.Contains(r.ID));
var targetInteracterRoleDiff
@ -120,7 +152,8 @@ public class UtilityService : IHostedService {
}
/// <summary>
/// Gets the string mentioning the <see cref="GuildSettings.EventNotificationRole"/> and event subscribers related to a scheduled
/// Gets the string mentioning the <see cref="GuildSettings.EventNotificationRole" /> and event subscribers related to
/// a scheduled
/// event.
/// </summary>
/// <param name="scheduledEvent">
@ -130,21 +163,24 @@ public class UtilityService : IHostedService {
/// <param name="ct">The cancellation token for this operation.</param>
/// <returns>A result containing the string which may or may not have succeeded.</returns>
public async Task<Result<string>> GetEventNotificationMentions(
IGuildScheduledEvent scheduledEvent, JsonNode settings, CancellationToken ct = default) {
IGuildScheduledEvent scheduledEvent, JsonNode settings, CancellationToken ct = default)
{
var builder = new StringBuilder();
var role = GuildSettings.EventNotificationRole.Get(settings);
var usersResult = await _eventApi.GetGuildScheduledEventUsersAsync(
scheduledEvent.GuildID, scheduledEvent.ID, withMember: true, ct: ct);
if (!usersResult.IsDefined(out var users)) return Result<string>.FromError(usersResult);
if (!usersResult.IsDefined(out var users))
{
return Result<string>.FromError(usersResult);
}
if (role.Value is not 0)
{
builder.Append($"{Mention.Role(role)} ");
}
builder = users.Where(
user => {
if (!user.GuildMember.IsDefined(out var member)) return true;
return !member.Roles.Contains(role);
})
user => user.GuildMember.IsDefined(out var member) && !member.Roles.Contains(role))
.Aggregate(builder, (current, user) => current.Append($"{Mention.User(user.User)} "));
return builder.ToString();
}
@ -160,17 +196,22 @@ public class UtilityService : IHostedService {
/// <param name="description">The description of the embed.</param>
/// <param name="avatar">The user whose avatar will be displayed next to the <paramref name="title" /> of the embed.</param>
/// <param name="color">The color of the embed.</param>
/// <param name="isPublic">Whether or not the embed should be sent in <see cref="GuildSettings.PublicFeedbackChannel"/></param>
/// <param name="isPublic">
/// Whether or not the embed should be sent in <see cref="GuildSettings.PublicFeedbackChannel" />
/// </param>
/// <param name="ct">The cancellation token for this operation.</param>
/// <returns>A result which has succeeded.</returns>
public Result LogActionAsync(
JsonNode cfg, Snowflake channelId, IUser user, string title, string description, IUser avatar,
Color color, bool isPublic = true, CancellationToken ct = default) {
JsonNode cfg, Snowflake channelId, IUser user, string title, string description, IUser avatar,
Color color, bool isPublic = true, CancellationToken ct = default)
{
var publicChannel = GuildSettings.PublicFeedbackChannel.Get(cfg);
var privateChannel = GuildSettings.PrivateFeedbackChannel.Get(cfg);
if (GuildSettings.PublicFeedbackChannel.Get(cfg).EmptyOrEqualTo(channelId)
&& GuildSettings.PrivateFeedbackChannel.Get(cfg).EmptyOrEqualTo(channelId))
{
return Result.FromSuccess();
}
var logEmbed = new EmbedBuilder().WithSmallTitle(title, avatar)
.WithDescription(description)
@ -180,20 +221,27 @@ public class UtilityService : IHostedService {
.Build();
if (!logEmbed.IsDefined(out var logBuilt))
{
return Result.FromError(logEmbed);
}
var builtArray = new[] { logBuilt };
// Not awaiting to reduce response time
if (isPublic && publicChannel != channelId)
{
_ = _channelApi.CreateMessageAsync(
publicChannel, embeds: builtArray,
ct: ct);
}
if (privateChannel != publicChannel
&& privateChannel != channelId)
{
_ = _channelApi.CreateMessageAsync(
privateChannel, embeds: builtArray,
ct: ct);
}
return Result.FromSuccess();
}