Discord integration backend.

This commit is contained in:
Michael VanOverbeek 2017-02-13 18:12:05 +00:00
parent 797f0be617
commit 15cf720b7e
6 changed files with 116 additions and 36 deletions

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<runtime>
<loadFromRemoteSources enabled="true" />
@ -16,4 +16,4 @@
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
</configuration>

View file

@ -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<List<ShiftOS.Objects.Channel>>(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}] `<mud/{msg.Channel}> {msg.Message}");
@ -119,9 +124,11 @@ namespace ShiftOS.Server
[MudRequest("chat_send")]
public static void ReceiveMessage(string guid, object contents)
{
var msg = JsonConvert.DeserializeObject<ChatMessage>(JsonConvert.SerializeObject(contents));
var args = contents as Dictionary<string, object>;
var msg = new ChatMessage(args["Username"] as string, args["SystemName"] as string, args["Message"] as string, args["Channel"] as string);
MessageReceived?.Invoke(guid, msg);
}
}
}

View file

@ -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;
}
}
}

View file

@ -123,16 +123,20 @@ namespace ShiftOS.Server
[MudRequest("mud_save")]
public static void SaveGame(string guid, object contents)
{
var sav = JsonConvert.DeserializeObject<Save>(contents as string);
var sav = JsonConvert.DeserializeObject<Save>(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")]

View file

@ -9,9 +9,10 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ShiftOS.Server</RootNamespace>
<AssemblyName>ShiftOS.Server</AssemblyName>
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
@ -33,14 +34,14 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Discord.Net.Commands, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Discord.Net.Commands.1.0.0-rc-00595\lib\netstandard1.1\Discord.Net.Commands.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Discord.Net.Core, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Discord.Net.Core.1.0.0-rc-00595\lib\netstandard1.1\Discord.Net.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Discord.Net.Providers.WS4Net, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Discord.Net.Providers.WS4Net.1.0.0-rc-00595\lib\net45\Discord.Net.Providers.WS4Net.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Discord.Net.Rest, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Discord.Net.Rest.1.0.0-rc-00595\lib\netstandard1.1\Discord.Net.Rest.dll</HintPath>
<Private>True</Private>
@ -53,6 +54,10 @@
<HintPath>..\packages\Discord.Net.WebSocket.1.0.0-rc-00595\lib\netstandard1.1\Discord.Net.WebSocket.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Win32.Primitives, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Win32.Primitives.4.0.1\lib\net46\Microsoft.Win32.Primitives.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NetSockets">
<HintPath>..\Libraries\NetSockets.dll</HintPath>
</Reference>
@ -77,18 +82,51 @@
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.AppContext, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.AppContext.4.1.0\lib\net46\System.AppContext.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Collections.Immutable, Version=1.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Collections.Immutable.1.3.0\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Console, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Console.4.0.0\lib\net46\System.Console.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.Diagnostics.DiagnosticSource, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Drawing" />
<Reference Include="System.Globalization.Calendars, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Globalization.Calendars.4.0.1\lib\net46\System.Globalization.Calendars.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Interactive.Async, Version=3.0.1000.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263, processorArchitecture=MSIL">
<HintPath>..\packages\System.Interactive.Async.3.1.0\lib\net45\System.Interactive.Async.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.IO.Compression.ZipFile, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.Compression.ZipFile.4.0.1\lib\net46\System.IO.Compression.ZipFile.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.IO.FileSystem, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.IO.FileSystem.Primitives, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Net.Sockets, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Runtime.Caching" />
<Reference Include="System.Runtime.DurableInstancing" />
@ -99,6 +137,22 @@
<Reference Include="System.Runtime.Remoting" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Runtime.Serialization.Formatters.Soap" />
<Reference Include="System.Security.Cryptography.Algorithms, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net461\System.Security.Cryptography.Algorithms.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Security.Cryptography.Encoding, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Security.Cryptography.Primitives, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Security.Cryptography.X509Certificates, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net461\System.Security.Cryptography.X509Certificates.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />

View file

@ -1,31 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Discord.Net" version="1.0.0-rc-00595" targetFramework="net451" />
<package id="Discord.Net.Commands" version="1.0.0-rc-00595" targetFramework="net451" />
<package id="Discord.Net.Core" version="1.0.0-rc-00595" targetFramework="net451" />
<package id="Discord.Net.Rest" version="1.0.0-rc-00595" targetFramework="net451" />
<package id="Discord.Net.Rpc" version="1.0.0-rc-00595" targetFramework="net451" />
<package id="Discord.Net.WebSocket" version="1.0.0-rc-00595" targetFramework="net451" />
<package id="Discord.Net.Core" version="1.0.0-rc-00595" targetFramework="net451" requireReinstallation="true" />
<package id="Discord.Net.Providers.WS4Net" version="1.0.0-rc-00595" targetFramework="net461" />
<package id="Discord.Net.Rest" version="1.0.0-rc-00595" targetFramework="net451" requireReinstallation="true" />
<package id="Discord.Net.Rpc" version="1.0.0-rc-00595" targetFramework="net451" requireReinstallation="true" />
<package id="Discord.Net.WebSocket" version="1.0.0-rc-00595" targetFramework="net451" requireReinstallation="true" />
<package id="DynamicLua" version="1.1.2.0" targetFramework="net452" />
<package id="Microsoft.NETCore.Platforms" version="1.0.1" targetFramework="net451" />
<package id="Microsoft.Win32.Primitives" version="4.0.1" targetFramework="net461" />
<package id="NETStandard.Library" version="1.6.0" targetFramework="net451" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net451" />
<package id="Nito.AsyncEx" version="3.0.1" targetFramework="net451" />
<package id="RestSharp" version="105.2.3" targetFramework="net451" />
<package id="RestSharp" version="105.2.3" targetFramework="net451" requireReinstallation="true" />
<package id="System.AppContext" version="4.1.0" targetFramework="net461" />
<package id="System.Collections" version="4.0.11" targetFramework="net451" />
<package id="System.Collections.Concurrent" version="4.3.0" targetFramework="net451" />
<package id="System.Collections.Immutable" version="1.3.0" targetFramework="net451" />
<package id="System.Console" version="4.0.0" targetFramework="net461" />
<package id="System.Diagnostics.Debug" version="4.0.11" targetFramework="net451" />
<package id="System.Diagnostics.DiagnosticSource" version="4.3.0" targetFramework="net461" />
<package id="System.Diagnostics.Tools" version="4.0.1" targetFramework="net451" />
<package id="System.Diagnostics.Tracing" version="4.1.0" targetFramework="net451" />
<package id="System.Globalization" version="4.0.11" targetFramework="net451" />
<package id="System.Interactive.Async" version="3.1.0" targetFramework="net451" />
<package id="System.Globalization.Calendars" version="4.0.1" targetFramework="net461" />
<package id="System.Interactive.Async" version="3.1.0" targetFramework="net451" requireReinstallation="true" />
<package id="System.IO" version="4.1.0" targetFramework="net451" />
<package id="System.IO.Compression" version="4.3.0" targetFramework="net451" />
<package id="System.IO.Compression" version="4.3.0" targetFramework="net451" requireReinstallation="true" />
<package id="System.IO.Compression.ZipFile" version="4.0.1" targetFramework="net461" />
<package id="System.IO.FileSystem" version="4.3.0" targetFramework="net461" />
<package id="System.IO.FileSystem.Primitives" version="4.3.0" targetFramework="net461" />
<package id="System.Linq" version="4.3.0" targetFramework="net451" />
<package id="System.Linq.Expressions" version="4.3.0" targetFramework="net451" />
<package id="System.Net.Http" version="4.3.0" targetFramework="net451" />
<package id="System.Net.Http" version="4.3.0" targetFramework="net451" requireReinstallation="true" />
<package id="System.Net.Primitives" version="4.0.11" targetFramework="net451" />
<package id="System.Net.Sockets" version="4.3.0" targetFramework="net461" />
<package id="System.ObjectModel" version="4.0.12" targetFramework="net451" />
<package id="System.Reflection" version="4.3.0" targetFramework="net451" />
<package id="System.Reflection.Extensions" version="4.0.1" targetFramework="net451" />
@ -33,10 +41,15 @@
<package id="System.Resources.ResourceManager" version="4.3.0" targetFramework="net451" />
<package id="System.Runtime" version="4.3.0" targetFramework="net451" />
<package id="System.Runtime.Extensions" version="4.1.0" targetFramework="net451" />
<package id="System.Runtime.Handles" version="4.0.1" targetFramework="net461" />
<package id="System.Runtime.InteropServices" version="4.3.0" targetFramework="net451" />
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.0.0" targetFramework="net451" />
<package id="System.Runtime.Numerics" version="4.0.1" targetFramework="net451" />
<package id="System.Runtime.Serialization.Primitives" version="4.3.0" targetFramework="net451" />
<package id="System.Runtime.Serialization.Primitives" version="4.3.0" targetFramework="net451" requireReinstallation="true" />
<package id="System.Security.Cryptography.Algorithms" version="4.3.0" targetFramework="net461" />
<package id="System.Security.Cryptography.Encoding" version="4.3.0" targetFramework="net461" />
<package id="System.Security.Cryptography.Primitives" version="4.3.0" targetFramework="net461" />
<package id="System.Security.Cryptography.X509Certificates" version="4.3.0" targetFramework="net461" />
<package id="System.Text.Encoding" version="4.0.11" targetFramework="net451" />
<package id="System.Text.Encoding.Extensions" version="4.0.11" targetFramework="net451" />
<package id="System.Text.RegularExpressions" version="4.1.0" targetFramework="net451" />