예제 #1
0
    private async ValueTask <bool> TryHandleCommand(int shardId, MessageCreateEvent evt, Guild?guild,
                                                    Channel channel, MessageContext ctx)
    {
        var content = evt.Content;

        if (content == null)
        {
            return(false);
        }

        var ourUserId = await _cache.GetOwnUser();

        // Check for command prefix
        if (!HasCommandPrefix(content, ourUserId, out var cmdStart) || cmdStart == content.Length)
        {
            return(false);
        }

        if (ctx.IsDeleting)
        {
            await _rest.CreateMessage(evt.ChannelId, new()
            {
                Content = $"{Emojis.Error} Your system is currently being deleted."
                          + " Due to database issues, it is not possible to use commands while a system is being deleted. Please wait a few minutes and try again.",
                MessageReference = new(guild?.Id, channel.Id, evt.Id)
            });

            return(true);
        }

        // Trim leading whitespace from command without actually modifying the string
        // This just moves the argPos pointer by however much whitespace is at the start of the post-argPos string
        var trimStartLengthDiff =
            content.Substring(cmdStart).Length - content.Substring(cmdStart).TrimStart().Length;

        cmdStart += trimStartLengthDiff;

        try
        {
            var system = ctx.SystemId != null ? await _repo.GetSystem(ctx.SystemId.Value) : null;

            var config = ctx.SystemId != null ? await _repo.GetSystemConfig(ctx.SystemId.Value) : null;

            await _tree.ExecuteCommand(new Context(_services, shardId, guild, channel, evt, cmdStart, system, config, ctx));
        }
        catch (PKError)
        {
            // Only permission errors will ever bubble this far and be caught here instead of Context.Execute
            // so we just catch and ignore these. TODO: this may need to change.
        }

        return(true);
    }
예제 #2
0
    private async ValueTask <bool> TryHandleCommand(int shardId, MessageCreateEvent evt, Guild?guild,
                                                    Channel channel, MessageContext ctx)
    {
        var content = evt.Content;

        if (content == null)
        {
            return(false);
        }

        var ourUserId = await _cache.GetOwnUser();

        // Check for command prefix
        if (!HasCommandPrefix(content, ourUserId, out var cmdStart) || cmdStart == content.Length)
        {
            return(false);
        }

        // Trim leading whitespace from command without actually modifying the string
        // This just moves the argPos pointer by however much whitespace is at the start of the post-argPos string
        var trimStartLengthDiff =
            content.Substring(cmdStart).Length - content.Substring(cmdStart).TrimStart().Length;

        cmdStart += trimStartLengthDiff;

        try
        {
            var system = ctx.SystemId != null ? await _repo.GetSystem(ctx.SystemId.Value) : null;

            var config = ctx.SystemId != null ? await _repo.GetSystemConfig(ctx.SystemId.Value) : null;

            await _tree.ExecuteCommand(new Context(_services, shardId, guild, channel, evt, cmdStart, system, config, ctx));
        }
        catch (PKError)
        {
            // Only permission errors will ever bubble this far and be caught here instead of Context.Execute
            // so we just catch and ignore these. TODO: this may need to change.
        }

        return(true);
    }