示例#1
0
        private void MessageBusOnReceived(object sender, MessageReceivedEventArgs eventArgs)
        {
            // Unpack locally.
            IInvocationContext context = eventArgs.Context;
            IMessage           message = eventArgs.Message;

            // Is it a command?
            if (message.Text?.StartsWith(".") != true)
            {
                return;
            }

            // Check if command exists.
            string command = message.Text.Split(' ').First().TrimStart('.');

            if (!_commandRegistry.Exists(command))
            {
                Debug.WriteLine($"Invalid command called: {command}");
                return;
            }


            // Ensure we do not back up the rest of the command invocation queue.
            // TODO: Per-server task pools.
            Task.Run(async() =>
            {
                try
                {
                    IMessage result = await _commandRegistry.ExecuteCommandAsync(command, context, message);
                    if (result != null)
                    {
                        _messageBus.RaiseOutgoing(context.Raiser, result);
                    }
                }
                catch (Exception e)
                {
                    // Check if there is a help function.
                    var help = _commandRegistry.GetHelp(command);
                    if (!String.IsNullOrEmpty(help))
                    {
                        _messageBus.RaiseOutgoing(context.Raiser, Message.Create(help));
                    }
                    else
                    {
                        _messageBus.RaiseOutgoing(context.Raiser, Message.Create("```\n" + e.StackTrace + "\n```", new FileAttachment(Path.Combine(PathExtensions.AppDir, "error.jpg"))));
                    }
                }
            });
        }
示例#2
0
 public string Help(string command)
 {
     return(_commandRegistry.GetHelp(command));
 }
示例#3
0
        private void MessageBusOnReceived(object sender, MessageReceivedEventArgs eventArgs)
        {
            // Unpack locally.
            IInvocationContext context = eventArgs.Context;
            IMessage           message = eventArgs.Message;

            // Is it a command?
            if (message.Text?.IsCommand() != true)
            {
                return;
            }

            // Check if command exists.
            string command = message.Text.Split(' ').First().TrimStart('.', '!');

            if (!_commandRegistry.Exists(command))
            {
                if (command[0] != '.' || command[0] != '!')
                {
                    if (message is IReactableMessage reactableMessage)
                    {
                        reactableMessage.React("🚫").Forget();
                    }
                }
                return;
            }

            IChannelTypingIndicator typingChannel = context?.Channel as IChannelTypingIndicator;

            // Ensure we do not back up the rest of the command invocation queue.
            // TODO: Per-server task pools.
            Task.Run(async() =>
            {
                try
                {
                    typingChannel?.SetTyping(true);
                    IMessage result = await _commandRegistry.ExecuteCommandAsync(command, context, message);
                    if (result != null)
                    {
                        _messageBus.RaiseOutgoing(context?.Raiser, result);
                    }
                    typingChannel?.SetTyping(false);
                    if (message.Text[0] == '!')
                    {
                        if (message is IDeletableMessage deletableMessage)
                        {
                            deletableMessage.DeleteAsync().Forget();
                        }
                    }
                }
                catch (Exception e)
                {
                    typingChannel?.SetTyping(false);
                    // Quickly dispose any streams in memory.
                    message.Attachments?.ForEach(x => x.Contents?.Dispose());
                    Log.Error($"{command} FAIL: {e}");

                    if (message is IReactableMessage erroredReactableMessage)
                    {
                        erroredReactableMessage.React("💔").Forget();
                    }
                    else
                    {
                        // Check if there is a help function.
                        var help = _commandRegistry.GetHelp(command);
                        if (!string.IsNullOrEmpty(help))
                        {
                            _messageBus.RaiseOutgoing(context.Raiser, Message.Create(help));
                        }
                        else
                        {
#if DEBUG
                            _messageBus.RaiseOutgoing(context.Raiser,
                                                      Message.Create("```\n" + e.StackTrace + "\n```",
                                                                     new FileAttachment(Path.Combine(PathExtensions.AppDir, "error.jpg"))));
#endif
                        }
                    }
                }
            });
        }