feat: Adds discord integration

This commit is contained in:
Bohicatv 2026-03-03 11:06:45 -08:00
parent 4836bff5eb
commit e6a783c517
6 changed files with 57 additions and 22 deletions

View file

@ -63,19 +63,12 @@ namespace Server.Engines.Chat
}
public bool AlwaysAvailable { get; set; }
public static List<Channel> Channels { get; } = new();
public bool Contains(ChatUser user) => m_Users.Contains(user);
public bool IsBanned(ChatUser user) => m_Banned.Contains(user);
public bool CanTalk(ChatUser user) => !m_VoiceRestricted || m_Voices.Contains(user) || m_Moderators.Contains(user);
public bool IsModerator(ChatUser user) => m_Moderators.Contains(user);
public bool IsVoiced(ChatUser user) => m_Voices.Contains(user);
public bool ValidatePassword(string password) => m_Password?.InsensitiveEquals(password) != false;
public bool ValidateModerator(ChatUser user)
@ -159,7 +152,7 @@ namespace Server.Engines.Chat
SendCommand(ChatCommand.RemoveUserFromChannel, user, user.Username);
ChatSystem.SendCommandTo(user.Mobile, ChatCommand.LeaveChannel);
if (m_Users.Count == 0 && !AlwaysAvailable)
if (!AlwaysAvailable && m_Users.Count == 0)
{
RemoveChannel(this);
}
@ -478,6 +471,9 @@ namespace Server.Engines.Chat
return null;
}
// currently, all chat is combined to a single webhook
// all static channels will be captured
// also captures player created channels
public static void Initialize()
{
AddStaticChannel("Newbie Help");

View file

@ -7,6 +7,7 @@ namespace Server.Engines.Chat
public static void Configure()
{
Enabled = ServerConfiguration.GetOrUpdateSetting("chat.enabled", false);
Discord.Configure();
}
public static void SendCommandTo(Mobile to, ChatCommand type, string param1 = null, string param2 = null)

View file

@ -12,9 +12,7 @@ namespace Server.Engines.Chat
}
public bool RequireModerator { get; }
public bool RequireConference { get; }
public OnChatAction Callback { get; }
}
}

View file

@ -9,9 +9,7 @@ namespace Server.Engines.Chat
m_Handlers = new ChatActionHandler[0x100];
Register(0x41, true, true, ChangeChannelPassword);
Register(0x58, false, false, LeaveChat);
Register(0x61, false, true, ChannelMessage);
Register(0x62, false, false, JoinChannel);
Register(0x63, false, false, JoinNewChannel);
@ -63,6 +61,8 @@ namespace Server.Engines.Chat
if (channel.CanTalk(from))
{
channel.SendIgnorableMessage(57, from, from.GetColorCharacter() + from.Username, param); // %1: %2
_ = Discord.SendChannelMessageAsync(channel.Name, from.Username, param);
}
else
{
@ -75,6 +75,8 @@ namespace Server.Engines.Chat
if (channel.CanTalk(from))
{
channel.SendIgnorableMessage(58, from, from.GetColorCharacter() + from.Username, param); // %1 %2
_ = Discord.SendChannelMessageAsync(channel.Name, from.Username, param);
}
else
{

View file

@ -20,25 +20,16 @@ namespace Server.Engines.Chat
}
public Mobile Mobile { get; }
public List<ChatUser> Ignored { get; }
public List<ChatUser> Ignoring { get; }
public string Username { get; }
public Channel CurrentChannel { get; set; }
public bool IsOnline => Mobile.NetState != null;
public bool Anonymous { get; set; }
public bool IgnorePrivateMessage { get; set; }
public bool IsModerator => CurrentChannel?.IsModerator(this) == true;
public char GetColorCharacter() =>
IsModerator ? ModeratorColorCharacter :
public char GetColorCharacter() => IsModerator ? ModeratorColorCharacter :
CurrentChannel?.IsVoiced(this) == true ? VoicedColorCharacter : NormalColorCharacter;
public bool CheckOnline()

View file

@ -0,0 +1,47 @@
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace Server.Engines.Chat
{
public static class Discord
{
private static readonly HttpClient _httpClient = new();
private static string _webhookUrl;
public static void Configure()
{
_webhookUrl = ServerConfiguration.GetOrUpdateSetting("chatiscord.webhookUrl", "DISCORD_CHANNEL_WEBHOOK_HERE");
}
public static bool IsEnabled => !string.IsNullOrEmpty(_webhookUrl);
public static async Task SendChannelMessageAsync(string channelName, string username, string message)
{
if (!IsEnabled)
{
return;
}
try
{
var payload = new
{
username = $"UO Chat",
content = $"**[{channelName}]** {username}: {message}",
};
var json = JsonSerializer.Serialize(payload);
var content = new StringContent(json, Encoding.UTF8, "application/json");
await _httpClient.PostAsync(_webhookUrl, content);
}
catch (Exception ex)
{
Console.WriteLine($"Discord webhook error: {ex.Message}");
}
}
}
}