Esempio n. 1
0
        public override IAsyncResultFunc <TInput> Invoke(Option <IAsyncResultFunc <TInput> > nextAsyncActionOption)
        {
            var asyncActionBuilder = AsyncFuncBuilder <TOutput> .Create();

            configuration.Invoke(asyncActionBuilder);

            var command = asyncActionBuilder.Build();

            var asyncContextSwitchDecorator =
                new ContextAdapter <TInput, TOutput>(parser, command);

            if (nextAsyncActionOption.TryGetValue(out var nextAsyncAction))
            {
                return(Actions.Enumerator.Enumerator <TInput> .Create(asyncContextSwitchDecorator, nextAsyncAction));
            }

            return(asyncContextSwitchDecorator);
        }
        public static AsyncFuncBuilder <PrefixNormalizedDiscordContext> WithGroup(
            this AsyncFuncBuilder <PrefixNormalizedDiscordContext> asyncFuncBuilder,
            IPrefix prefix,
            List <Action <AsyncFuncBuilder <PrefixNormalizedDiscordContext> > > configuration,
            Action <AsyncFuncBuilder <PrefixNormalizedDiscordContext> > defaultAction)
        {
            configuration.Add(defaultAction);

            // TODO: Optimize by making a new type parser for the specific use case
            asyncFuncBuilder.WithAdapter(
                new PrefixTypeParser <PrefixNormalizedDiscordContext, PrefixNormalizedDiscordContext>(
                    prefix,
                    context => context.PrefixNormalizedMessage,
                    (context, normalizedMessage) =>
                    new PrefixNormalizedDiscordContext(context.DiscordContext, normalizedMessage)),
                group => group.Any(configuration, result => result == Result.Completed || result == Result.Aborted));

            return(asyncFuncBuilder);
        }
        public static AsyncFuncBuilder <TContext> WithGroup <TContext, TContext2>(
            this AsyncFuncBuilder <TContext> asyncFuncBuilder,
            IPrefix prefix,
            Func <TContext, string> message,
            Func <TContext, string, TContext2> typeParser,
            List <Action <AsyncFuncBuilder <TContext2> > > configuration,
            Action <AsyncFuncBuilder <TContext2> > defaultAction)
        {
            configuration.Add(defaultAction);

            asyncFuncBuilder.WithAdapter(
                new PrefixTypeParser <TContext, TContext2>(
                    prefix,
                    message,
                    typeParser),
                group => group.Any(configuration, result => result == Result.Completed || result == Result.Aborted));

            return(asyncFuncBuilder);
        }
        public static async Task Main()
        {
            var discordSocketClient = new DiscordSocketClient(
                new DiscordSocketConfig
            {
                AlwaysDownloadUsers = true, LogLevel = LogSeverity.Verbose, DefaultRetryMode = RetryMode.AlwaysRetry
            });

            discordSocketClient.Log += message =>
            {
                Console.WriteLine(message);
                return(Task.CompletedTask);
            };

            await discordSocketClient.LoginAsync(TokenType.Bot, "TOKEN").ConfigureAwait(false);

            await discordSocketClient.StartAsync().ConfigureAwait(false);

            var discordContextFactory = new DiscordContextFactory(discordSocketClient);

            var asyncFuncBuilder = AsyncFuncBuilder <DiscordContext> .Create();

            discordSocketClient.Ready += () =>
            {
                var autoBuilderFactory = new AutoBuilderFactory(TypeParserDictionaryFactory.CreateDefault());

                asyncFuncBuilder
                .WithCondition(context => !context.User.IsBot)
                .WithCondition(context => !context.IsPrivateChannel)
                .WithPrefix(
                    new MentionPrefix(discordSocketClient.CurrentUser),
                    root =>
                    root.WithGroup(
                        new StringPrefix("misc"),
                        new List <Action <AsyncFuncBuilder <PrefixNormalizedDiscordContext> > >
                {
                    echo => echo.WithPrefix(
                        new StringPrefix("echo"),
                        command => command.WithAdapter(
                            autoBuilderFactory.Create()
                            .Build <PrefixNormalizedDiscordContext, EchoContext>(
                                input => input.PrefixNormalizedMessage),
                            CreateEchoConfiguration())),

                    reverse => reverse.WithPrefix(
                        new StringPrefix("reverse"),
                        command => command.WithAdapter(
                            new ReverseTypeParser(),
                            CreateReverseConfiguration()))
                },
                        fallback => fallback.WithAsyncFunc(
                            async context =>
                {
                    await context.DiscordContext.Channel.SendMessageAsync(
                        "Sorry, I don't know that command!")
                    .ConfigureAwait(false);
                })));

                var asyncAction = asyncFuncBuilder.Build();

                discordSocketClient.MessageReceived += async message =>
                {
                    if (message is IUserMessage userMessage)
                    {
                        var result = await asyncAction.InvokeAsync(
                            discordContextFactory.Create(userMessage),
                            CancellationToken.None)
                                     .ConfigureAwait(false);
                    }
                };

                return(Task.CompletedTask);
            };

            await Task.Delay(-1).ConfigureAwait(false);
        }