Beispiel #1
0
        public CommandHandler(DiscordSocketClient client, IConfig config, ILogger logger, IExecutor executor)
        {
            _client   = client;
            _config   = config;
            _logger   = logger;
            _executor = executor;
            _cmd      = new CommandService();

            _timer = new Timer(async _ =>
            {
                try
                {
                    using (var proc = System.Diagnostics.Process.GetCurrentProcess())
                    {
                        _logger.Log($"mem usage: {proc.WorkingSet64 / 1048576} MiB");
                        using (var dba = new Database.AnalyticsContext(_config))
                        {
                            dba.SystemData.Add(new SystemAnalytics
                            {
                                MeasureDate = DateTime.Now,
                                Value       = proc.WorkingSet64 / 1048576,
                                Type        = SystemAnalyticsEventType.Ram,
                            });
                            await dba.SaveChangesAsync();
                        }
                    }
                }
                catch (Exception ex)
                {
                    _logger.Log($"in mem check: {ex}");
                }
            },
                               null,
                               TimeSpan.FromMinutes(1),
                               TimeSpan.FromMinutes(1));
        }
Beispiel #2
0
        private async Task HandleCommandAsync(SocketMessage message)
        {
            var msg = message as SocketUserMessage;

            if (msg == null)
            {
                return;
            }

            if (msg.Author.IsBot || msg.Author.IsWebhook)
            {
                return;
            }

            string prefix  = _config.Get().Prefix;
            var    context = new SocketCommandContext(_client, msg);

            if (context.Guild != null)
            {
                using (var db = new Database.GuildConfigContext(_config))
                {
                    var gConfig = await db.GetCachedGuildFullConfigAsync(context.Guild.Id);

                    if (gConfig?.Prefix != null)
                    {
                        prefix = gConfig.Prefix;
                    }
                }
            }

            int argPos = 0;

            if (msg.HasStringPrefix(prefix, ref argPos, StringComparison.OrdinalIgnoreCase))
            {
                if (_config.Get().BlacklistedGuilds.Any(x => x == (context.Guild?.Id ?? 0)))
                {
                    return;
                }

                var res = await _cmd.GetExecutableCommandAsync(context, argPos, _provider);

                if (res.IsSuccess())
                {
                    _logger.Log($"Run cmd: u{msg.Author.Id} {res.Command.Match.Command.Name}");
                    using (var dbc = new Database.AnalyticsContext(_config))
                    {
                        string param = null;
                        try
                        {
                            var paramStart = argPos + res.Command.Match.Command.Name.Length;
                            var textBigger = context.Message.Content.Length > paramStart;
                            param = textBigger ? context.Message.Content.Substring(paramStart) : null;
                        }
                        catch (Exception) { }

                        dbc.CommandsData.Add(new CommandsAnalytics()
                        {
                            CmdName   = res.Command.Match.Command.Name,
                            GuildId   = context.Guild?.Id ?? 0,
                            UserId    = context.User.Id,
                            Date      = DateTime.Now,
                            CmdParams = param,
                        });
                        await dbc.SaveChangesAsync();
                    }

                    switch (res.Command.Match.Command.RunMode)
                    {
                    case RunMode.Async:
                        await res.Command.ExecuteAsync(_provider);

                        break;

                    default:
                    case RunMode.Sync:
                        if (!await _executor.TryAdd(res.Command, TimeSpan.FromSeconds(1)))
                        {
                            await context.Channel.SendMessageAsync("", embed : "Przekroczono czas oczekiwania!".ToEmbedMessage(EMType.Error).Build());
                        }
                        break;
                    }
                }
                else
                {
                    await ProcessResultAsync(res.Result, context, argPos, prefix);
                }
            }
        }