forked from TeamInklings/Octobot
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:
parent
4cb39a34b5
commit
84e730838b
39 changed files with 2917 additions and 623 deletions
|
@ -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();
|
||||
}
|
||||
|
|
Reference in a new issue