2023-07-09 16:32:14 +03:00
|
|
|
using System.ComponentModel;
|
2023-07-18 15:25:02 +03:00
|
|
|
using JetBrains.Annotations;
|
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-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;
|
|
|
|
|
2023-09-30 16:58:32 +03:00
|
|
|
namespace Octobot.Commands;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
/// <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;
|
2023-11-04 21:28:22 +03:00
|
|
|
private readonly IFeedbackService _feedback;
|
2023-08-02 23:51:16 +03:00
|
|
|
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,
|
2023-11-04 21:28:22 +03:00
|
|
|
GuildDataService guildData, IFeedbackService feedback, IDiscordRestUserAPI userApi)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
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
|
|
|
|
2023-10-04 18:21:10 +03:00
|
|
|
var botResult = await _userApi.GetCurrentUserAsync(CancellationToken);
|
|
|
|
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-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-10-04 18:21:10 +03:00
|
|
|
return await SendLatencyAsync(channelId, bot, CancellationToken);
|
2023-07-20 23:41:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<Result> SendLatencyAsync(
|
2023-10-04 18:21:10 +03:00
|
|
|
Snowflake channelId, IUser bot, CancellationToken ct = default)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
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-09-30 16:58:32 +03:00
|
|
|
// No heartbeat has occurred, estimate latency from local time and "Octobot is thinking..." message
|
2023-07-09 16:32:14 +03:00
|
|
|
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
|
|
|
{
|
2024-03-20 21:08:16 +03:00
|
|
|
return ResultExtensions.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;
|
|
|
|
}
|
|
|
|
|
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{Random.Shared.Next(1, 4)}".Localized())
|
2023-07-09 16:32:14 +03:00
|
|
|
.WithDescription($"{latency:F0}{Messages.Milliseconds}")
|
|
|
|
.WithColour(latency < 250 ? ColorsList.Green : latency < 500 ? ColorsList.Yellow : ColorsList.Red)
|
|
|
|
.WithCurrentTimestamp()
|
|
|
|
.Build();
|
|
|
|
|
2023-12-17 18:44:18 +03:00
|
|
|
return await _feedback.SendContextualEmbedResultAsync(embed, ct: ct);
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|
|
|
|
}
|