From 40e15494811bb66219709981060c7faa6c465a84 Mon Sep 17 00:00:00 2001 From: VollRahm Date: Wed, 4 Nov 2020 00:21:31 +0100 Subject: [PATCH] - Reversed commit #958 --- .../Discord.NET/Discord.Net.Core/TokenType.cs | 1 - .../Discord.Net.Rest/DiscordRestApiClient.cs | 19 ++++++----- .../DiscordSocketClient.cs | 7 +++- .../Entities/Guilds/SocketGuild.cs | 7 ++++ .../stream-sniper/MainWindow.xaml | 2 +- .../stream-sniper/MainWindow.xaml.cs | 34 ++++++++++++------- .../stream-sniper/Misc/Config.cs | 23 +++++++++++++ .../stream-sniper/Misc/Helper.cs | 17 ++++++++++ .../stream-sniper/stream-sniper.csproj | 2 ++ 9 files changed, 88 insertions(+), 24 deletions(-) create mode 100644 src/stream-sniper/stream-sniper/Misc/Config.cs create mode 100644 src/stream-sniper/stream-sniper/Misc/Helper.cs diff --git a/src/stream-sniper/stream-sniper/Lib/Discord.NET/Discord.Net.Core/TokenType.cs b/src/stream-sniper/stream-sniper/Lib/Discord.NET/Discord.Net.Core/TokenType.cs index 8ca3f03..76e2f2c 100644 --- a/src/stream-sniper/stream-sniper/Lib/Discord.NET/Discord.Net.Core/TokenType.cs +++ b/src/stream-sniper/stream-sniper/Lib/Discord.NET/Discord.Net.Core/TokenType.cs @@ -5,7 +5,6 @@ namespace Discord /// Specifies the type of token to use with the client. public enum TokenType { - [Obsolete("User logins are deprecated and may result in a ToS strike against your account - please see https://github.com/RogueException/Discord.Net/issues/827", error: true)] User, /// /// An OAuth2 token type. diff --git a/src/stream-sniper/stream-sniper/Lib/Discord.NET/Discord.Net.Rest/DiscordRestApiClient.cs b/src/stream-sniper/stream-sniper/Lib/Discord.NET/Discord.Net.Rest/DiscordRestApiClient.cs index ff6d172..be6ca4a 100644 --- a/src/stream-sniper/stream-sniper/Lib/Discord.NET/Discord.Net.Rest/DiscordRestApiClient.cs +++ b/src/stream-sniper/stream-sniper/Lib/Discord.NET/Discord.Net.Rest/DiscordRestApiClient.cs @@ -82,12 +82,12 @@ namespace Discord.API { switch (tokenType) { - case default(TokenType): - return token; case TokenType.Bot: return $"Bot {token}"; case TokenType.Bearer: return $"Bearer {token}"; + case TokenType.User: + return token; default: throw new ArgumentException(message: "Unknown OAuth token type.", paramName: nameof(tokenType)); } @@ -127,7 +127,7 @@ namespace Discord.API { _loginCancelToken?.Dispose(); _loginCancelToken = new CancellationTokenSource(); - + AuthTokenType = TokenType.User; AuthToken = null; await RequestQueue.SetCancelTokenAsync(_loginCancelToken.Token).ConfigureAwait(false); RestClient.SetCancelToken(_loginCancelToken.Token); @@ -186,7 +186,7 @@ namespace Discord.API { options = options ?? new RequestOptions(); options.HeaderOnly = true; - options.BucketId = bucketId; + options.BucketId = AuthTokenType == TokenType.User ? ClientBucket.Get(clientBucket).Id : bucketId; var request = new RestRequest(RestClient, method, endpoint, options); await SendInternalAsync(method, endpoint, request).ConfigureAwait(false); @@ -200,7 +200,7 @@ namespace Discord.API { options = options ?? new RequestOptions(); options.HeaderOnly = true; - options.BucketId = bucketId; + options.BucketId = AuthTokenType == TokenType.User ? ClientBucket.Get(clientBucket).Id : bucketId; string json = payload != null ? SerializeJson(payload) : null; var request = new JsonRestRequest(RestClient, method, endpoint, json, options); @@ -215,7 +215,7 @@ namespace Discord.API { options = options ?? new RequestOptions(); options.HeaderOnly = true; - options.BucketId = bucketId; + options.BucketId = AuthTokenType == TokenType.User ? ClientBucket.Get(clientBucket).Id : bucketId; var request = new MultipartRestRequest(RestClient, method, endpoint, multipartArgs, options); await SendInternalAsync(method, endpoint, request).ConfigureAwait(false); @@ -228,7 +228,7 @@ namespace Discord.API string bucketId = null, ClientBucketType clientBucket = ClientBucketType.Unbucketed, RequestOptions options = null) where TResponse : class { options = options ?? new RequestOptions(); - options.BucketId = bucketId; + options.BucketId = AuthTokenType == TokenType.User ? ClientBucket.Get(clientBucket).Id : bucketId; var request = new RestRequest(RestClient, method, endpoint, options); return DeserializeJson(await SendInternalAsync(method, endpoint, request).ConfigureAwait(false)); @@ -241,7 +241,7 @@ namespace Discord.API string bucketId = null, ClientBucketType clientBucket = ClientBucketType.Unbucketed, RequestOptions options = null) where TResponse : class { options = options ?? new RequestOptions(); - options.BucketId = bucketId; + options.BucketId = AuthTokenType == TokenType.User ? ClientBucket.Get(clientBucket).Id : bucketId; string json = payload != null ? SerializeJson(payload) : null; var request = new JsonRestRequest(RestClient, method, endpoint, json, options); @@ -255,7 +255,8 @@ namespace Discord.API string bucketId = null, ClientBucketType clientBucket = ClientBucketType.Unbucketed, RequestOptions options = null) { options = options ?? new RequestOptions(); - options.BucketId = bucketId; + + options.BucketId = AuthTokenType == TokenType.User ? ClientBucket.Get(clientBucket).Id : bucketId; var request = new MultipartRestRequest(RestClient, method, endpoint, multipartArgs, options); return DeserializeJson(await SendInternalAsync(method, endpoint, request).ConfigureAwait(false)); diff --git a/src/stream-sniper/stream-sniper/Lib/Discord.NET/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/stream-sniper/stream-sniper/Lib/Discord.NET/Discord.Net.WebSocket/DiscordSocketClient.cs index ed142d0..e70457f 100644 --- a/src/stream-sniper/stream-sniper/Lib/Discord.NET/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/stream-sniper/stream-sniper/Lib/Discord.NET/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -534,7 +534,7 @@ namespace Discord.WebSocket { var model = data.Guilds[i]; var guild = AddGuild(model, state); - if (!guild.IsAvailable) + if (!guild.IsAvailable || ApiClient.AuthTokenType == TokenType.User) unavailableGuilds++; else await GuildAvailableAsync(guild).ConfigureAwait(false); @@ -553,6 +553,9 @@ namespace Discord.WebSocket return; } + if (ApiClient.AuthTokenType == TokenType.User) + await SyncGuildsAsync().ConfigureAwait(false); + _lastGuildAvailableTime = Environment.TickCount; _guildDownloadTask = WaitForGuildsAsync(_connection.CancelToken, _gatewayLogger) .ContinueWith(async x => @@ -627,6 +630,8 @@ namespace Discord.WebSocket var guild = AddGuild(data, State); if (guild != null) { + if (ApiClient.AuthTokenType == TokenType.User) + await SyncGuildsAsync().ConfigureAwait(false); await TimedInvokeAsync(_joinedGuildEvent, nameof(JoinedGuild), guild).ConfigureAwait(false); } else diff --git a/src/stream-sniper/stream-sniper/Lib/Discord.NET/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs b/src/stream-sniper/stream-sniper/Lib/Discord.NET/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs index da9a316..1643d22 100644 --- a/src/stream-sniper/stream-sniper/Lib/Discord.NET/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs +++ b/src/stream-sniper/stream-sniper/Lib/Discord.NET/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs @@ -304,6 +304,13 @@ namespace Discord.WebSocket _features = ImmutableArray.Create();*/ _syncPromise = new TaskCompletionSource(); _downloaderPromise = new TaskCompletionSource(); + + if (Discord.ApiClient.AuthTokenType != TokenType.User) + { + _syncPromise.TrySetResultAsync(true); + /*if (!model.Large) + _ = _downloaderPromise.TrySetResultAsync(true);*/ + } return; } diff --git a/src/stream-sniper/stream-sniper/MainWindow.xaml b/src/stream-sniper/stream-sniper/MainWindow.xaml index 0e9e0d3..527c293 100644 --- a/src/stream-sniper/stream-sniper/MainWindow.xaml +++ b/src/stream-sniper/stream-sniper/MainWindow.xaml @@ -5,7 +5,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:stream_sniper" mc:Ignorable="d" - Title="MainWindow" Height="450" Width="800"> + Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded"> diff --git a/src/stream-sniper/stream-sniper/MainWindow.xaml.cs b/src/stream-sniper/stream-sniper/MainWindow.xaml.cs index 6c4429a..2fb20ff 100644 --- a/src/stream-sniper/stream-sniper/MainWindow.xaml.cs +++ b/src/stream-sniper/stream-sniper/MainWindow.xaml.cs @@ -1,18 +1,9 @@ -using Discord.WebSocket; +using Discord; +using Discord.WebSocket; +using stream_sniper.Misc; using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; using System.Windows; -using System.Windows.Controls; -using System.Windows.Data; -using System.Windows.Documents; -using System.Windows.Input; -using System.Windows.Media; -using System.Windows.Media.Imaging; -using System.Windows.Navigation; -using System.Windows.Shapes; namespace stream_sniper { @@ -21,9 +12,28 @@ namespace stream_sniper /// public partial class MainWindow : Window { + public DiscordSocketClient client = new DiscordSocketClient(); + + //this is only for testing public MainWindow() { InitializeComponent(); + Helper.AllocConsole(); + } + + + //initialize Discord client on window load + private async void Window_Loaded(object sender, RoutedEventArgs e) + { + client = new DiscordSocketClient(); + await client.LoginAsync(TokenType.User, Config.TOKEN, false); + + client.Ready += new Func(ClientReady); + } + + private async Task ClientReady() + { + Console.WriteLine("Client logged in and ready"); } } } diff --git a/src/stream-sniper/stream-sniper/Misc/Config.cs b/src/stream-sniper/stream-sniper/Misc/Config.cs new file mode 100644 index 0000000..3f0985c --- /dev/null +++ b/src/stream-sniper/stream-sniper/Misc/Config.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace stream_sniper.Misc +{ + public static class Config + { + private static string TOKEN_PATH = "token.txt"; + + public static string TOKEN + { + get + { + if (!File.Exists(TOKEN_PATH)) return ""; + return File.ReadAllText(TOKEN_PATH); + } + } + } +} diff --git a/src/stream-sniper/stream-sniper/Misc/Helper.cs b/src/stream-sniper/stream-sniper/Misc/Helper.cs new file mode 100644 index 0000000..e1c4e3d --- /dev/null +++ b/src/stream-sniper/stream-sniper/Misc/Helper.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace stream_sniper.Misc +{ + public static class Helper + { + [DllImport("kernel32")] + public static extern bool AllocConsole(); + + + } +} diff --git a/src/stream-sniper/stream-sniper/stream-sniper.csproj b/src/stream-sniper/stream-sniper/stream-sniper.csproj index 1a5e2e9..9c28f7a 100644 --- a/src/stream-sniper/stream-sniper/stream-sniper.csproj +++ b/src/stream-sniper/stream-sniper/stream-sniper.csproj @@ -665,6 +665,8 @@ + + Code