2024-10-14 14:56:40 +02:00
|
|
|
// Copyright (C) 2021-present sam (starshines.gay)
|
|
|
|
|
//
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
2024-08-19 16:12:28 +02:00
|
|
|
using Catalogger.Backend.Cache.InMemoryCache;
|
2024-08-16 22:28:05 +02:00
|
|
|
using Catalogger.Backend.Database;
|
|
|
|
|
using Catalogger.Backend.Database.Queries;
|
|
|
|
|
using Catalogger.Backend.Extensions;
|
|
|
|
|
using Catalogger.Backend.Services;
|
|
|
|
|
using Remora.Discord.API.Abstractions.Gateway.Events;
|
|
|
|
|
using Remora.Discord.API.Abstractions.Objects;
|
|
|
|
|
using Remora.Discord.Extensions.Embeds;
|
|
|
|
|
using Remora.Discord.Gateway.Responders;
|
|
|
|
|
using Remora.Results;
|
|
|
|
|
|
|
|
|
|
namespace Catalogger.Backend.Bot.Responders.Channels;
|
|
|
|
|
|
|
|
|
|
public class ChannelCreateResponder(
|
|
|
|
|
DatabaseContext db,
|
2024-08-19 16:12:28 +02:00
|
|
|
RoleCache roleCache,
|
|
|
|
|
ChannelCache channelCache,
|
2024-08-20 18:18:17 +02:00
|
|
|
UserCache userCache,
|
2024-10-09 17:35:11 +02:00
|
|
|
WebhookExecutorService webhookExecutor
|
|
|
|
|
) : IResponder<IChannelCreate>
|
2024-08-16 22:28:05 +02:00
|
|
|
{
|
|
|
|
|
public async Task<Result> RespondAsync(IChannelCreate ch, CancellationToken ct = default)
|
|
|
|
|
{
|
2024-10-09 17:35:11 +02:00
|
|
|
if (!ch.GuildID.IsDefined())
|
|
|
|
|
return Result.Success;
|
2024-08-16 22:28:05 +02:00
|
|
|
channelCache.Set(ch);
|
|
|
|
|
|
|
|
|
|
var builder = new EmbedBuilder()
|
2024-10-09 17:35:11 +02:00
|
|
|
.WithTitle(
|
|
|
|
|
ch.Type switch
|
|
|
|
|
{
|
|
|
|
|
ChannelType.GuildVoice => "Voice channel created",
|
|
|
|
|
ChannelType.GuildCategory => "Category channel created",
|
|
|
|
|
ChannelType.GuildAnnouncement or ChannelType.GuildText =>
|
|
|
|
|
"Text channel created",
|
|
|
|
|
_ => "Channel created",
|
|
|
|
|
}
|
|
|
|
|
)
|
2024-08-16 22:28:05 +02:00
|
|
|
.WithColour(DiscordUtils.Green)
|
|
|
|
|
.WithFooter($"ID: {ch.ID}");
|
|
|
|
|
|
|
|
|
|
if (ch.ParentID.IsDefined(out var parentId))
|
|
|
|
|
{
|
2024-10-09 17:35:11 +02:00
|
|
|
builder.WithDescription(
|
|
|
|
|
channelCache.TryGet(parentId.Value, out var parentChannel)
|
|
|
|
|
? $"**Name:** {ch.Name}\n**Category:** {parentChannel.Name}"
|
|
|
|
|
: $"**Name:** {ch.Name}"
|
|
|
|
|
);
|
2024-08-16 22:28:05 +02:00
|
|
|
}
|
2024-10-09 17:35:11 +02:00
|
|
|
else
|
|
|
|
|
builder.WithDescription($"**Name:** {ch.Name}");
|
2024-08-16 22:28:05 +02:00
|
|
|
|
|
|
|
|
foreach (var overwrite in ch.PermissionOverwrites.OrDefault() ?? [])
|
|
|
|
|
{
|
|
|
|
|
if (overwrite.Type == PermissionOverwriteType.Role)
|
|
|
|
|
{
|
2024-10-09 17:35:11 +02:00
|
|
|
var roleName = roleCache.TryGet(overwrite.ID, out var role)
|
|
|
|
|
? role.Name
|
|
|
|
|
: $"role {overwrite.ID}";
|
2024-08-16 22:28:05 +02:00
|
|
|
var embedFieldValue = "";
|
|
|
|
|
if (overwrite.Allow.GetPermissions().Count != 0)
|
|
|
|
|
embedFieldValue += $"\u2705 {overwrite.Allow.ToPrettyString()}";
|
|
|
|
|
if (overwrite.Deny.GetPermissions().Count != 0)
|
|
|
|
|
embedFieldValue += $"\n\n\u274c {overwrite.Deny.ToPrettyString()}";
|
|
|
|
|
|
|
|
|
|
builder.AddField($"Override for {roleName}", embedFieldValue.Trim());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-08-20 18:18:17 +02:00
|
|
|
var user = await userCache.GetUserAsync(overwrite.ID);
|
2024-08-16 22:28:05 +02:00
|
|
|
var embedFieldValue = "";
|
|
|
|
|
if (overwrite.Allow.GetPermissions().Count != 0)
|
|
|
|
|
embedFieldValue += $"\u2705 {overwrite.Allow.ToPrettyString()}";
|
|
|
|
|
if (overwrite.Deny.GetPermissions().Count != 0)
|
|
|
|
|
embedFieldValue += $"\n\n\u274c {overwrite.Deny.ToPrettyString()}";
|
|
|
|
|
|
2024-10-09 17:35:11 +02:00
|
|
|
builder.AddField(
|
|
|
|
|
$"Override for {user?.Tag() ?? $"user {overwrite.ID}"}",
|
|
|
|
|
embedFieldValue.Trim()
|
|
|
|
|
);
|
2024-08-16 22:28:05 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var guildConfig = await db.GetGuildAsync(ch.GuildID.Value, ct);
|
2024-10-09 17:35:11 +02:00
|
|
|
webhookExecutor.QueueLog(
|
|
|
|
|
guildConfig,
|
|
|
|
|
LogChannelType.ChannelCreate,
|
|
|
|
|
builder.Build().GetOrThrow()
|
|
|
|
|
);
|
2024-08-16 22:28:05 +02:00
|
|
|
return Result.Success;
|
|
|
|
|
}
|
2024-10-09 17:35:11 +02:00
|
|
|
}
|