示例#1
0
    public void AddMessageAsync()
    {
        var mock = new Mock <IMessageData>(MockBehavior.Strict);

        mock.Setup(x => x.AddMessageAsync(1, 2, 3, "message", new DateTime())).Returns(Task.CompletedTask);
        var messageService = new MessageService(mock.Object);

        messageService.AddMessageAsync(1, 2, 3, "message", new string[0], new DateTime());

        mock.Verify(x => x.AddMessageAsync(1, 2, 3, "message", new DateTime()), Times.Exactly(1));
    }
示例#2
0
    /// <summary>
    /// Asynchronously handles commands to the bot, which are SocketMessages.
    /// </summary>
    /// <param name="messageParam">The SocketMessage</param>
    /// <returns>Is an async Task.</returns>
    private async Task HandleCommandAsync(SocketMessage messageParam)
    {
        // Don't process the command if it was a system message
        var message = messageParam as SocketUserMessage;

        if (message == null)
        {
            return;
        }

        if (message.Author.IsBot)
        {
            return;
        }

        // Create a WebSocket-based command context based on the message
        var context = new SocketCommandContext(_client, message);

        // Create a number to track where the prefix ends and the command begins
        int argPos = 0;

        // Determine if the message is a command based on the prefix and make sure no bots trigger commands
        if (message.HasCharPrefix(await _guildService.GetPrefixAsync(context.Guild.Id), ref argPos))
        {
            // Execute the command with the command context we just
            // created, along with the service provider for precondition checks.
            await _commands.ExecuteAsync(
                context : context,
                argPos : argPos,
                services : _services);
        }
        else if (await _guildService.GetMessageLogAsync(context.Guild.Id))
        {
            await _messageService.AddMessageAsync(
                context.Guild.Id,
                context.Channel.Id,
                context.User.Id,
                message.Content,
                message.Attachments.Select(x => x.ProxyUrl).ToArray(),
                message.Timestamp.UtcDateTime);
        }
    }