Beispiel #1
0
        public async Task SendMessageAsync(Message message)
        {
            try
            {
                await semaphore.DoAsync(async() =>
                {
                    using (var stream = new NetworkStream(socket))
                    {
                        var stopwatch = Stopwatch.StartNew();

                        using (var byteStream = new MemoryStream())
                            using (var writer = new BinaryWriter(byteStream))
                            {
                                NetworkEncoder.EncodeMessage(writer, message);

                                var messageBytes = byteStream.ToArray();
                                await stream.WriteAsync(messageBytes, 0, messageBytes.Length);
                            }

                        stopwatch.Stop();

                        if (logger.IsTraceEnabled)
                        {
                            logger.Trace($"Sent {message.Command} in {stopwatch.ElapsedMilliseconds} ms\nPayload: {message.Payload.ToArray().ToHexDataString()}");
                        }
                    }
                });
            }
            catch (Exception e)
            {
                Fail(e);
            }
        }
Beispiel #2
0
        public async Task SendMessageAsync(Message message)
        {
            try
            {
                await semaphore.DoAsync(async() =>
                {
                    using (var stream = new NetworkStream(this.socket))
                    {
                        var stopwatch = new Stopwatch();
                        stopwatch.Start();

                        var byteStream = new MemoryStream();
                        NetworkEncoder.EncodeMessage(byteStream, message);

                        var messageBytes = byteStream.ToArray();
                        await stream.WriteAsync(messageBytes, 0, messageBytes.Length);

                        stopwatch.Stop();
                        //Debug.WriteLine("-------------------------");
                        //Debug.WriteLine(string.Format("Sent {0} in {1} ms\nPayload: {2}", message.Command, stopwatch.ElapsedMilliseconds, message.Payload.ToHexDataString()));
                    }
                });
            }
            catch (Exception e)
            {
                Fail(e);
            }
        }
Beispiel #3
0
        public async Task SendMessageAsync(Message message)
        {
            try
            {
                await semaphore.DoAsync(async() =>
                {
                    using (var stream = new NetworkStream(this.socket))
                    {
                        var stopwatch = Stopwatch.StartNew();

                        using (var byteStream = new MemoryStream())
                        {
                            NodeEncoder.EncodeMessage(byteStream, message);

                            var messageBytes = byteStream.ToArray();
                            await stream.WriteAsync(messageBytes, 0, messageBytes.Length);
                        }

                        stopwatch.Stop();

                        if (this.logger.IsTraceEnabled)
                        {
                            this.logger.Trace("Sent {0} in {1} ms\nPayload: {2}".Format2(message.Command, stopwatch.ElapsedMilliseconds, message.Payload.ToHexDataString()));
                        }
                    }
                });
            }
            catch (Exception e)
            {
                Fail(e);
            }
        }
Beispiel #4
0
 private static Task <TResult> DoAsync <TResult>(Func <Reddit, Task <TResult> > func)
 {
     return(_semaphoreSlim.DoAsync(async() =>
     {
         if ((_date + _interval) < DateTime.Now)
         {
         }
         else
         {
             var delay = _interval - (DateTime.Now - _date);
             await Task.Delay(delay).ConfigureAwait(false);
         }
         _date = DateTime.Now;
         return await func(_reddit);
     }));
 }
Beispiel #5
0
        public async Task ConnectAsync()
        {
            await semaphore.DoAsync(async() =>
            {
                try
                {
                    if (!IsConnected)
                    {
                        this.startedConnecting = true;

                        await Task.Factory.FromAsync(this.socket.BeginConnect(this.remoteEndPoint, null, null), this.socket.EndConnect);

                        this.localEndPoint = (IPEndPoint)this.socket.LocalEndPoint;

                        this.isConnected = true;
                    }
                }
                catch (Exception e)
                {
                    this.logger.DebugException(string.Format("Error on connecting to {0}", remoteEndPoint), e);
                    Disconnect();
                }
            });
        }
Beispiel #6
0
        private static async Task ProcessMessageAsync(IMessage message)
        {
            if (message.Author.IsBot && message.Channel is ITextChannel textChannel)
            {
                var embed = message.Embeds.FirstOrDefault();
                if (embed != null)
                {
                    var links = _links.Where(x => x.Channel == textChannel);
                    if (links.Count() > 0)
                    {
                        if (_regexGateClosed.IsMatch(embed.Description))
                        {
                            foreach (var link in links)
                            {
                                var user = await GetUserFromLinkAsync(link).ConfigureAwait(false);

                                await _semaphoreSlim.DoAsync(async() =>
                                {
                                    await link.Channel.SendMessageAsync($"{user.Mention} has cleared this gate, cleared the custom channel permissions.").ConfigureAwait(false);
                                    await UnpermitLinkAsync(link).ConfigureAwait(false);
                                });
                            }
                        }
                        else if (_regexMonsterSlain.IsMatch(embed.Description))
                        {
                            foreach (var link in links)
                            {
                                await _semaphoreSlim.DoAsync(async() =>
                                {
                                    var user = await GetUserFromLinkAsync(link).ConfigureAwait(false);
                                    if (user != null)
                                    {
                                        try
                                        {
                                            var numberOfPermittedKills = GetPermittedNumberOfKillsFromLink(link);
                                            var numberOfRecordedKills  = GetRecordedNumberOfKillsFromLink(link) + 1;
                                            var createdDate            = message?.CreatedAt.UtcDateTime ?? DateTime.UtcNow;
                                            link.Value = FormatValueToString(user, numberOfPermittedKills, (numberOfRecordedKills), createdDate);

                                            if (numberOfPermittedKills > 0 && numberOfRecordedKills > numberOfPermittedKills)
                                            {
                                                await link.Channel.SendMessageAsync($"{user.Mention} has killed {numberOfRecordedKills} monsters, cleared the custom channel permissions.").ConfigureAwait(false);
                                                await UnpermitLinkAsync(link).ConfigureAwait(false);
                                            }
                                            else
                                            {
                                                await Ditto.Database.WriteAsync(uow =>
                                                {
                                                    uow.Links.Update(link);
                                                }).ConfigureAwait(false);
                                            }

                                            // Logging message in case we need it.
                                            Log.Info($"[SoloLeveling][{link.Channel.Name}][{user.Username}#{user.Discriminator}]: Killed {numberOfRecordedKills}/{numberOfPermittedKills}");
                                            //var _ = Task.Run(() =>
                                            //{
                                            //    link.Channel.SendMessageAsync($"[Debug] You killed {numberOfRecordedKills} out of {(numberOfPermittedKills == 0 ? "\\♾️" : $"{numberOfPermittedKills}")} monsters.");
                                            //});
                                        }
                                        catch (Exception ex)
                                        {
                                            Log.Error($"[SoloLeveling] Exception thrown: {ex}");
                                        }
                                    }
                                }).ConfigureAwait(false);
                            }
                        }
                    }
                }
            }
        }