2023-07-09 16:32:14 +03:00
|
|
|
using System.ComponentModel;
|
2023-07-18 15:25:02 +03:00
|
|
|
using Boyfriend.Data;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Boyfriend.Services;
|
2023-07-18 15:25:02 +03:00
|
|
|
using JetBrains.Annotations;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Remora.Commands.Attributes;
|
|
|
|
using Remora.Commands.Groups;
|
2023-07-20 23:41:02 +03:00
|
|
|
using Remora.Discord.API.Abstractions.Objects;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Remora.Discord.API.Abstractions.Rest;
|
2023-07-18 17:18:35 +03:00
|
|
|
using Remora.Discord.Commands.Attributes;
|
|
|
|
using Remora.Discord.Commands.Conditions;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Remora.Discord.Commands.Contexts;
|
|
|
|
using Remora.Discord.Commands.Feedback.Services;
|
|
|
|
using Remora.Discord.Extensions.Embeds;
|
|
|
|
using Remora.Discord.Gateway;
|
2023-07-20 23:41:02 +03:00
|
|
|
using Remora.Rest.Core;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Remora.Results;
|
|
|
|
|
|
|
|
namespace Boyfriend.Commands;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Handles the command to get the time taken for the gateway to respond to the last heartbeat: /ping
|
|
|
|
/// </summary>
|
2023-07-18 15:25:02 +03:00
|
|
|
[UsedImplicitly]
|
2023-08-02 23:51:16 +03:00
|
|
|
public class PingCommandGroup : CommandGroup
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
private readonly IDiscordRestChannelAPI _channelApi;
|
2023-08-02 23:51:16 +03:00
|
|
|
private readonly DiscordGatewayClient _client;
|
|
|
|
private readonly ICommandContext _context;
|
|
|
|
private readonly FeedbackService _feedback;
|
|
|
|
private readonly GuildDataService _guildData;
|
|
|
|
private readonly IDiscordRestUserAPI _userApi;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
public PingCommandGroup(
|
2023-08-02 23:51:16 +03:00
|
|
|
IDiscordRestChannelAPI channelApi, ICommandContext context, DiscordGatewayClient client,
|
|
|
|
GuildDataService guildData, FeedbackService feedback, IDiscordRestUserAPI userApi)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
_channelApi = channelApi;
|
|
|
|
_context = context;
|
|
|
|
_client = client;
|
2023-08-02 23:51:16 +03:00
|
|
|
_guildData = guildData;
|
|
|
|
_feedback = feedback;
|
2023-07-09 16:32:14 +03:00
|
|
|
_userApi = userApi;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// A slash command that shows time taken for the gateway to respond to the last heartbeat.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>
|
|
|
|
/// A feedback sending result which may or may not have succeeded.
|
|
|
|
/// </returns>
|
|
|
|
[Command("ping", "пинг")]
|
|
|
|
[Description("Get bot latency")]
|
2023-07-18 17:18:35 +03:00
|
|
|
[DiscordDefaultDMPermission(false)]
|
|
|
|
[RequireContext(ChannelContext.Guild)]
|
2023-07-18 15:25:02 +03:00
|
|
|
[UsedImplicitly]
|
2023-08-02 23:51:16 +03:00
|
|
|
public async Task<Result> ExecutePingAsync()
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out _))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-28 19:58:55 +03:00
|
|
|
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context");
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
var currentUserResult = await _userApi.GetCurrentUserAsync(CancellationToken);
|
|
|
|
if (!currentUserResult.IsDefined(out var currentUser))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return Result.FromError(currentUserResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
var cfg = await _guildData.GetSettings(guildId, CancellationToken);
|
2023-07-18 15:25:02 +03:00
|
|
|
Messages.Culture = GuildSettings.Language.Get(cfg);
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-07-24 14:57:41 +03:00
|
|
|
return await SendLatencyAsync(channelId, currentUser, CancellationToken);
|
2023-07-20 23:41:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<Result> SendLatencyAsync(
|
2023-08-02 23:51:16 +03:00
|
|
|
Snowflake channelId, IUser currentUser, CancellationToken ct = default)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
var latency = _client.Latency.TotalMilliseconds;
|
2023-08-02 23:51:16 +03:00
|
|
|
if (latency is 0)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
// No heartbeat has occurred, estimate latency from local time and "Boyfriend is thinking..." message
|
|
|
|
var lastMessageResult = await _channelApi.GetChannelMessagesAsync(
|
2023-07-20 23:41:02 +03:00
|
|
|
channelId, limit: 1, ct: ct);
|
2023-07-09 16:32:14 +03:00
|
|
|
if (!lastMessageResult.IsDefined(out var lastMessage))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return Result.FromError(lastMessageResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
|
|
|
|
2023-07-09 16:32:14 +03:00
|
|
|
latency = DateTimeOffset.UtcNow.Subtract(lastMessage.Single().Timestamp).TotalMilliseconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
var embed = new EmbedBuilder().WithSmallTitle(currentUser.GetTag(), currentUser)
|
|
|
|
.WithTitle($"Beep{Random.Shared.Next(1, 4)}".Localized())
|
|
|
|
.WithDescription($"{latency:F0}{Messages.Milliseconds}")
|
|
|
|
.WithColour(latency < 250 ? ColorsList.Green : latency < 500 ? ColorsList.Yellow : ColorsList.Red)
|
|
|
|
.WithCurrentTimestamp()
|
|
|
|
.Build();
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
return await _feedback.SendContextualEmbedResultAsync(embed, ct);
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|
|
|
|
}
|