From 15cf720b7e0042f8e0fc30d23ac4c468a39a5a42 Mon Sep 17 00:00:00 2001 From: Michael VanOverbeek Date: Mon, 13 Feb 2017 18:12:05 +0000 Subject: [PATCH] Discord integration backend. --- ShiftOS.Server/App.config | 4 +- ShiftOS.Server/ChatBackend.cs | 23 ++++++---- ShiftOS.Server/Program.cs | 10 +++-- ShiftOS.Server/SaveManager.cs | 16 ++++--- ShiftOS.Server/ShiftOS.Server.csproj | 64 +++++++++++++++++++++++++--- ShiftOS.Server/packages.config | 35 ++++++++++----- 6 files changed, 116 insertions(+), 36 deletions(-) diff --git a/ShiftOS.Server/App.config b/ShiftOS.Server/App.config index a0a13df..05150dc 100644 --- a/ShiftOS.Server/App.config +++ b/ShiftOS.Server/App.config @@ -1,7 +1,7 @@  - + @@ -16,4 +16,4 @@ - \ No newline at end of file + diff --git a/ShiftOS.Server/ChatBackend.cs b/ShiftOS.Server/ChatBackend.cs index 8de2fec..b4fa4a5 100644 --- a/ShiftOS.Server/ChatBackend.cs +++ b/ShiftOS.Server/ChatBackend.cs @@ -10,6 +10,7 @@ using System.IO; using static ShiftOS.Server.Program; using Discord; using Discord.WebSocket; +using Discord.Net.WebSockets; namespace ShiftOS.Server { @@ -22,12 +23,13 @@ namespace ShiftOS.Server File.WriteAllText("chats.json", "[]"); foreach (var chat in JsonConvert.DeserializeObject>(File.ReadAllText("chats.json"))) { - + string chatID = chat.ID; bool chatKilled = false; if (chat.IsDiscordProxy == true) { DiscordSocketConfig builder = new DiscordSocketConfig(); builder.AudioMode = Discord.Audio.AudioMode.Disabled; + builder.WebSocketProvider = () => Discord.Net.Providers.WS4Net.WS4NetProvider.Instance(); var client = new DiscordSocketClient(builder); await client.LoginAsync(TokenType.Bot, chat.DiscordBotToken); @@ -40,12 +42,15 @@ namespace ShiftOS.Server { if (s.Channel.Id == Convert.ToUInt64(chat.DiscordChannelID)) { - server.DispatchAll(new NetObject("chat_msgreceived", new ServerMessage + if (s.Author.Id != client.CurrentUser.Id) { - Name = "chat_msgreceived", - GUID = "server", - Contents = JsonConvert.SerializeObject(new ChatMessage(s.Author.Mention, "discord_" + s.Channel.Name, s.Content, chat.ID)) - })); + server.DispatchAll(new NetObject("chat_msgreceived", new ServerMessage + { + Name = "chat_msgreceived", + GUID = "server", + Contents = JsonConvert.SerializeObject(new ChatMessage(s.Author.Username, "discord_" + s.Channel.Name, (s as SocketUserMessage).Resolve(0), chatID)) + })); + } } } }; @@ -57,7 +62,7 @@ namespace ShiftOS.Server if (msg.Channel == chat.ID) { //Get the Discord channel for this chat. - var dChan = client.GetChannel(Convert.ToUInt64(chat.ID)) as ISocketMessageChannel; + var dChan = client.GetChannel(Convert.ToUInt64(chat.DiscordChannelID)) as ISocketMessageChannel; //Relay the message to Discord. dChan.SendMessageAsync($"**[{msg.Username}@{msg.SystemName}] ` {msg.Message}"); @@ -119,9 +124,11 @@ namespace ShiftOS.Server [MudRequest("chat_send")] public static void ReceiveMessage(string guid, object contents) { - var msg = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(contents)); + var args = contents as Dictionary; + var msg = new ChatMessage(args["Username"] as string, args["SystemName"] as string, args["Message"] as string, args["Channel"] as string); MessageReceived?.Invoke(guid, msg); } } + } diff --git a/ShiftOS.Server/Program.cs b/ShiftOS.Server/Program.cs index ea16ab3..69571d0 100644 --- a/ShiftOS.Server/Program.cs +++ b/ShiftOS.Server/Program.cs @@ -163,7 +163,8 @@ namespace ShiftOS.Server Console.WriteLine("Server stopping."); }; - ChatBackend.StartDiscordBots(); + var task = ChatBackend.StartDiscordBots(); + task.Wait(); } public static string ReadEncFile(string fPath) @@ -247,8 +248,9 @@ namespace ShiftOS.Server method?.Invoke(null, new[] { msg.GUID, contents }); } - catch (MudException mEx) + catch (Exception mEx) { + Console.WriteLine(mEx); ClientDispatcher.DispatchTo("Error", msg.GUID, mEx); } catch @@ -264,8 +266,8 @@ namespace ShiftOS.Server } catch (Exception ex) { - Console.WriteLine($"[{DateTime.Now}] Exception while handling request {msg.Name}: {ex}"); - return; + //Console.WriteLine($"[{DateTime.Now}] Exception while handling request {msg.Name}: {ex}"); + //return; } } } diff --git a/ShiftOS.Server/SaveManager.cs b/ShiftOS.Server/SaveManager.cs index 2a0fcd4..785bdec 100644 --- a/ShiftOS.Server/SaveManager.cs +++ b/ShiftOS.Server/SaveManager.cs @@ -123,16 +123,20 @@ namespace ShiftOS.Server [MudRequest("mud_save")] public static void SaveGame(string guid, object contents) { - var sav = JsonConvert.DeserializeObject(contents as string); + var sav = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(contents)); WriteEncFile("saves/" + sav.Username + ".save", JsonConvert.SerializeObject(sav, Formatting.Indented)); - Program.server.DispatchTo(new Guid(guid), new NetObject("auth_failed", new ServerMessage - { - Name = "mud_saved", - GUID = "server" - })); + try + { + Program.server.DispatchTo(new Guid(guid), new NetObject("auth_failed", new ServerMessage + { + Name = "mud_saved", + GUID = "server" + })); + } + catch { } } [MudRequest("usr_givecp")] diff --git a/ShiftOS.Server/ShiftOS.Server.csproj b/ShiftOS.Server/ShiftOS.Server.csproj index b2b3a1e..cb231cc 100644 --- a/ShiftOS.Server/ShiftOS.Server.csproj +++ b/ShiftOS.Server/ShiftOS.Server.csproj @@ -9,9 +9,10 @@ Properties ShiftOS.Server ShiftOS.Server - v4.5.1 + v4.6.1 512 true + AnyCPU @@ -33,14 +34,14 @@ 4 - - ..\packages\Discord.Net.Commands.1.0.0-rc-00595\lib\netstandard1.1\Discord.Net.Commands.dll - True - ..\packages\Discord.Net.Core.1.0.0-rc-00595\lib\netstandard1.1\Discord.Net.Core.dll True + + ..\packages\Discord.Net.Providers.WS4Net.1.0.0-rc-00595\lib\net45\Discord.Net.Providers.WS4Net.dll + True + ..\packages\Discord.Net.Rest.1.0.0-rc-00595\lib\netstandard1.1\Discord.Net.Rest.dll True @@ -53,6 +54,10 @@ ..\packages\Discord.Net.WebSocket.1.0.0-rc-00595\lib\netstandard1.1\Discord.Net.WebSocket.dll True + + ..\packages\Microsoft.Win32.Primitives.4.0.1\lib\net46\Microsoft.Win32.Primitives.dll + True + ..\Libraries\NetSockets.dll @@ -77,18 +82,51 @@ True + + ..\packages\System.AppContext.4.1.0\lib\net46\System.AppContext.dll + True + ..\packages\System.Collections.Immutable.1.3.0\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll True + + ..\packages\System.Console.4.0.0\lib\net46\System.Console.dll + True + + + ..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll + True + + + ..\packages\System.Globalization.Calendars.4.0.1\lib\net46\System.Globalization.Calendars.dll + True + ..\packages\System.Interactive.Async.3.1.0\lib\net45\System.Interactive.Async.dll True + + + ..\packages\System.IO.Compression.ZipFile.4.0.1\lib\net46\System.IO.Compression.ZipFile.dll + True + + + ..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll + True + + + ..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll + True + + + ..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll + True + @@ -99,6 +137,22 @@ + + ..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net461\System.Security.Cryptography.Algorithms.dll + True + + + ..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll + True + + + ..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll + True + + + ..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net461\System.Security.Cryptography.X509Certificates.dll + True + diff --git a/ShiftOS.Server/packages.config b/ShiftOS.Server/packages.config index f90dec0..074a8be 100644 --- a/ShiftOS.Server/packages.config +++ b/ShiftOS.Server/packages.config @@ -1,31 +1,39 @@  - - - - - - + + + + + + - + + + + - + + - + + + + - + + @@ -33,10 +41,15 @@ + - + + + + +