Exemplo n.º 1
0
        private void ManageException(DiscordMessage message, DiscordUser author, DiscordChannel channel, Exception ex, BaseDiscordCommand command)
        {
            LogMessage?.Invoke(this, $"\n --- Something's f****d up! --- \n{ex.ToString()}\n");
            _telemetryClient?.TrackException(ex, new Dictionary <string, string> {
                { "command", command.GetType().Name }
            });

            if (!(ex is TaskCanceledException) && !(ex is OperationCanceledException))
            {
                new Task(async() =>
                {
                    try
                    {
                        DiscordMessage msg = await channel.SendMessageAsync(
                            $"Something's gone very wrong executing that command, and an {ex.GetType().Name} occured." +
                            "\r\nThis error has been reported, and should be fixed soon:tm:!" +
                            $"\r\nThis message will be deleted in 10 seconds.");

                        await Task.Delay(10_000);
                        await msg.DeleteAsync();
                    }
                    catch { }
                }).Start();
            }
        }
Exemplo n.º 2
0
        internal static RequestTelemetry GetRequestTelemetry(DiscordUser author, DiscordChannel channel, BaseDiscordCommand command, DateTimeOffset start, string code, bool success)
        {
            RequestTelemetry tel = new RequestTelemetry(command?.GetType().Name ?? "N/A", start, DateTimeOffset.Now - start, code, success);

            tel.Properties.Add("invoker", author.Id.ToString());
            tel.Properties.Add("channel", channel.Id.ToString());
            tel.Properties.Add("guild", channel.Guild?.Id.ToString());

            return(tel);
        }
Exemplo n.º 3
0
        private async Task RunCommandAsync(DiscordMessage message, DiscordUser author, DiscordChannel channel, IEnumerable <string> commandSegments, string commandAlias, BaseDiscordCommand command, DateTimeOffset start)
        {
            bool success = true;

            try
            {
                await channel.TriggerTypingAsync();

                command = InstantateDiscordCommand(command.GetType());
                _config.Happiness.TryGetValue(author.Id, out sbyte h);

                CommandContext context = new CommandContext(commandSegments.ToArray(), message, _discordClient)
                {
                    Happiness = h,
                    _logger   = _loggerFactory.CreateLogger("Commands")
                };


                context.AdditionalData["botContext"] = this;
                string[]      cmdsegarr = commandSegments.ToArray();
                CommandResult result    = await command.RunCommand(cmdsegarr, context);

                LogMessage?.Invoke(this, ($"[{message.Channel.Guild?.Name ?? message.Channel.Name}] \"{command.Name}\" returned ReturnType.{result.ReturnType}."));
                if (result.ReturnType != ReturnType.None)
                {
                    if (result.ReturnType == ReturnType.Text && result.ResultText.Length > 2000)
                    {
                        for (int i = 0; i < result.ResultText.Length; i += 1993)
                        {
                            string str = result.ResultText.Substring(i, Math.Min(1993, result.ResultText.Length - i));
                            if (result.ResultText.StartsWith("```") && !str.StartsWith("```"))
                            {
                                str = "```" + str;
                            }
                            if (result.ResultText.EndsWith("```") && !str.EndsWith("```"))
                            {
                                str = str + "```";
                            }

                            LogMessage?.Invoke(this, ($"Chunking message to {str.Length} chars"));
                            await channel.SendMessageAsync(str);

                            await Task.Delay(2000);
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrWhiteSpace(result.Attachment))
                        {
                            await channel.SendFileAsync(result.Attachment, result.ResultText, embed : result.ResultEmbed);
                        }
                        else if (result.Stream != null || result.ReturnType == ReturnType.File)
                        {
                            if (result.Stream.Length <= 8 * 1024 * 1024)
                            {
                                await channel.SendFileAsync(result.Stream, result.FileName, result.ResultText, false, result.ResultEmbed);
                            }
                            else
                            {
                                await channel.SendMessageAsync("This command has resulted in an attachment that is over 8MB in size and cannot be sent.");
                            }
                        }
                        else
                        {
                            await channel.SendMessageAsync(result.ResultText, embed : result.ResultEmbed);
                        }
                    }
                }


                RequestTelemetry request = InternalTools.GetRequestTelemetry(author, channel, command, start, success ? "200" : "500", success);
                foreach (var pair in result.InsightsData)
                {
                    request.Properties.Add(pair);
                }

                _telemetryClient?.TrackRequest(request);

                _config.Happiness[author.Id] = (sbyte)((int)(context.Happiness).Clamp(sbyte.MinValue, sbyte.MaxValue));
            }
            catch (BadArgumentsException ex)
            {
                Console.WriteLine(ex);
                await HandleBadRequest(message, author, channel, commandSegments, commandAlias, command, start);
            }
            catch (CommandException ex)
            {
                Console.WriteLine(ex);
                await channel.SendMessageAsync(ex.Message);

                _telemetryClient?.TrackRequest(InternalTools.GetRequestTelemetry(author, channel, command, start, "400", false));
            }
            catch (ArgumentOutOfRangeException ex)
            {
                Console.WriteLine(ex);
                await channel.SendMessageAsync("Hey there! That's gonna cause some issues, no thanks!!");

                _telemetryClient?.TrackRequest(InternalTools.GetRequestTelemetry(author, channel, command, start, "400", false));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                success = false;
                ManageException(message, author, channel, ex, command);

                sbyte h = 0;
                _config.Happiness?.TryGetValue(author.Id, out h);

                _config.Happiness[author.Id] = (sbyte)((int)h - 1).Clamp(sbyte.MinValue, sbyte.MaxValue);
            }
            finally
            {
                if (command is IDisposable disp)
                {
                    disp.Dispose();
                }
            }
        }