mirror of
https://github.com/barelyprofessional/KfChatDotNet.git
synced 2026-05-02 04:22:04 -04:00
Added support for channel creation / deletion and joining / leaving stages for Discord
This commit is contained in:
@@ -430,9 +430,31 @@ public class ChatBot
|
||||
_discord.OnInvalidCredentials += DiscordOnInvalidCredentials;
|
||||
_discord.OnMessageReceived += DiscordOnMessageReceived;
|
||||
_discord.OnPresenceUpdated += DiscordOnPresenceUpdated;
|
||||
_discord.OnChannelCreated += DiscordOnChannelCreated;
|
||||
_discord.OnChannelDeleted += DiscordOnChannelDeleted;
|
||||
_discord.StartWsClient().Wait(_cancellationToken);
|
||||
}
|
||||
|
||||
private void DiscordOnChannelDeleted(object sender, DiscordChannelDeletionModel channel)
|
||||
{
|
||||
_logger.Info($"Received channel deletion event of type {channel.Type} with name {channel.Name}");
|
||||
if (channel.Type != DiscordChannelType.GuildText && channel.Type != DiscordChannelType.GuildVoice &&
|
||||
channel.Type != DiscordChannelType.GuildStageVoice) return;
|
||||
var discordIcon = Helpers.GetValue(BuiltIn.Keys.DiscordIcon).Result;
|
||||
var channelName = channel.Name ?? "Unknown name";
|
||||
SendChatMessage($"[img]{discordIcon.Value}[/img] Discord {channel.Type.Humanize()} channel {channelName} was deleted", true);
|
||||
}
|
||||
|
||||
private void DiscordOnChannelCreated(object sender, DiscordChannelCreationModel channel)
|
||||
{
|
||||
_logger.Info($"Received channel creation event of type {channel.Type} with name {channel.Name}");
|
||||
if (channel.Type != DiscordChannelType.GuildText && channel.Type != DiscordChannelType.GuildVoice &&
|
||||
channel.Type != DiscordChannelType.GuildStageVoice) return;
|
||||
var discordIcon = Helpers.GetValue(BuiltIn.Keys.DiscordIcon).Result;
|
||||
var channelName = channel.Name ?? "Unknown name";
|
||||
SendChatMessage($"[img]{discordIcon.Value}[/img] New Discord {channel.Type.Humanize()} channel created: {channelName}", true);
|
||||
}
|
||||
|
||||
private void BuildTwitchChat()
|
||||
{
|
||||
var settings = Helpers.GetMultipleValues([BuiltIn.Keys.TwitchBossmanJackUsername, BuiltIn.Keys.Proxy]).Result;
|
||||
@@ -485,6 +507,19 @@ public class ChatBot
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (message.Type == DiscordMessageType.StageStart)
|
||||
{
|
||||
SendChatMessage($"[img]{settings[BuiltIn.Keys.DiscordIcon].Value}[/img] BossmanJack just started a stage called {message.Content} 🚨🚨",
|
||||
true);
|
||||
return;
|
||||
}
|
||||
if (message.Type == DiscordMessageType.StageEnd)
|
||||
{
|
||||
SendChatMessage($"[img]{settings[BuiltIn.Keys.DiscordIcon].Value}[/img] BossmanJack just ended a stage called {message.Content} :lossmanjack:",
|
||||
true);
|
||||
return;
|
||||
}
|
||||
|
||||
var result = $"[img]{settings[BuiltIn.Keys.DiscordIcon].Value}[/img] BossmanJack: {message.Content}";
|
||||
foreach (var attachment in message.Attachments ?? [])
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Net;
|
||||
using System.ComponentModel;
|
||||
using System.Net;
|
||||
using System.Net.WebSockets;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
@@ -23,10 +24,15 @@ public class DiscordService : IDisposable
|
||||
public delegate void PresenceUpdateEventHandler(object sender, DiscordPresenceUpdateModel presence);
|
||||
public delegate void WsDisconnectionEventHandler(object sender, DisconnectionInfo e);
|
||||
public delegate void InvalidCredentialsEventHandler(object sender, DiscordPacketReadModel packet);
|
||||
public delegate void ChannelCreatedEventHandler(object sender, DiscordChannelCreationModel channel);
|
||||
public delegate void ChannelDeletedEventHandler(object sender, DiscordChannelDeletionModel channel);
|
||||
|
||||
public event MessageReceivedEventHandler OnMessageReceived;
|
||||
public event PresenceUpdateEventHandler OnPresenceUpdated;
|
||||
public event WsDisconnectionEventHandler OnWsDisconnection;
|
||||
public event InvalidCredentialsEventHandler OnInvalidCredentials;
|
||||
public event ChannelCreatedEventHandler OnChannelCreated;
|
||||
public event ChannelDeletedEventHandler OnChannelDeleted;
|
||||
|
||||
private readonly CancellationToken _cancellationToken = CancellationToken.None;
|
||||
private readonly CancellationTokenSource _pingCts = new();
|
||||
@@ -186,6 +192,16 @@ public class DiscordService : IDisposable
|
||||
OnMessageReceived?.Invoke(this,
|
||||
packet.Data.Deserialize<DiscordMessageModel>() ?? throw new InvalidOperationException());
|
||||
return;
|
||||
case "CHANNEL_CREATE":
|
||||
OnChannelCreated?.Invoke(this,
|
||||
packet.Data.Deserialize<DiscordChannelCreationModel>() ??
|
||||
throw new InvalidOperationException());
|
||||
return;
|
||||
case "CHANNEL_DELETE":
|
||||
OnChannelDeleted?.Invoke(this,
|
||||
packet.Data.Deserialize<DiscordChannelDeletionModel>() ??
|
||||
throw new InvalidOperationException());
|
||||
return;
|
||||
default:
|
||||
_logger.Debug($"{packet.DispatchEvent} was unhandled. JSON follows");
|
||||
_logger.Debug(message.Text);
|
||||
@@ -248,10 +264,51 @@ public class DiscordUserModel
|
||||
|
||||
public class DiscordMessageModel
|
||||
{
|
||||
[JsonPropertyName("type")]
|
||||
public required DiscordMessageType Type { get; set; }
|
||||
[JsonPropertyName("content")]
|
||||
public string? Content { get; set; }
|
||||
[JsonPropertyName("author")]
|
||||
public required DiscordUserModel Author { get; set; }
|
||||
[JsonPropertyName("attachments")]
|
||||
public JsonElement[]? Attachments { get; set; }
|
||||
}
|
||||
|
||||
public class DiscordChannelCreationModel
|
||||
{
|
||||
[JsonPropertyName("type")]
|
||||
public required DiscordChannelType Type { get; set; }
|
||||
[JsonPropertyName("name")]
|
||||
public string? Name { get; set; }
|
||||
[JsonPropertyName("guild_id")]
|
||||
public required string GuildId { get; set; }
|
||||
}
|
||||
|
||||
public class DiscordChannelDeletionModel
|
||||
{
|
||||
[JsonPropertyName("type")]
|
||||
public required DiscordChannelType Type { get; set; }
|
||||
[JsonPropertyName("name")]
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
// https://discord.com/developers/docs/resources/channel#channel-object-channel-types
|
||||
// Ignored the ones nobody cares about
|
||||
public enum DiscordChannelType
|
||||
{
|
||||
[Description("Text")]
|
||||
GuildText = 0,
|
||||
[Description("Voice")]
|
||||
GuildVoice = 2,
|
||||
[Description("Stage")]
|
||||
GuildStageVoice = 13
|
||||
}
|
||||
|
||||
public enum DiscordMessageType
|
||||
{
|
||||
Default = 0,
|
||||
[Description("Stage start")]
|
||||
StageStart = 27,
|
||||
[Description("Stage end")]
|
||||
StageEnd = 28
|
||||
}
|
||||
Reference in New Issue
Block a user