Exemplo n.º 1
0
        protected async Task HandleCommandResult(string action, bool isSuccess, IList <string> descParams = null,
                                                 IList <string> titleParams = null, IList <string> footerParams = null)
        {
            var payload = _embedPayloadFactory.CreateEmbedPayload(GetStruct(isSuccess), GetType(isSuccess), action, Context.User,
                                                                  descParams, titleParams, footerParams);
            var embed = _embedService.CreateBaseEmbed(payload);

            await SendDeletableEmbed(embed);
        }
        public async Task LogError(string errorCode, string extraMsg = "", params string[] args)
        {
            var descArgs = new List <string>(args);

            if (string.IsNullOrEmpty(extraMsg))
            {
                descArgs.Add(extraMsg);
            }

            var payload = _embedPayloadFactory.CreateEmbedPayload(EmbedStruct.ErrorCode, EmbedPayloadType.Error, errorCode, null, descArgs);

            var embed = _embedService.CreateBaseEmbed(payload);
            await _ownerDMChannel.SendEmbedAsync(embed);
        }
        private async Task MessageReceivedHandler(SocketMessage msg)
        {
            if (msg.Author.IsBot || !_injhinuity.Ready) // Wait until bot connected and initialized
            {
                return;
            }

            if (!(msg is SocketUserMessage usrMsg))
            {
                return;
            }

            if (msg.Content.Length <= 0)
            {
                return;
            }

            var channel = msg.Channel as ISocketMessageChannel;
            var guild   = (msg.Channel as SocketTextChannel)?.Guild;

            var context = new SocketCommandContext(_client, usrMsg);

            // Initialise error handler for exceptions to reuse in case of errors
            async void errorCodeHandler(ErrorCodeException ex)
            {
                var ticks = DateTimeOffset.Now.Ticks;

                if (_timeErrorCodeExceptionWasShown + ERROR_CODE_COOLDOWN <= ticks)
                {
                    _timeErrorCodeExceptionWasShown = ticks;

                    if (ex.ErrorCode != "0004")
                    {
                        await _ownerLogger.LogError(ex.ErrorCode, ex.Message);
                    }

                    var titleParams = new List <string> {
                        ex.ErrorCode
                    };
                    var footerParams = new List <string> {
                        ex.ErrorCode
                    };

                    var payload = _embedPayloadFactory.CreateEmbedPayload(EmbedStruct.ErrorCode, EmbedPayloadType.Error, ex.ErrorCode,
                                                                          context.Message.Author, null, titleParams, footerParams);
                    var embed = _embedService.CreateBaseEmbed(payload);

                    var message = await context.Channel.SendMessageAsync("", false, embed.Build());

                    await message.DeleteAfterLong();

                    LogException(ex);
                }
                else
                {
                    await usrMsg.AddReactionAsync(new Emoji("❌"));
                }
            }

            try
            {
                /*if (await _conversationHandler.TryHandlingConversationForUser(context, usrMsg.Content))
                 *  return;*/

                if (await _customCommandHandler.TryHandlingCustomCommand(context, usrMsg.Content))
                {
                    return;
                }

                /*_pollService.TryAddingNewVote(msg.Content, context);*/

                await TryRunCommand(guild, channel, usrMsg, context, errorCodeHandler);

                /*await _foolsService.CheckMessageLength(channel, (SocketUserMessage)msg, guild);*/
            }
            catch (ErrorCodeException ex) { errorCodeHandler(ex); }
            catch (HttpRequestException ex) { errorCodeHandler(new ErrorCodeException("0002", ex.Message, ex.InnerException)); }
            catch (Exception ex)
            {
                await _ownerLogger.LogException(new BotException(context, ex.TargetSite.GetType().Name, ex.Message, ex.StackTrace, ex.InnerException));

                LogException(ex);
            }
        }