53 lines
No EOL
2.2 KiB
C#
53 lines
No EOL
2.2 KiB
C#
using System.Text.Json;
|
|
using Foxcord.Gateway.Events.Dispatch;
|
|
using Foxcord.Rest;
|
|
using Foxcord.Rest.Types;
|
|
|
|
namespace Foxcord.Gateway;
|
|
|
|
public partial class DiscordGatewayClient
|
|
{
|
|
private IDispatch ParseDispatchEvent(string rawType, JsonElement rawPayload)
|
|
{
|
|
switch (rawType)
|
|
{
|
|
case DispatchEventTypeName.Ready:
|
|
return rawPayload.Deserialize<ReadyEvent>(_jsonSerializerOptions)!;
|
|
case DispatchEventTypeName.GuildCreate:
|
|
return rawPayload.Deserialize<GuildCreateEvent>(_jsonSerializerOptions)!;
|
|
case DispatchEventTypeName.MessageCreate:
|
|
return rawPayload.Deserialize<MessageCreateEvent>(_jsonSerializerOptions)!;
|
|
default:
|
|
throw new ArgumentOutOfRangeException(nameof(rawType), $"Unknown dispatch event '{rawType}'");
|
|
}
|
|
}
|
|
|
|
private async Task HandleDispatch(IDispatch dispatch, CancellationToken ct = default)
|
|
{
|
|
switch (dispatch)
|
|
{
|
|
case ReadyEvent ready:
|
|
_logger.Debug("Received READY! API version: {Version}, user: {UserId}, shard: {Id}/{Total}",
|
|
ready.Version, ready.User.Id, ready.Shard.ShardId, ready.Shard.NumShards);
|
|
break;
|
|
case GuildCreateEvent guildCreate:
|
|
_logger.Debug("Received guild create for guild {Id} / {Name}", guildCreate.Id, guildCreate.Name);
|
|
break;
|
|
case MessageCreateEvent m:
|
|
_logger.Debug("Received message create from {User} in {Channel}. Content: {Content}", m.Author.Tag,
|
|
m.ChannelId, m.Content);
|
|
if (m.Content == "!ping")
|
|
{
|
|
var rest = new DiscordRestClient(_logger,
|
|
new DiscordRestClientOptions { Token = _token["Bot ".Length..] });
|
|
await rest.CreateMessageAsync(m.ChannelId,
|
|
new CreateMessageParams(Content: $"Pong! Latency: {Latency.TotalMilliseconds}ms"), ct);
|
|
}
|
|
|
|
break;
|
|
default:
|
|
_logger.Debug("Received dispatch event {DispatchType}", dispatch.GetType().Name);
|
|
break;
|
|
}
|
|
}
|
|
} |