Beispiel #1
0
        internal static async Task LoginAsync(string token, AsyncEventHandler <ReadyEventArgs> onReady, Func <Exception, Task> onError, bool background, UserStatus status = UserStatus.Online)
        {
            Exception taskEx = null;

            await _connectSemaphore.WaitAsync();

            try
            {
                var loader = ResourceLoader.GetForViewIndependentUse();

                if (Discord == null)
                {
                    if (background || await WindowsHelloManager.VerifyAsync(VERIFY_LOGIN, loader.GetString("VerifyLoginDisplayReason")))
                    {
                        try
                        {
                            async Task ReadyHandler(ReadyEventArgs e)
                            {
                                LocalSettings.Save("Token", token);

                                e.Client.Ready         -= ReadyHandler;
                                e.Client.SocketErrored -= SocketErrored;
                                e.Client.ClientErrored -= ClientErrored;
                                _readySource.TrySetResult(e);
                                if (onReady != null)
                                {
                                    await onReady(e);
                                }
                            }

                            Task SocketErrored(SocketErrorEventArgs e)
                            {
                                e.Client.Ready         -= ReadyHandler;
                                e.Client.SocketErrored -= SocketErrored;
                                e.Client.ClientErrored -= ClientErrored;

                                Logger.LogError(e.Exception);

                                _readySource.SetException(e.Exception);
                                return(Task.CompletedTask);
                            }

                            Task ClientErrored(ClientErrorEventArgs e)
                            {
                                e.Client.Ready         -= ReadyHandler;
                                e.Client.SocketErrored -= SocketErrored;
                                e.Client.ClientErrored -= ClientErrored;

                                Logger.LogError(e.Exception);

                                _readySource.SetException(e.Exception);
                                return(Task.CompletedTask);
                            }

                            Discord = new DiscordClient(new DiscordConfiguration()
                            {
                                Token     = token,
                                TokenType = TokenType.User,
                                LogLevel  = DSharpPlus.LogLevel.Debug,
                            });

                            Discord.DebugLogger.LogMessageReceived += (o, ee) => Logger.Log(ee.Message, ee.Application);
                            Discord.Ready         += ReadyHandler;
                            Discord.SocketErrored += SocketErrored;
                            Discord.ClientErrored += ClientErrored;

                            await Discord.ConnectAsync(status : status, idlesince : AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Desktop"?(DateTimeOffset?)null : DateTimeOffset.Now);
                        }
                        catch (Exception ex)
                        {
                            Logger.LogError(ex);
                            Tools.ResetPasswordVault();
                            _readySource.TrySetException(ex);
                            await onError(ex);
                        }
                    }
                    else
                    {
                        await onError(null);
                    }
                }
                else
                {
                    try
                    {
                        var res = await _readySource.Task;
                        await onReady(res);
                    }
                    catch
                    {
                        await onError(taskEx);
                    }
                }
            }
            finally
            {
                _connectSemaphore.Release();
            }
        }
Beispiel #2
0
        internal static async Task LoginAsync(string token, AsyncEventHandler <ReadyEventArgs> onReady, Func <Exception, Task> onError, bool background, UserStatus status = UserStatus.Online)
        {
            Exception taskEx = null;

            await _connectSemaphore.WaitAsync();

            if (Discord == null || Discord.IsDisposed)
            {
                if (background || await WindowsHelloManager.VerifyAsync(VERIFY_LOGIN, "Verify your identitiy to login to Unicord!"))
                {
                    try
                    {
                        async Task ReadyHandler(ReadyEventArgs e)
                        {
                            e.Client.Ready         -= ReadyHandler;
                            e.Client.SocketErrored -= SocketErrored;
                            e.Client.ClientErrored -= ClientErrored;
                            _readySource.TrySetResult(e);
                            if (onReady != null)
                            {
                                await onReady(e);
                            }
                        }

                        Task SocketErrored(SocketErrorEventArgs e)
                        {
                            e.Client.Ready         -= ReadyHandler;
                            e.Client.SocketErrored -= SocketErrored;
                            e.Client.ClientErrored -= ClientErrored;
                            _readySource.SetException(e.Exception);
                            return(Task.CompletedTask);
                        }

                        Task ClientErrored(ClientErrorEventArgs e)
                        {
                            e.Client.Ready         -= ReadyHandler;
                            e.Client.SocketErrored -= SocketErrored;
                            e.Client.ClientErrored -= ClientErrored;
                            _readySource.SetException(e.Exception);
                            return(Task.CompletedTask);
                        }

                        Discord = await Task.Run(() => new DiscordClient(new DiscordConfiguration()
                        {
                            Token = token,
                            TokenType = TokenType.User,
                            AutomaticGuildSync = false,
                            LogLevel = DSharpPlus.LogLevel.Debug,
                            MutedStore = new UnicordMutedStore(),
                            GatewayCompressionLevel = GatewayCompressionLevel.None,
                            WebSocketClientFactory = WebSocket4NetCoreClient.CreateNew,
                            ReconnectIndefinitely = true
                        }));

                        Discord.DebugLogger.LogMessageReceived += (o, ee) => Logger.Log(ee.Message, ee.Application);
                        Discord.Ready         += ReadyHandler;
                        Discord.SocketErrored += SocketErrored;
                        Discord.ClientErrored += ClientErrored;

                        // here we go bois
                        // Discord.UseVoiceNext(new VoiceNextConfiguration() { EnableIncoming = true });

                        _connectSemaphore.Release();

                        await Discord.InitializeAsync();

                        await Discord.ConnectAsync(status : status);
                    }
                    catch (Exception ex)
                    {
                        Tools.ResetPasswordVault();
                        _readySource.TrySetException(ex);
                        await onError(ex);
                    }
                }
                else
                {
                    await onError(null);
                }
            }
            else
            {
                _connectSemaphore.Release();

                try
                {
                    var res = await _readySource.Task;
                    await onReady(res);
                }
                catch
                {
                    await onError(taskEx);
                }
            }
        }