예제 #1
0
        // 例えば 1d100 などといった、コマンドを用いない特別な処理。
        private async Task <(Response result, bool executesCommand)> ProcessNonCommandAsync(ILazySocketMessage message, ulong botCurrentUserId)
        {
            var expr = await Expr.Main.InterpretFromLazySocketMessageAsync(message, botCurrentUserId);

            if (!expr.HasValue)
            {
                var author = await message.GetAuthorAsync();

                return(Response.TryCreateCaution(expr.Error, await message.GetChannelAsync(), author), false);
            }
            var executed = expr.Value.ExecuteOrDefault();

            if (executed == null || expr.Value.IsConstant)
            {
                return(Response.None, true);
            }
            {
                var channel = await message.GetChannelAsync();

                var author = await message.GetAuthorAsync();

                await _basicMachines.Scan.SetDiceAsync(await channel.GetIdAsync(), await author.GetIdAsync(), await author.GetUsernameAsync(), executed);

                var result = await Response.TryCreateSayAsync(_client, $"{await author.GetMentionAsync()} {executed.Message}", await channel.GetIdAsync()) ?? Response.None;

                return(result, false);
            }
        }
예제 #2
0
        public async Task ReceiveMessageAsync(ILazySocketMessage message, ulong botCurrentUserId)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            //var content = await message.GetContentAsync();
            //ConsoleEx.WriteReceivedMessage(content);

            await _basicMachines.Scan.TryUpdateScansAsync();

            var author = await message.GetAuthorAsync();

            if (await author.GetIsBotAsync())
            {
                return;
            }

            var(nonCommandResponse, executesCommand) = await ProcessNonCommandAsync(message, botCurrentUserId);

            if (nonCommandResponse.Type != ResponseType.None)
            {
                _manualResponseSent.OnNext(nonCommandResponse);
            }
            if (!executesCommand)
            {
                return;
            }

            var rawCommand = await RawCommand.CreateFromSocketMessageOrDefaultAsync(message, botCurrentUserId);

            if (rawCommand == null || rawCommand.Body == null)
            {
                return;
            }

            if (rawCommand.HasMentions && !rawCommand.IsMentioned)
            {
                return;
            }

            var channel = await message.GetChannelAsync();

            var allCommands = CreateAllCommands();

            if (allCommands.TryGetValue(rawCommand.Body, out var command))
            {
                _manualResponseSent.OnNext(await command.InvokeAsync(rawCommand, _client, await channel.GetIdAsync(), await author.GetIdAsync()));
                return;
            }
            //{
            //    var response = await Response.TryCreateCautionAsync(_client, Texts.Error.Commands.NotFound(rawCommand.Value.Body), await channel.GetIdAsync(), await author.GetIdAsync()) ?? Response.None;
            //    _manualResponseSent.OnNext(response);
            //}
        }