feat: wip on sending welcome message
This commit is contained in:
@@ -36,6 +36,7 @@ internal sealed class DiscordEventConverter : JsonConverter<DiscordEvent>
|
||||
return type switch
|
||||
{
|
||||
DiscordEventTypes.Ready => JsonSerializer.Deserialize<ReadyDiscordEvent>(root.GetRawText(), options),
|
||||
DiscordEventTypes.MessageCreate => JsonSerializer.Deserialize<MessageCreateDiscordEvent>(root.GetRawText(), options),
|
||||
_ => JsonSerializer.Deserialize<DispatchDiscordEvent>(root.GetRawText(), modifiedOptions),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ namespace StevesBot.Worker.Discord.Events;
|
||||
internal static class DiscordEventTypes
|
||||
{
|
||||
public const string Ready = "READY";
|
||||
public const string GuildMemberAdd = "GUILD_MEMBER_ADD";
|
||||
public const string MessageCreate = "MESSAGE_CREATE";
|
||||
|
||||
public static bool IsValidEvent(string eventName)
|
||||
{
|
||||
@@ -11,6 +13,8 @@ internal static class DiscordEventTypes
|
||||
return false;
|
||||
}
|
||||
|
||||
return eventName is Ready;
|
||||
return eventName is Ready or
|
||||
GuildMemberAdd or
|
||||
MessageCreate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace StevesBot.Worker.Discord.Events;
|
||||
|
||||
internal static class DiscordMessageTypes
|
||||
{
|
||||
public const int UserJoin = 7;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
namespace StevesBot.Worker.Discord.Events;
|
||||
|
||||
internal sealed record MessageCreateDiscordEvent : DispatchDiscordEvent
|
||||
{
|
||||
[JsonPropertyName("d")]
|
||||
public new MessageCreateData Data { get; init; } = new MessageCreateData();
|
||||
|
||||
public bool IsMessageType(int messageType)
|
||||
{
|
||||
return Data.Type == messageType;
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed record MessageCreateData
|
||||
{
|
||||
[JsonPropertyName("id")]
|
||||
public string Id { get; init; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("type")]
|
||||
public int Type { get; init; }
|
||||
|
||||
[JsonPropertyName("channel_id")]
|
||||
public string ChannelId { get; init; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("guild_id")]
|
||||
public string GuildId { get; init; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("author")]
|
||||
public DiscordUser Author { get; init; } = new DiscordUser();
|
||||
|
||||
}
|
||||
|
||||
internal sealed record DiscordUser
|
||||
{
|
||||
[JsonPropertyName("id")]
|
||||
public string Id { get; init; } = string.Empty;
|
||||
}
|
||||
@@ -20,17 +20,29 @@ internal class Worker : IHostedService
|
||||
// in delegate...don't try to resolve it from the service provider
|
||||
// TODO: Provide .On and .Off method overloads to allow
|
||||
// caller to not need to use discard for unused parameters
|
||||
_discordGatewayClient.On(DiscordEventTypes.Ready, static (discordEvent, sp) =>
|
||||
_discordGatewayClient.On(DiscordEventTypes.MessageCreate, static (discordEvent, sp) =>
|
||||
{
|
||||
var discordRestClient = sp.GetRequiredService<IDiscordRestClient>();
|
||||
var logger = sp.GetRequiredService<ILogger<DiscordGatewayClient>>();
|
||||
logger.LogInformation("Ready handler 1");
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
|
||||
_discordGatewayClient.On(DiscordEventTypes.Ready, static (discordEvent, sp) =>
|
||||
{
|
||||
var logger = sp.GetRequiredService<ILogger<DiscordGatewayClient>>();
|
||||
logger.LogInformation("Ready handler 2");
|
||||
// TODO: When a user join message is received,
|
||||
// we are going to reply to the message with
|
||||
// a welcome greeting
|
||||
// A user join message is type 8
|
||||
// Will need to use REST API to reply to the message
|
||||
|
||||
if (
|
||||
discordEvent is MessageCreateDiscordEvent mcde &&
|
||||
mcde.IsMessageType(DiscordMessageTypes.UserJoin)
|
||||
)
|
||||
{
|
||||
logger.LogInformation("Guild Id: {GuildId}", mcde.Data.GuildId);
|
||||
logger.LogInformation("Channel Id: {ChannelId}", mcde.Data.ChannelId);
|
||||
logger.LogInformation("Message Id: {MessageId}", mcde.Data.Id);
|
||||
logger.LogInformation("User Id: {UserId}", mcde.Data.Author.Id);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user