Skip to content

Commit

Permalink
Support for association role.
Browse files Browse the repository at this point in the history
  • Loading branch information
Misha12 committed Feb 29, 2024
1 parent 8665e2f commit 0bd959d
Show file tree
Hide file tree
Showing 13 changed files with 1,009 additions and 16 deletions.
23 changes: 15 additions & 8 deletions src/GrillBot.App/Actions/Api/V1/Guild/GetGuildDetail.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AutoMapper;
using GrillBot.App.Managers.DataResolve;
using GrillBot.Cache.Services;
using GrillBot.Common.Extensions.Discord;
using GrillBot.Common.Managers.Localization;
Expand All @@ -11,7 +12,6 @@
using GrillBot.Core.Services.PointsService;
using GrillBot.Core.Services.PointsService.Models;
using GrillBot.Core.Services.UserMeasures;
using GrillBot.Data.Models.API;
using GrillBot.Data.Models.API.Guilds;
using GrillBot.Database.Models.Guilds;

Expand All @@ -28,8 +28,11 @@ public class GetGuildDetail : ApiAction
private IAuditLogServiceClient AuditLogServiceClient { get; }
private IUserMeasuresServiceClient UserMeasuresService { get; }

private readonly DataResolveManager _dataResolve;

public GetGuildDetail(ApiRequestContext apiContext, GrillBotDatabaseBuilder databaseBuilder, IMapper mapper, IDiscordClient discordClient, GrillBotCacheBuilder cacheBuilder,
ITextsManager texts, IPointsServiceClient pointsServiceClient, IAuditLogServiceClient auditLogServiceClient, IUserMeasuresServiceClient userMeasuresService) : base(apiContext)
ITextsManager texts, IPointsServiceClient pointsServiceClient, IAuditLogServiceClient auditLogServiceClient, IUserMeasuresServiceClient userMeasuresService,
DataResolveManager dataResolve) : base(apiContext)
{
DatabaseBuilder = databaseBuilder;
Mapper = mapper;
Expand All @@ -39,6 +42,7 @@ public GetGuildDetail(ApiRequestContext apiContext, GrillBotDatabaseBuilder data
PointsServiceClient = pointsServiceClient;
AuditLogServiceClient = auditLogServiceClient;
UserMeasuresService = userMeasuresService;
_dataResolve = dataResolve;
}

public override async Task<ApiResult> ProcessAsync()
Expand All @@ -58,22 +62,25 @@ public override async Task<ApiResult> ProcessAsync()
detail.DatabaseReport = await CreateDatabaseReportAsync(id);
detail = Mapper.Map(discordGuild, detail);
if (!string.IsNullOrEmpty(dbGuild.AdminChannelId))
detail.AdminChannel = Mapper.Map<Data.Models.API.Channels.Channel>(await discordGuild.GetChannelAsync(dbGuild.AdminChannelId.ToUlong()));
detail.AdminChannel = await _dataResolve.GetChannelAsync(discordGuild.Id, dbGuild.AdminChannelId.ToUlong());

if (!string.IsNullOrEmpty(dbGuild.EmoteSuggestionChannelId))
detail.EmoteSuggestionChannel = Mapper.Map<Data.Models.API.Channels.Channel>(await discordGuild.GetChannelAsync(dbGuild.EmoteSuggestionChannelId.ToUlong()));
detail.EmoteSuggestionChannel = await _dataResolve.GetChannelAsync(discordGuild.Id, dbGuild.EmoteSuggestionChannelId.ToUlong());

if (!string.IsNullOrEmpty(dbGuild.BoosterRoleId))
detail.BoosterRole = Mapper.Map<Role>(discordGuild.GetRole(dbGuild.BoosterRoleId.ToUlong()));
detail.BoosterRole = await _dataResolve.GetRoleAsync(dbGuild.BoosterRoleId.ToUlong());

if (!string.IsNullOrEmpty(dbGuild.MuteRoleId))
detail.MutedRole = Mapper.Map<Role>(discordGuild.GetRole(dbGuild.MuteRoleId.ToUlong()));
detail.MutedRole = await _dataResolve.GetRoleAsync(dbGuild.MuteRoleId.ToUlong());

if (!string.IsNullOrEmpty(dbGuild.VoteChannelId))
detail.VoteChannel = Mapper.Map<Data.Models.API.Channels.Channel>(await discordGuild.GetChannelAsync(dbGuild.VoteChannelId.ToUlong()));
detail.VoteChannel = await _dataResolve.GetChannelAsync(discordGuild.Id, dbGuild.VoteChannelId.ToUlong());

if (!string.IsNullOrEmpty(dbGuild.BotRoomChannelId))
detail.BotRoomChannel = Mapper.Map<Data.Models.API.Channels.Channel>(await discordGuild.GetChannelAsync(dbGuild.BotRoomChannelId.ToUlong()));
detail.BotRoomChannel = await _dataResolve.GetChannelAsync(discordGuild.Id, dbGuild.BotRoomChannelId.ToUlong());

if (!string.IsNullOrEmpty(dbGuild.AssociationRoleId))
detail.AssociationRole = await _dataResolve.GetRoleAsync(dbGuild.AssociationRoleId.ToUlong());

var guildUsers = await discordGuild.GetUsersAsync();
detail.UserStatusReport = guildUsers
Expand Down
11 changes: 7 additions & 4 deletions src/GrillBot.App/Actions/Api/V1/Guild/UpdateGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ public override async Task<ApiResult> ProcessAsync()
{
var id = (ulong)Parameters[0]!;
var parameters = (UpdateGuildParams)Parameters[1]!;

var guild = await DiscordClient.GetGuildAsync(id);
if (guild == null)
throw new NotFoundException(Texts["GuildModule/GuildDetail/NotFound", ApiContext.Language]);
var guild = await DiscordClient.GetGuildAsync(id)
?? throw new NotFoundException(Texts["GuildModule/GuildDetail/NotFound", ApiContext.Language]);

await using var repository = DatabaseBuilder.CreateRepository();

Expand Down Expand Up @@ -59,6 +57,11 @@ public override async Task<ApiResult> ProcessAsync()
else
dbGuild.BotRoomChannelId = parameters.BotRoomChannelId;

if (!string.IsNullOrEmpty(parameters.AssociationRoleId) && guild.GetRole(parameters.AssociationRoleId.ToUlong()) == null)
ThrowValidationException("AssociationRoleNotFound", parameters.AssociationRoleId, nameof(parameters.AssociationRoleId));
else
dbGuild.AssociationRoleId = parameters.AssociationRoleId;

dbGuild.EmoteSuggestionsFrom = parameters.EmoteSuggestionsValidity?.From;
dbGuild.EmoteSuggestionsTo = parameters.EmoteSuggestionsValidity?.To;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public async Task ProcessAsync()
dbGuild.VoteChannelId = null;
if (CanResetRoleId(dbGuild.MuteRoleId, guild))
dbGuild.MuteRoleId = null;
if (CanResetRoleId(dbGuild.AssociationRoleId, guild))
dbGuild.AssociationRoleId = null;
}

await repository.CommitAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using GrillBot.Common.Managers.Events.Contracts;
using GrillBot.Core.RabbitMQ.Publisher;
using GrillBot.Core.Services.AuditLog.Enums;
using GrillBot.Database.Enums;
using GrillBot.Database.Services.Repository;

namespace GrillBot.App.Handlers.RoleDeleted;

Expand All @@ -27,6 +29,7 @@ public async Task ProcessAsync(IRole role)

var log = new List<string>();
ModifyMutedRoleId(role, guild, log);
ModifyAssociationRoleId(role, guild, log);

if (log.Count == 0)
return;
Expand All @@ -44,6 +47,15 @@ private static void ModifyMutedRoleId(IRole role, Database.Entity.Guild guild, L
guild.MuteRoleId = null;
}

private static void ModifyAssociationRoleId(IRole role, Database.Entity.Guild guild, List<string> log)
{
if (string.IsNullOrEmpty(guild.AssociationRoleId) || guild.AssociationRoleId != role.Id.ToString())
return;

log.Add($"Removed AssociationRoleId value and cleared user flags. OldValue: {guild.AssociationRoleId}");
guild.AssociationRoleId = null;
}

private async Task WriteToAuditLogAsync(IGuild guild, List<string> log)
{
var logRequest = new LogRequest(LogType.Info, DateTime.UtcNow, guild.Id.ToString())
Expand Down
1 change: 1 addition & 0 deletions src/GrillBot.Data/Models/API/Guilds/GuildDetail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,5 @@ public class GuildDetail : Guild
public Dictionary<UserStatus, int> UserStatusReport { get; set; } = new();
public Dictionary<ClientType, int> ClientTypeReport { get; set; } = new();
public GuildDatabaseReport DatabaseReport { get; set; } = null!;
public Role? AssociationRole { get; set; }
}
13 changes: 12 additions & 1 deletion src/GrillBot.Data/Models/API/Guilds/UpdateGuildParams.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using GrillBot.Core.Extensions;
using GrillBot.Core.Infrastructure;
using GrillBot.Core.Validation;
Expand All @@ -10,22 +11,31 @@ namespace GrillBot.Data.Models.API.Guilds;
public class UpdateGuildParams : IDictionaryObject
{
[DiscordId]
[StringLength(30)]
public string? MuteRoleId { get; set; }

[DiscordId]
[StringLength(30)]
public string? AdminChannelId { get; set; }

[DiscordId]
[StringLength(30)]
public string? EmoteSuggestionChannelId { get; set; }

[DiscordId]
[StringLength(30)]
public string? VoteChannelId { get; set; }

[DiscordId]
[StringLength(30)]
public string? BotRoomChannelId { get; set; }

public RangeParams<DateTime>? EmoteSuggestionsValidity { get; set; }

[DiscordId]
[StringLength(30)]
public string? AssociationRoleId { get; set; }

public Dictionary<string, string?> ToDictionary()
{
var result = new Dictionary<string, string?>
Expand All @@ -34,7 +44,8 @@ public class UpdateGuildParams : IDictionaryObject
{ nameof(AdminChannelId), AdminChannelId },
{ nameof(EmoteSuggestionChannelId), EmoteSuggestionChannelId },
{ nameof(VoteChannelId), VoteChannelId },
{ nameof(BotRoomChannelId), BotRoomChannelId }
{ nameof(BotRoomChannelId), BotRoomChannelId },
{ nameof(AssociationRoleId), AssociationRoleId }
};

result.MergeDictionaryObjects(EmoteSuggestionsValidity, nameof(EmoteSuggestionsValidity));
Expand Down
2 changes: 1 addition & 1 deletion src/GrillBot.Data/Models/API/Users/GetUserListParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public class GetUserListParams : IQueryableModel<Database.Entity.User>, IDiction
}

if (Flags != null && Flags > 0)
query = query.Where(o => (o.Flags & Flags) == Flags);
query = query.Where(o => (o.Flags & Flags) != 0);

if (HaveBirthday)
query = query.Where(o => o.Birthday != null);
Expand Down
3 changes: 2 additions & 1 deletion src/GrillBot.Data/Resources/Localization/messages.cs.json
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@
"MuteRoleNotFound": "Nepodařilo se dohledat roli, která reprezentuje umlčení uživatele při unverify.",
"EmoteSuggestionChannelNotFound": "Nepodařilo se dohledat kanál pro návrhy emotů.",
"VoteChannelNotFound": "Nepodařilo se dohledat kanál pro veřejná hlasování.",
"BotRoomChannelNotFound": "Nepodařilo se dohledat kanál pro boty."
"BotRoomChannelNotFound": "Nepodařilo se dohledat kanál pro boty.",
"AssociationRoleNotFound": "Nepodařilo se dohledat roli spolku."
}
},
"MathModule": {
Expand Down
3 changes: 2 additions & 1 deletion src/GrillBot.Data/Resources/Localization/messages.en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@
"MuteRoleNotFound": "Failed to find the role that represents silencing the user on unverify.",
"EmoteSuggestionChannelNotFound": "Failed to find channel for emote suggestions.",
"VoteChannelNotFound": "Could not find public polling channel.",
"BotRoomChannelNotFound": "Could not find channel for bots."
"BotRoomChannelNotFound": "Could not find channel for bots.",
"AssociationRoleNotFound": "Could not found association role."
}
},
"MathModule": {
Expand Down
3 changes: 3 additions & 0 deletions src/GrillBot.Database/Entity/Guild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public class Guild
[StringLength(30)]
public string? BotRoomChannelId { get; set; }

[StringLength(30)]
public string? AssociationRoleId { get; set; }

public ISet<GuildUser> Users { get; set; }
public ISet<Invite> Invites { get; set; }
public ISet<GuildChannel> Channels { get; set; }
Expand Down
Loading

0 comments on commit 0bd959d

Please sign in to comment.