about summary refs log tree commit diff
path: root/Utilities/LibMatrix.Utilities.Bot/Commands/PingCommand.cs
blob: ff41b70f8fced417206e6d4592471f84dc25e8d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using LibMatrix.EventTypes.Spec;
using LibMatrix.Utilities.Bot.Interfaces;

namespace LibMatrix.Utilities.Bot.Commands;

public class PingCommand : ICommand {
    public string Name { get; } = "ping";
    public string[]? Aliases { get; } = [];
    public string Description { get; } = "Pong!";
    public bool Unlisted { get; }

    public async Task Invoke(CommandContext ctx) {
        var latency = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - ctx.MessageEvent.OriginServerTs;
        await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent(body: $"Pong! ({latency} ms)") {
            AdditionalData = new() {
                // maubot ping compatibility
                ["pong"] = new {
                    ms = latency,
                    from = ctx.Homeserver.ServerName,
                    ping = ctx.MessageEvent.EventId
                },
            },
            RelatesTo = new() {
                RelationType = "xyz.maubot.pong",
                EventId = ctx.MessageEvent.EventId,
                AdditionalData = new() {
                    ["ms"] = latency!,
                    ["from"] = ctx.Homeserver.ServerName
                }
            }
        });
    }
}