2023-07-18 15:25:02 +03:00
|
|
|
using JetBrains.Annotations;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2023-09-30 16:58:32 +03:00
|
|
|
using Octobot.Data;
|
2023-10-12 18:37:25 +03:00
|
|
|
using Octobot.Extensions;
|
2023-09-30 16:58:32 +03:00
|
|
|
using Octobot.Services;
|
2023-07-18 15:25:02 +03:00
|
|
|
using Remora.Discord.API.Abstractions.Gateway.Events;
|
2023-11-04 21:33:37 +03:00
|
|
|
using Remora.Discord.API.Abstractions.Objects;
|
2023-07-18 15:25:02 +03:00
|
|
|
using Remora.Discord.API.Abstractions.Rest;
|
|
|
|
using Remora.Discord.API.Gateway.Events;
|
2023-12-17 21:35:09 +03:00
|
|
|
using Remora.Discord.API.Objects;
|
2023-07-18 15:25:02 +03:00
|
|
|
using Remora.Discord.Extensions.Embeds;
|
|
|
|
using Remora.Discord.Gateway.Responders;
|
|
|
|
using Remora.Results;
|
|
|
|
|
2023-09-30 16:58:32 +03:00
|
|
|
namespace Octobot.Responders;
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Handles sending a <see cref="Ready" /> message to a guild that has just initialized if that guild
|
|
|
|
/// has <see cref="GuildSettings.ReceiveStartupMessages" /> enabled
|
|
|
|
/// </summary>
|
|
|
|
[UsedImplicitly]
|
2023-08-02 23:51:16 +03:00
|
|
|
public class GuildLoadedResponder : IResponder<IGuildCreate>
|
|
|
|
{
|
|
|
|
private readonly IDiscordRestChannelAPI _channelApi;
|
|
|
|
private readonly GuildDataService _guildData;
|
2023-07-18 15:25:02 +03:00
|
|
|
private readonly ILogger<GuildLoadedResponder> _logger;
|
2023-08-02 23:51:16 +03:00
|
|
|
private readonly IDiscordRestUserAPI _userApi;
|
2023-12-20 19:33:52 +03:00
|
|
|
private readonly Utility _utility;
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
public GuildLoadedResponder(
|
2023-08-02 23:51:16 +03:00
|
|
|
IDiscordRestChannelAPI channelApi, GuildDataService guildData, ILogger<GuildLoadedResponder> logger,
|
2023-12-20 19:33:52 +03:00
|
|
|
IDiscordRestUserAPI userApi, Utility utility)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
_channelApi = channelApi;
|
2023-08-02 23:51:16 +03:00
|
|
|
_guildData = guildData;
|
2023-07-18 15:25:02 +03:00
|
|
|
_logger = logger;
|
|
|
|
_userApi = userApi;
|
2023-11-04 21:33:37 +03:00
|
|
|
_utility = utility;
|
2023-07-18 15:25:02 +03:00
|
|
|
}
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
public async Task<Result> RespondAsync(IGuildCreate gatewayEvent, CancellationToken ct = default)
|
|
|
|
{
|
|
|
|
if (!gatewayEvent.Guild.IsT0) // Guild is not IAvailableGuild
|
|
|
|
{
|
2024-03-21 18:55:34 +03:00
|
|
|
return Result.Success;
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
var guild = gatewayEvent.Guild.AsT0;
|
|
|
|
|
2023-08-04 16:52:54 +03:00
|
|
|
var data = await _guildData.GetData(guild.ID, ct);
|
|
|
|
var cfg = data.Settings;
|
|
|
|
foreach (var member in guild.Members.Where(m => m.User.HasValue))
|
|
|
|
{
|
|
|
|
data.GetOrCreateMemberData(member.User.Value.ID);
|
|
|
|
}
|
|
|
|
|
2023-10-26 18:14:27 +03:00
|
|
|
var botResult = await _userApi.GetCurrentUserAsync(ct);
|
|
|
|
if (!botResult.IsDefined(out var bot))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2024-03-20 21:08:16 +03:00
|
|
|
return ResultExtensions.FromError(botResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
|
|
|
|
2023-10-26 18:14:27 +03:00
|
|
|
if (data.DataLoadFailed)
|
|
|
|
{
|
2023-11-04 21:33:37 +03:00
|
|
|
return await SendDataLoadFailed(guild, data, bot, ct);
|
2023-10-26 18:14:27 +03:00
|
|
|
}
|
|
|
|
|
2023-11-06 21:39:26 +03:00
|
|
|
var ownerResult = await _userApi.GetUserAsync(guild.OwnerID, ct);
|
|
|
|
if (!ownerResult.IsDefined(out var owner))
|
|
|
|
{
|
2024-03-20 21:08:16 +03:00
|
|
|
return ResultExtensions.FromError(ownerResult);
|
2023-11-06 21:39:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_logger.LogInformation("Loaded guild \"{Name}\" ({ID}) owned by {Owner} ({OwnerID}) with {MemberCount} members",
|
|
|
|
guild.Name, guild.ID, owner.GetTag(), owner.ID, guild.MemberCount);
|
2023-10-26 18:14:27 +03:00
|
|
|
|
2024-03-21 19:31:17 +03:00
|
|
|
if (GuildSettings.PrivateFeedbackChannel.Get(cfg).Empty()
|
|
|
|
|| !GuildSettings.ReceiveStartupMessages.Get(cfg))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2024-03-21 18:55:34 +03:00
|
|
|
return Result.Success;
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
Messages.Culture = GuildSettings.Language.Get(cfg);
|
|
|
|
var i = Random.Shared.Next(1, 4);
|
|
|
|
|
2023-10-04 18:21:10 +03:00
|
|
|
var embed = new EmbedBuilder().WithSmallTitle(bot.GetTag(), bot)
|
2024-03-20 14:22:54 +03:00
|
|
|
.WithTitle($"Generic{i}".Localized())
|
2023-07-18 15:25:02 +03:00
|
|
|
.WithDescription(Messages.Ready)
|
|
|
|
.WithCurrentTimestamp()
|
|
|
|
.WithColour(ColorsList.Blue)
|
|
|
|
.Build();
|
|
|
|
|
2023-12-17 19:47:52 +03:00
|
|
|
return await _channelApi.CreateMessageWithEmbedResultAsync(
|
|
|
|
GuildSettings.PrivateFeedbackChannel.Get(cfg), embedResult: embed, ct: ct);
|
2023-07-18 15:25:02 +03:00
|
|
|
}
|
2023-10-26 18:14:27 +03:00
|
|
|
|
2023-11-04 21:33:37 +03:00
|
|
|
private async Task<Result> SendDataLoadFailed(IGuild guild, GuildData data, IUser bot, CancellationToken ct)
|
2023-10-26 18:14:27 +03:00
|
|
|
{
|
2023-12-17 19:47:52 +03:00
|
|
|
var channelResult = await _utility.GetEmergencyFeedbackChannel(guild, data, ct);
|
|
|
|
if (!channelResult.IsDefined(out var channel))
|
|
|
|
{
|
2024-03-20 21:08:16 +03:00
|
|
|
return ResultExtensions.FromError(channelResult);
|
2023-12-17 19:47:52 +03:00
|
|
|
}
|
|
|
|
|
2023-11-04 21:33:37 +03:00
|
|
|
var errorEmbed = new EmbedBuilder()
|
|
|
|
.WithSmallTitle(Messages.DataLoadFailedTitle, bot)
|
|
|
|
.WithDescription(Messages.DataLoadFailedDescription)
|
|
|
|
.WithFooter(Messages.ContactDevelopers)
|
|
|
|
.WithColour(ColorsList.Red)
|
|
|
|
.Build();
|
|
|
|
|
2023-12-17 21:35:09 +03:00
|
|
|
var issuesButton = new ButtonComponent(
|
|
|
|
ButtonComponentStyle.Link,
|
2024-03-20 15:59:25 +03:00
|
|
|
BuildInfo.IsDirty
|
|
|
|
? Messages.ButtonDirty
|
|
|
|
: Messages.ButtonReportIssue,
|
2023-12-17 21:35:09 +03:00
|
|
|
new PartialEmoji(Name: "⚠️"),
|
2024-03-20 15:59:25 +03:00
|
|
|
URL: BuildInfo.IssuesUrl,
|
|
|
|
IsDisabled: BuildInfo.IsDirty
|
2023-12-17 21:35:09 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
return await _channelApi.CreateMessageWithEmbedResultAsync(channel, embedResult: errorEmbed,
|
|
|
|
components: new[] { new ActionRowComponent(new[] { issuesButton }) }, ct: ct);
|
2023-10-26 18:14:27 +03:00
|
|
|
}
|
2023-07-18 15:25:02 +03:00
|
|
|
}
|