diff --git a/Commands/ErrorLoggingPreparationErrorEvent.cs b/Commands/ErrorLoggingEvents.cs similarity index 100% rename from Commands/ErrorLoggingPreparationErrorEvent.cs rename to Commands/ErrorLoggingEvents.cs diff --git a/Commands/MuteCommandGroup.cs b/Commands/MuteCommandGroup.cs index ef1e981..c13cf59 100644 --- a/Commands/MuteCommandGroup.cs +++ b/Commands/MuteCommandGroup.cs @@ -21,7 +21,7 @@ using Remora.Results; namespace Boyfriend.Commands; /// -/// Handles commands related to mute management: /mute and unmute. +/// Handles commands related to mute management: /mute and /unmute. /// public class MuteCommandGroup : CommandGroup { private readonly IDiscordRestChannelAPI _channelApi; @@ -65,8 +65,10 @@ public class MuteCommandGroup : CommandGroup { [RequireBotDiscordPermissions(DiscordPermission.ModerateMembers)] [Description("мутит друга <3")] public async Task MuteUserAsync( - [Description("друг которого нужно замутить ПОТОМУ-ЧТО ОН ЗАЕБАЛ")] IUser target, - [Description("причина зачем мутить друга (пиши заебал)")] string reason, + [Description("друг которого нужно замутить ПОТОМУ-ЧТО ОН ЗАЕБАЛ")] + IUser target, + [Description("причина зачем мутить друга (пиши заебал)")] + string reason, TimeSpan duration) { // Data checks if (!_context.TryGetGuildID(out var guildId)) @@ -82,7 +84,8 @@ public class MuteCommandGroup : CommandGroup { return Result.FromError(currentUserResult); var interactionResult - = await _utility.CheckInteractionsAsync(guildId.Value, userId.Value, target.ID, "Timeout", CancellationToken); + = await _utility.CheckInteractionsAsync( + guildId.Value, userId.Value, target.ID, "Timeout", CancellationToken); if (!interactionResult.IsSuccess) return Result.FromError(interactionResult); @@ -90,8 +93,6 @@ public class MuteCommandGroup : CommandGroup { var cfg = data.Configuration; Messages.Culture = data.Culture; - var newCoolDuration = DateTimeOffset.UtcNow.Add(duration); - Result responseEmbed; if (interactionResult.Entity is not null) { responseEmbed = new EmbedBuilder().WithSmallTitle(interactionResult.Entity, currentUser) @@ -101,9 +102,10 @@ public class MuteCommandGroup : CommandGroup { if (!userResult.IsDefined(out var user)) return Result.FromError(userResult); + var until = DateTimeOffset.UtcNow.Add(duration); // >:) var muteResult = await _guildApi.ModifyGuildMemberAsync( guildId.Value, target.ID, reason: $"({user.GetTag()}) {reason}".EncodeHeader(), - communicationDisabledUntil: newCoolDuration, ct: CancellationToken); + communicationDisabledUntil: until, ct: CancellationToken); if (!muteResult.IsSuccess) return Result.FromError(muteResult.Error); @@ -113,10 +115,10 @@ public class MuteCommandGroup : CommandGroup { if ((cfg.PublicFeedbackChannel is not 0 && cfg.PublicFeedbackChannel != channelId.Value) || (cfg.PrivateFeedbackChannel is not 0 && cfg.PrivateFeedbackChannel != channelId.Value)) { - var builder = new StringBuilder().AppendLine(string.Format(Messages.DescriptionActionReason, reason)); - builder.Append( + var builder = new StringBuilder().AppendLine(string.Format(Messages.DescriptionActionReason, reason)) + .Append( string.Format( - Messages.DescriptionActionExpiresAt, Markdown.Timestamp(newCoolDuration))); + Messages.DescriptionActionExpiresAt, Markdown.Timestamp(until))); var logEmbed = new EmbedBuilder().WithSmallTitle( string.Format(Messages.UserMuted, target.GetTag()), target) @@ -163,7 +165,7 @@ public class MuteCommandGroup : CommandGroup { /// /// /// - [Command("unmute")] + [Command("unmute", "размут")] [RequireContext(ChannelContext.Guild)] [RequireDiscordPermission(DiscordPermission.ModerateMembers)] [RequireBotDiscordPermissions(DiscordPermission.ModerateMembers)] @@ -185,7 +187,8 @@ public class MuteCommandGroup : CommandGroup { var cfg = await _dataService.GetConfiguration(guildId.Value, CancellationToken); Messages.Culture = cfg.GetCulture(); - var existingMuteResult = await _guildApi.ModifyGuildMemberAsync(guildId.Value, target.ID, communicationDisabledUntil: DateTimeOffset.UtcNow); + var existingMuteResult = await _guildApi.ModifyGuildMemberAsync( + guildId.Value, target.ID, communicationDisabledUntil: DateTimeOffset.UtcNow); if (!existingMuteResult.IsSuccess) { var embed = new EmbedBuilder().WithSmallTitle(Messages.UserNotMuted, currentUser) .WithColour(ColorsList.Red).Build(); diff --git a/Services/Data/GuildDataService.cs b/Services/Data/GuildDataService.cs index 4552597..d2d11f0 100644 --- a/Services/Data/GuildDataService.cs +++ b/Services/Data/GuildDataService.cs @@ -1,3 +1,4 @@ +using System.Collections.Concurrent; using System.Text.Json; using Boyfriend.Data; using Microsoft.Extensions.Hosting; @@ -11,9 +12,9 @@ namespace Boyfriend.Services.Data; /// Handles saving, loading, initializing and providing . /// public class GuildDataService : IHostedService { - private readonly Dictionary _datas = new(); - private readonly IDiscordRestGuildAPI _guildApi; - private readonly ILogger _logger; + private readonly ConcurrentDictionary _datas = new(); + private readonly IDiscordRestGuildAPI _guildApi; + private readonly ILogger _logger; // https://github.com/dotnet/aspnetcore/issues/39139 public GuildDataService( @@ -85,8 +86,6 @@ public class GuildDataService : IHostedService { var memberResult = await _guildApi.GetGuildMemberAsync(guildId, data.Id.ToDiscordSnowflake(), ct); if (memberResult.IsSuccess) data.Roles = memberResult.Entity.Roles.ToList(); - else - _logger.LogWarning("Error in member retrieval.\n{ErrorMessage}", memberResult.Error.Message); memberData.Add(data.Id, data); } @@ -95,7 +94,7 @@ public class GuildDataService : IHostedService { await configuration ?? new GuildConfiguration(), configurationPath, await events ?? new Dictionary(), scheduledEventsPath, memberData, memberDataPath); - _datas.Add(guildId, finalData); + while (!_datas.ContainsKey(guildId)) _datas.TryAdd(guildId, finalData); return finalData; } diff --git a/Services/GuildUpdateService.cs b/Services/GuildUpdateService.cs index dcc4d1b..ea880f5 100644 --- a/Services/GuildUpdateService.cs +++ b/Services/GuildUpdateService.cs @@ -87,14 +87,9 @@ public class GuildUpdateService : BackgroundService { foreach (var memberData in data.MemberData.Values) { var userIdSnowflake = memberData.Id.ToDiscordSnowflake(); - if (!memberData.Roles.Contains(defaultRoleSnowflake)) { - var defaultRoleResult = await _guildApi.AddGuildMemberRoleAsync( + if (defaultRoleSnowflake.Value is not 0 && !memberData.Roles.Contains(defaultRoleSnowflake)) + _ = _guildApi.AddGuildMemberRoleAsync( guildId, userIdSnowflake, defaultRoleSnowflake, ct: ct); - if (!defaultRoleResult.IsSuccess) - _logger.LogWarning( - "Error in automatic default role add request.\n{ErrorMessage}", - defaultRoleResult.Error.Message); - } if (DateTimeOffset.UtcNow > memberData.BannedUntil) { var unbanResult = await _guildApi.RemoveGuildBanAsync(