Upgrade Discord.Net from 0.9.x to 1.0.x

This commit is contained in:
Cyberboss
2017-09-22 15:15:13 -04:00
parent 39cc432a5d
commit 1980f2fca5
4 changed files with 75 additions and 74 deletions
+41 -35
View File
@@ -1,4 +1,5 @@
using Discord;
using Discord.WebSocket;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -10,11 +11,11 @@ namespace TGServerService
class TGDiscordChatProvider : ITGChatProvider
{
public event OnChatMessage OnChatMessage;
DiscordClient client;
DiscordSocketClient client;
TGDiscordSetupInfo DiscordConfig;
object DiscordLock = new object();
IDictionary<ulong, Channel> SeenPrivateChannels = new Dictionary<ulong, Channel>();
IDictionary<ulong, ISocketMessageChannel> SeenPrivateChannels = new Dictionary<ulong, ISocketMessageChannel>();
public TGDiscordChatProvider(TGChatSetupInfo info)
{
@@ -29,49 +30,54 @@ namespace TGServerService
void Init(TGChatSetupInfo info)
{
DiscordConfig = new TGDiscordSetupInfo(info);
client = new DiscordClient();
client = new DiscordSocketClient();
client.MessageReceived += Client_MessageReceived;
}
private bool CheckAdmin(User u)
private bool CheckAdmin(SocketUser u)
{
if (!DiscordConfig.AdminsAreSpecial)
return DiscordConfig.AdminList.Contains(u.Id.ToString());
/*
foreach (var I in u.Roles)
if (DiscordConfig.AdminList.Contains(I.Id.ToString()))
return true;
*/
return false;
}
private void Client_MessageReceived(object sender, MessageEventArgs e)
private async Task Client_MessageReceived(SocketMessage e)
{
if (e.User.Id == client.CurrentUser.Id)
return;
await Task.Run(() =>
{
if (e.Author.Id == client.CurrentUser.Id)
return;
var pm = e.Channel.IsPrivate;
if (pm && !SeenPrivateChannels.ContainsKey(e.Channel.Id))
SeenPrivateChannels.Add(e.Channel.Id, e.Channel);
var pm = false; //TODO
bool found = false;
if (pm && !SeenPrivateChannels.ContainsKey(e.Channel.Id))
SeenPrivateChannels.Add(e.Channel.Id, e.Channel);
var formattedMessage = e.Message.RawText;
foreach (var u in e.Message.MentionedUsers)
if(u.Id == client.CurrentUser.Id)
{
found = true;
formattedMessage = formattedMessage.Replace(u.Mention, "");
break;
}
bool found = false;
if (!found && !pm)
return;
var formattedMessage = e.Content;
foreach (var u in e.MentionedUsers)
if (u.Id == client.CurrentUser.Id)
{
found = true;
formattedMessage = formattedMessage.Replace(u.Mention, "");
break;
}
formattedMessage = formattedMessage.Trim();
var cid = e.Channel.Id.ToString();
if (!found && !pm)
return;
OnChatMessage(this, e.User.Id.ToString(), cid, formattedMessage, CheckAdmin(e.User), pm || DiscordConfig.AdminChannels.Contains(cid));
formattedMessage = formattedMessage.Trim();
var cid = e.Channel.Id.ToString();
OnChatMessage(this, e.Author.Id.ToString(), cid, formattedMessage, CheckAdmin(e.Author), pm || DiscordConfig.AdminChannels.Contains(cid));
});
}
public string Connect()
@@ -83,7 +89,7 @@ namespace TGServerService
lock (DiscordLock)
{
SeenPrivateChannels.Clear();
client.Connect(DiscordConfig.BotToken, TokenType.Bot).Wait();
client.LoginAsync(TokenType.Bot, DiscordConfig.BotToken).Wait();
}
return !Connected() ? "Connection failed!" : null;
}
@@ -97,7 +103,7 @@ namespace TGServerService
{
lock (DiscordLock)
{
return client.State == ConnectionState.Connected;
return client.ConnectionState == ConnectionState.Connected;
}
}
@@ -110,7 +116,7 @@ namespace TGServerService
lock (DiscordLock)
{
SeenPrivateChannels.Clear();
client.Disconnect().Wait();
client.LogoutAsync().Wait();
}
}
catch { }
@@ -135,7 +141,7 @@ namespace TGServerService
{
var tasks = new List<Task>();
var Config = Properties.Settings.Default;
foreach (var I in client.Servers)
foreach (var I in client.Guilds)
foreach (var J in I.TextChannels)
{
var cid = J.Id.ToString();
@@ -146,7 +152,7 @@ namespace TGServerService
|| (mt.HasFlag(ChatMessageType.WatchdogInfo) && DiscordConfig.WatchdogChannels.Contains(cid));
if (SendToThisChannel)
tasks.Add(J.SendMessage(msg));
tasks.Add(J.SendMessageAsync(msg));
}
foreach (var I in tasks)
I.Wait();
@@ -165,12 +171,12 @@ namespace TGServerService
var Config = Properties.Settings.Default;
var channel = Convert.ToUInt64(channelname);
if (SeenPrivateChannels.ContainsKey(channel))
SeenPrivateChannels[channel].SendMessage(message).Wait();
SeenPrivateChannels[channel].SendMessageAsync(message).Wait();
else
foreach (var I in client.Servers)
foreach (var I in client.Guilds)
foreach (var J in I.TextChannels)
if (J.Id == channel)
J.SendMessage(message).Wait();
J.SendMessageAsync(message).Wait();
TGServerService.WriteInfo(String.Format("Discord Send ({0}): {1}", channelname, message), TGServerService.EventID.ChatSend);
return null;
}
@@ -184,7 +190,7 @@ namespace TGServerService
{
try
{
client.Disconnect().Wait();
client.LogoutAsync().Wait();
}
catch (Exception e) {
TGServerService.WriteError("Discord failed DnD: " + e.ToString(), TGServerService.EventID.ChatDisconnectFail);
+16 -19
View File
@@ -40,8 +40,14 @@
<ApplicationIcon>tgs.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="Discord.Net, Version=0.9.6.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Discord.Net.0.9.6\lib\net45\Discord.Net.dll</HintPath>
<Reference Include="Discord.Net.Core, Version=1.0.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Discord.Net.Core.1.0.2\lib\net45\Discord.Net.Core.dll</HintPath>
</Reference>
<Reference Include="Discord.Net.Rest, Version=1.0.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Discord.Net.Rest.1.0.2\lib\net45\Discord.Net.Rest.dll</HintPath>
</Reference>
<Reference Include="Discord.Net.WebSocket, Version=1.0.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Discord.Net.WebSocket.1.0.2\lib\net45\Discord.Net.WebSocket.dll</HintPath>
</Reference>
<Reference Include="LibGit2Sharp, Version=1.0.22.0, Culture=neutral, PublicKeyToken=7cbde695407f0333, processorArchitecture=MSIL">
<HintPath>..\packages\LibGit2Sharp-SSH.1.0.22\lib\net40\LibGit2Sharp.dll</HintPath>
@@ -49,25 +55,19 @@
<Reference Include="Meebey.SmartIrc4net, Version=0.4.5.0, Culture=neutral, PublicKeyToken=7868485fbf407e0f, processorArchitecture=MSIL">
<HintPath>..\packages\SmartIrc4net.1.1\lib\Meebey.SmartIrc4net.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="Nito.AsyncEx, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.dll</HintPath>
</Reference>
<Reference Include="Nito.AsyncEx.Concurrent, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.Concurrent.dll</HintPath>
</Reference>
<Reference Include="Nito.AsyncEx.Enlightenment, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.Enlightenment.dll</HintPath>
</Reference>
<Reference Include="RestSharp, Version=105.2.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\RestSharp.105.2.3\lib\net452\RestSharp.dll</HintPath>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Collections.Immutable, Version=1.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Collections.Immutable.1.3.1\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll</HintPath>
</Reference>
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Configuration.Install" />
<Reference Include="System.DirectoryServices.AccountManagement" />
<Reference Include="System.Interactive.Async, Version=3.0.1000.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263, processorArchitecture=MSIL">
<HintPath>..\packages\System.Interactive.Async.3.1.1\lib\net45\System.Interactive.Async.dll</HintPath>
</Reference>
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Numerics" />
@@ -79,9 +79,6 @@
<Reference Include="System.ServiceProcess" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
<Reference Include="WebSocket4Net, Version=0.14.1.0, Culture=neutral, PublicKeyToken=eb4e154b696bf72a, processorArchitecture=MSIL">
<HintPath>..\packages\WebSocket4Net.0.14.1\lib\net45\WebSocket4Net.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Administration.cs" />
+6 -5
View File
@@ -1,12 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Discord.Net" version="0.9.6" targetFramework="net452" />
<package id="Discord.Net.Core" version="1.0.2" targetFramework="net452" />
<package id="Discord.Net.Rest" version="1.0.2" targetFramework="net452" />
<package id="Discord.Net.WebSocket" version="1.0.2" targetFramework="net452" />
<package id="DynamicLanguageRuntime" version="1.1.2" targetFramework="net452" />
<package id="LibGit2Sharp-SSH" version="1.0.22" targetFramework="net452" />
<package id="LibGit2Sharp-SSH.NativeBinaries" version="1.0.14" targetFramework="net452" />
<package id="Newtonsoft.Json" version="8.0.3" targetFramework="net452" />
<package id="Nito.AsyncEx" version="3.0.1" targetFramework="net452" />
<package id="RestSharp" version="105.2.3" targetFramework="net452" />
<package id="Newtonsoft.Json" version="10.0.2" targetFramework="net452" />
<package id="SmartIrc4net" version="1.1" targetFramework="net452" />
<package id="WebSocket4Net" version="0.14.1" targetFramework="net452" />
<package id="System.Collections.Immutable" version="1.3.1" targetFramework="net452" />
<package id="System.Interactive.Async" version="3.1.1" targetFramework="net452" />
</packages>
+12 -15
View File
@@ -89,8 +89,14 @@
<ServiceInstall Id="ServiceInstaller" Name="TG Station Server" Type="ownProcess" EraseDescription="no" ErrorControl="normal" Start="auto" Vital="yes" />
<ServiceControl Id="StartService" Start="install" Stop="both" Remove="uninstall" Name="TG Station Server" Wait="yes" />
</Component>
<Component Id="DiscordNet" Guid="*">
<File Source="$(var.TGServerService.TargetDir)/Discord.Net.dll" />
<Component Id="DiscordNetCore" Guid="*">
<File Source="$(var.TGServerService.TargetDir)/Discord.Net.Core.dll" />
</Component>
<Component Id="DiscordNetRest" Guid="*">
<File Source="$(var.TGServerService.TargetDir)/Discord.Net.Rest.dll" />
</Component>
<Component Id="DiscordNetWebSocket" Guid="*">
<File Source="$(var.TGServerService.TargetDir)/Discord.Net.WebSocket.dll" />
</Component>
<Component Id="LibGit2Sharp" Guid="*">
<File Source="$(var.TGServerService.TargetDir)/LibGit2Sharp.dll" />
@@ -101,20 +107,11 @@
<Component Id="NewtonsoftJson" Guid="*">
<File Source="$(var.TGServerService.TargetDir)/Newtonsoft.Json.dll" />
</Component>
<Component Id="NitoAsyncConc" Guid="*">
<File Source="$(var.TGServerService.TargetDir)/Nito.AsyncEx.Concurrent.dll" />
<Component Id="SystemCollectionsImmutable" Guid="*">
<File Source="$(var.TGServerService.TargetDir)/System.Collections.Immutable.dll" />
</Component>
<Component Id="NitoAsync" Guid="*">
<File Source="$(var.TGServerService.TargetDir)/Nito.AsyncEx.dll" />
</Component>
<Component Id="NitoAsyncEnlight" Guid="*">
<File Source="$(var.TGServerService.TargetDir)/Nito.AsyncEx.Enlightenment.dll" />
</Component>
<Component Id="RestSharp" Guid="*">
<File Source="$(var.TGServerService.TargetDir)/RestSharp.dll" />
</Component>
<Component Id="WebSock" Guid="*">
<File Source="$(var.TGServerService.TargetDir)/WebSocket4Net.dll" />
<Component Id="SystemInteractiveAsync" Guid="*">
<File Source="$(var.TGServerService.TargetDir)/System.Interactive.Async.dll" />
</Component>
<Component Id="TGControlPanel" Guid="*">
<File Source="$(var.TGControlPanel.TargetPath)" KeyPath="yes" />