示例#1
0
        public async Task <TypingContext> WaitForTypingChannelAsync(DiscordUser user, TimeSpan timeout)
        {
            var user_id = user.Id;
            var tsc     = new TaskCompletionSource <TypingContext>();
            var ct      = new CancellationTokenSource(timeout);

            ct.Token.Register(() => tsc.TrySetResult(null));

            AsyncEventHandler <TypingStartEventArgs> handler = async(e) =>
            {
                await Task.Yield();

                if (e.User.Id == user_id)
                {
                    var tc = new TypingContext()
                    {
                        Channel       = e.Channel,
                        Interactivity = this,
                        StartedAt     = e.StartedAt,
                        User          = e.User
                    };
                    tsc.TrySetResult(tc);
                    return;
                }
            };

            this.Client.TypingStarted += handler;

            var result = await tsc.Task;

            this.Client.TypingStarted -= handler;
            return(result);
        }
示例#2
0
        // I don't really know anymore why I added this.. -Naam
        // I think I told you it might be useful, but tbh I have no idea myself -Emzi
        // Did you? I don't remember either. Nice it's there anyway I guess.. -Naam
        /// <summary>
        /// Waits for any user to start typing
        /// </summary>
        /// <param name="channel">Channel to check</param>
        /// <param name="timeoutoverride">Timeout override</param>
        /// <returns></returns>
        public async Task <TypingContext> WaitForTypingUserAsync(DiscordChannel channel, TimeSpan?timeoutoverride = null)
        {
            if (channel == null)
            {
                throw new ArgumentNullException(nameof(channel));
            }

            TimeSpan timeout = Config.Timeout;

            if (timeoutoverride != null)
            {
                timeout = (TimeSpan)timeoutoverride;
            }

            var channel_id = channel.Id;
            var tsc        = new TaskCompletionSource <TypingContext>();
            var ct         = new CancellationTokenSource(timeout);

            ct.Token.Register(() => tsc.TrySetResult(null));

            try
            {
                this.Client.TypingStarted += Handler;

                var result = await tsc.Task.ConfigureAwait(false);

                return(result);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                this.Client.TypingStarted -= Handler;
                ct.Dispose();
            }

            #region Handler
            async Task Handler(TypingStartEventArgs e)
            {
                await Task.Yield();

                if (e.Channel.Id == channel_id)
                {
                    var tc = new TypingContext()
                    {
                        Channel       = e.Channel,
                        Interactivity = this,
                        StartedAt     = e.StartedAt,
                        User          = e.User
                    };
                    tsc.TrySetResult(tc);
                    return;
                }
            }

            #endregion
        }
示例#3
0
        public async Task <TypingContext> WaitForTypingChannelAsync(DiscordUser user, TimeSpan?timeoutoverride = null)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            TimeSpan timeout = Config.Timeout;

            if (timeoutoverride != null)
            {
                timeout = (TimeSpan)timeoutoverride;
            }

            var user_id = user.Id;
            var tsc     = new TaskCompletionSource <TypingContext>();
            var ct      = new CancellationTokenSource(timeout);

            ct.Token.Register(() => tsc.TrySetResult(null));

            AsyncEventHandler <TypingStartEventArgs> handler = async(e) =>
            {
                await Task.Yield();

                if (e.User.Id == user_id)
                {
                    var tc = new TypingContext()
                    {
                        Channel       = e.Channel,
                        Interactivity = this,
                        StartedAt     = e.StartedAt,
                        User          = e.User
                    };
                    tsc.TrySetResult(tc);
                    return;
                }
            };

            try
            {
                this.Client.TypingStarted += handler;

                var result = await tsc.Task;
                return(result);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                this.Client.TypingStarted -= handler;
            }
        }