Beispiel #1
0
        /// <summary>
        /// Executes the command
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="context"></param>
        /// <param name="server"></param>
        /// <returns>Returns false if it isn't a command with a prefix</returns>
        private bool HandleCommand(IUserMessage msg, SocketCommandContext context, ServerList server)
        {
            int argPos = 0;

            if (!msg.HasStringPrefix(Global.BotPrefix, ref argPos) &&
                !msg.HasMentionPrefix(Global.BotUser, ref argPos))
            {
                return(false);
            }

            //Check user's permission to use command
            if (!CheckUserPermission(context, server, argPos))
            {
                context.Channel.SendMessageAsync(
                    "You do not have permission to use that command on this guild!").GetAwaiter().GetResult();
                return(true);
            }

            //Execute the command and handle the result
            IResult result = commands.ExecuteAsync(context, argPos, services).GetAwaiter().GetResult();

            HandleCommandResult(context, msg, result).GetAwaiter().GetResult();

            return(true);
        }
        public async Task HandleCommandAsync(IUserMessage message, bool parsePrefix)
        {
            if (message == null)
            {
                m_logger.LogCritical("Received a message that wasn't a SocketUserMessage");
                return;
            }

            if (!IsIntentionalCommand(message !.Content))
            {
                m_logger.LogInfo("Probably unintentional command, ignoring");
                return;
            }

            int argPos = 0;

            if (parsePrefix)
            {
                if (!(message.HasStringPrefix(m_config.Configuration.Prefix, ref argPos, StringComparison.OrdinalIgnoreCase) ||
                      message.HasMentionPrefix(m_client?.CurrentUser, ref argPos)) ||
                    message !.Author.IsBot)
                {
                    return;
                }
            }

            BelfastCommandContext context = new BelfastCommandContext(m_client, message);

            Task <IResult> task = m_command.ExecuteAsync(
                context: context,
                argPos: argPos,
                services: m_services);

            IResult?result = null;

            if ((await Task.WhenAny(task, Task.Delay(TimeSpan.FromSeconds(10)))) == task)
            {
                result = task.Result;
            }

            if (result == null)
            {
                await context.Channel.SendMessageAsync(
                    $"{Emotes.BelfastPout} Sorry For My Misbehaviour Commander!\n" +
                    $"Command timed out"
                    );
            }
            else if (!result.IsSuccess)
            {
                await context.Channel.SendMessageAsync(
                    $"{Emotes.BelfastShock} Sorry For My Misbehaviour Commander!\n" +
                    $"{result.ErrorReason}\n" +
                    $"try **{m_config.Configuration.Prefix}help** for lists of commands"
                    );
            }
            else
            {
                m_logger.LogInfo("Successfully handled command");
            }
        }
Beispiel #3
0
        public Task <int?> TryFindCommandArgPosAsync(
            IUserMessage message,
            CancellationToken cancellationToken)
        {
            // Optimization: Odds are this will only ever be called on one message, per service scope,
            // so we can cache the result.
            if (_lastResult.HasValue &&
                _lastResult.Value.message.Id == message.Id)
            {
                return(Task.FromResult(_lastResult.Value.argPos));
            }

            var argPos = default(int);

            _lastResult = (
                message,
                (message.HasCharPrefix('!', ref argPos) ||
                 message.HasMentionPrefix(_discordSocketClient.CurrentUser, ref argPos))
                    ? argPos
                    : null as int?);

            return(Task.FromResult(_lastResult.Value.argPos));
        }
 private bool MessageIsValid(IUserMessage message, ref int argPos)
 {
     return((message.HasStringPrefix("giv ", ref argPos) ||
             message.HasMentionPrefix(client.CurrentUser, ref argPos)) &&
            !(message.Channel is SocketDMChannel));
 }
Beispiel #5
0
 public static bool HasPrefix(this IUserMessage message, string prefix, IUser user, ref int position)
 => message.HasPrefix(prefix, ref position) || message.HasMentionPrefix(user, ref position);
Beispiel #6
0
 private static bool ParseTriggers(IUserMessage message, ref int argPos)
 => message.HasMentionPrefix(_client.CurrentUser, ref argPos) || message.HasStringPrefix(_config.Prefix, ref argPos);
Beispiel #7
0
 private bool ParseTriggers(IUserMessage message, ref int argPos)
 => message.HasMentionPrefix(_client.CurrentUser, ref argPos) || message.HasStringPrefix(_config["Prefixes:Main"] + " ", ref argPos);