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;
|
|
|
|
using Remora.Discord.API.Abstractions.Rest;
|
|
|
|
using Remora.Discord.API.Gateway.Events;
|
|
|
|
using Remora.Discord.Extensions.Embeds;
|
|
|
|
using Remora.Discord.Gateway.Responders;
|
2023-10-26 18:14:27 +03:00
|
|
|
using Remora.Rest.Core;
|
2023-07-18 15:25:02 +03:00
|
|
|
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-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
public GuildLoadedResponder(
|
2023-08-02 23:51:16 +03:00
|
|
|
IDiscordRestChannelAPI channelApi, GuildDataService guildData, ILogger<GuildLoadedResponder> logger,
|
|
|
|
IDiscordRestUserAPI userApi)
|
|
|
|
{
|
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-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
|
|
|
|
{
|
|
|
|
return Result.FromSuccess();
|
|
|
|
}
|
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
|
|
|
{
|
2023-10-26 18:14:27 +03:00
|
|
|
return Result.FromError(botResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
|
|
|
|
2023-10-26 18:14:27 +03:00
|
|
|
if (data.DataLoadFailed)
|
|
|
|
{
|
|
|
|
var errorEmbed = new EmbedBuilder()
|
|
|
|
.WithSmallTitle(Messages.DataLoadFailedTitle, bot)
|
|
|
|
.WithDescription(Messages.DataLoadFailedDescription)
|
|
|
|
.WithFooter(Messages.ContactDevelopers)
|
|
|
|
.WithColour(ColorsList.Red)
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
if (!errorEmbed.IsDefined(out var errorBuilt))
|
|
|
|
{
|
|
|
|
return Result.FromError(errorEmbed);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (Result)await _channelApi.CreateMessageAsync(
|
|
|
|
GetEmergencyFeedbackChannel(guild, data), embeds: new[] { errorBuilt }, ct: ct);
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.LogInformation("Loaded guild {ID} (\"{Name}\")", guild.ID, guild.Name);
|
|
|
|
|
|
|
|
if (!GuildSettings.ReceiveStartupMessages.Get(cfg))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
return Result.FromSuccess();
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-18 15:25:02 +03:00
|
|
|
|
2023-10-26 18:14:27 +03:00
|
|
|
if (GuildSettings.PrivateFeedbackChannel.Get(cfg).Empty())
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-10-26 18:14:27 +03:00
|
|
|
return Result.FromSuccess();
|
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)
|
2023-09-30 16:58:32 +03:00
|
|
|
.WithTitle($"Sound{i}".Localized())
|
2023-07-18 15:25:02 +03:00
|
|
|
.WithDescription(Messages.Ready)
|
|
|
|
.WithCurrentTimestamp()
|
|
|
|
.WithColour(ColorsList.Blue)
|
|
|
|
.Build();
|
2023-08-02 23:51:16 +03:00
|
|
|
if (!embed.IsDefined(out var built))
|
|
|
|
{
|
|
|
|
return Result.FromError(embed);
|
|
|
|
}
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
return (Result)await _channelApi.CreateMessageAsync(
|
|
|
|
GuildSettings.PrivateFeedbackChannel.Get(cfg), embeds: new[] { built }, ct: ct);
|
|
|
|
}
|
2023-10-26 18:14:27 +03:00
|
|
|
|
|
|
|
private static Snowflake GetEmergencyFeedbackChannel(IGuildCreate.IAvailableGuild guild, GuildData data)
|
|
|
|
{
|
|
|
|
var privateFeedback = GuildSettings.PrivateFeedbackChannel.Get(data.Settings);
|
|
|
|
if (!privateFeedback.Empty())
|
|
|
|
{
|
|
|
|
return privateFeedback;
|
|
|
|
}
|
|
|
|
|
|
|
|
var publicFeedback = GuildSettings.PublicFeedbackChannel.Get(data.Settings);
|
|
|
|
if (!publicFeedback.Empty())
|
|
|
|
{
|
|
|
|
return publicFeedback;
|
|
|
|
}
|
|
|
|
|
|
|
|
return guild.SystemChannelID.AsOptional().IsDefined(out var systemChannel)
|
|
|
|
? systemChannel
|
|
|
|
: guild.Channels[0].ID;
|
|
|
|
}
|
2023-07-18 15:25:02 +03:00
|
|
|
}
|