diff --git a/ModLauncher/App.config b/ModLauncher/App.config index 88fa402..71a06ba 100644 --- a/ModLauncher/App.config +++ b/ModLauncher/App.config @@ -1,6 +1,14 @@ - + + + + + + + + + \ No newline at end of file diff --git a/ShiftOS.Modding.VB.LegacySkinConverter/App.config b/ShiftOS.Modding.VB.LegacySkinConverter/App.config index 88fa402..71a06ba 100644 --- a/ShiftOS.Modding.VB.LegacySkinConverter/App.config +++ b/ShiftOS.Modding.VB.LegacySkinConverter/App.config @@ -1,6 +1,14 @@ - + + + + + + + + + \ No newline at end of file diff --git a/ShiftOS.Objects/Objects.cs b/ShiftOS.Objects/Objects.cs index bcd476f..18677be 100644 --- a/ShiftOS.Objects/Objects.cs +++ b/ShiftOS.Objects/Objects.cs @@ -74,8 +74,26 @@ namespace ShiftOS.Objects public string Topic { get; set; } public int MaxUsers { get; set; } //0 for unlimited users (or the MUD maximum) public List Users = new List(); + public bool IsDiscordProxy { get; set; } + public string DiscordBotToken { get; set; } + public string DiscordChannelID { get; set; } } + public class ChatMessage + { + public ChatMessage(string uname, string sys, string message, string chan) + { + Username = uname; + SystemName = sys; + Message = message; + Channel = chan; + } + + public string Username { get; private set; } + public string SystemName { get; private set; } + public string Channel { get; private set; } + public string Message { get; private set; } + } [Serializable] public class ServerMessage diff --git a/ShiftOS.Server.WebAdmin/App.config b/ShiftOS.Server.WebAdmin/App.config index 88fa402..71a06ba 100644 --- a/ShiftOS.Server.WebAdmin/App.config +++ b/ShiftOS.Server.WebAdmin/App.config @@ -1,6 +1,14 @@ - + + + + + + + + + \ No newline at end of file diff --git a/ShiftOS.Server/ChatBackend.cs b/ShiftOS.Server/ChatBackend.cs new file mode 100644 index 0000000..f3d1dee --- /dev/null +++ b/ShiftOS.Server/ChatBackend.cs @@ -0,0 +1,113 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ShiftOS.Objects; +using NetSockets; +using Newtonsoft.Json; +using System.IO; +using static ShiftOS.Server.Program; +using Discord; + +namespace ShiftOS.Server +{ + public static class ChatBackend + { + public static void StartDiscordBots() + { + Reinitialized?.Invoke(); + foreach (var chat in JsonConvert.DeserializeObject>(File.ReadAllText("chats.json"))) + { + + bool chatKilled = false; + if (chat.IsDiscordProxy == true) + { + DiscordConfigBuilder builder = new DiscordConfigBuilder(); + builder.AppName = "ShiftOS"; + builder.AppVersion = System.Reflection.Assembly.GetExecutingAssembly().FullName; + builder.AppUrl = "http://getshiftos.ml/"; + var client = new DiscordClient(builder); + client.Connect(chat.DiscordBotToken, TokenType.Bot); + client.SetGame("ShiftOS"); + client.SetStatus(UserStatus.Online); + client.MessageReceived += (s, e) => + { + if (chatKilled == false) + { + if (e.Channel.Id.ToString() == chat.DiscordChannelID) + { + server.DispatchAll(new NetObject("chat_msgreceived", new ServerMessage + { + Name = "chat_msgreceived", + GUID = "server", + Contents = JsonConvert.SerializeObject(new ChatMessage(e.User.Name, "discord_" + e.Channel.Name, e.Message.Text, chat.ID)) + })); + } + } + }; + MessageReceived += (g, msg) => + { + if (chatKilled == false) + { + //Determine if the message was sent to this channel. + if (msg.Channel == chat.ID) + { + //Get the Discord channel for this chat. + var dChan = client.GetChannel(Convert.ToUInt64(chat.DiscordChannelID)); + //Relay the message to Discord. + dChan.SendMessage($"**[{msg.Username}@{msg.SystemName} ({msg.Channel})]: {msg.Message}"); + + } + //Relay it back to all MUD clients. + RelayMessage(g, msg); + } + }; + Reinitialized += () => + { + client.Disconnect(); + client.Dispose(); + chatKilled = true; + }; + } + else + { + MessageReceived += (g, msg) => + { + if (chatKilled == false) + { + //Just relay it. + RelayMessage(g, msg); + } + }; + Reinitialized += () => { chatKilled = true; }; + } + } + } + + internal static void RelayMessage(string guid, ChatMessage msg) + { + server.DispatchAllExcept(new Guid(guid), new NetObject("chat_msgreceived", new ServerMessage + { + Name = "chat_msgreceived", + GUID = "server", + Contents = JsonConvert.SerializeObject(msg) + })); + + } + + public static event Action MessageReceived; + public static event empty Reinitialized; + + + public delegate void empty(); + + [MudRequest("chat_send")] + public static void ReceiveMessage(string guid, object contents) + { + var msg = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(contents)); + MessageReceived?.Invoke(guid, msg); + + } + } +} diff --git a/ShiftOS.Server/ShiftOS.Server.csproj b/ShiftOS.Server/ShiftOS.Server.csproj index ee12b2e..d5b2cb2 100644 --- a/ShiftOS.Server/ShiftOS.Server.csproj +++ b/ShiftOS.Server/ShiftOS.Server.csproj @@ -33,9 +33,29 @@ 4 + + ..\packages\Discord.Net.0.9.6\lib\net45\Discord.Net.dll + True + ..\Libraries\NetSockets.dll + + ..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.dll + True + + + ..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.Concurrent.dll + True + + + ..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.Enlightenment.dll + True + + + ..\packages\RestSharp.105.2.3\lib\net451\RestSharp.dll + True + @@ -51,8 +71,13 @@ ..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll + + ..\packages\WebSocket4Net.0.14.1\lib\net45\WebSocket4Net.dll + True + + diff --git a/ShiftOS.Server/packages.config b/ShiftOS.Server/packages.config index 5fb0f8f..5309d49 100644 --- a/ShiftOS.Server/packages.config +++ b/ShiftOS.Server/packages.config @@ -1,5 +1,9 @@  + + + + \ No newline at end of file