Example #1
0
        private async Task StartBot(string oauthToken)
        {
            var botChatAuthenticated = Twitch.AuthenticateBot()
                                       .FromOAuthToken(oauthToken)
                                       .Build();

            _chatBot = new TwitchChatBot(botChatAuthenticated, _appClient, ServiceProvider, ServiceProvider.GetRequiredService <ILogger <TwitchChatBot> >());

            var orleansTaskScheduler = TaskScheduler.Current;

            _botCancellationSource = new CancellationTokenSource();
            var cancellationToken = _botCancellationSource.Token;

            _botTask = Task.Run(() => _chatBot.StartAsync(cancellationToken));

            _chatBot.SetChannel(_channelId);

            foreach ((var key, var processorInfo) in _channelBotState.State.Commands)
            {
                var registration = _registeredCommands[processorInfo.Type];
                await _chatBot.RegisterMessageProcessor(registration.ProcessorType, processorInfo);
            }
        }
Example #2
0
        static async Task Main(string[] args)
        {
            IConfiguration configuration = null;
            var            builder       = new HostBuilder()
                                           .ConfigureLogging(configure =>
            {
                configure.AddConsole();
            })
                                           .ConfigureAppConfiguration(configure =>
            {
                configure.AddJsonFile("appsettings.json", true);
#if DEBUG
                configure.AddJsonFile("appsettings.Debug.json", true);
#endif
                configure.AddEnvironmentVariables();
                configure.AddUserSecrets <Program>();
                configuration = configure.Build();
            })
                                           .ConfigureServices((hostContext, services) =>
            {
                // Load channels and command configuration from static json file, and inject
                var channelsConfig = new ConfigurationBuilder().AddJsonFile("channels.json").Build();
                IEnumerable <ChannelOptions> channelOptions = new List <ChannelOptions>();
                channelsConfig.GetSection("channels").Bind(channelOptions);
                services.AddTransient <IEnumerable <ChannelOptions> >((_) => channelOptions);

                // Configure services
                services.AddHttpClient();
                services.Configure <TwitchApplicationOptions>(configuration.GetSection("twitch"));
                services.Configure <TwitchChatClientOptions>(configuration.GetSection("twitch").GetSection("IrcOptions"));
                services.AddSingleton <IMessageProcessor, TracingMessageProcessor>();
                services.AddTransient <TwitchChatClient>();
                services.AddTransient <TwitchAPIClient>();
                services.AddTransient <IGDBClient>();
                services.AddSingleton <IMemoryCache, MemoryCache>();
                services.AddSingleton <SteamStoreClient>();
                services.AddSingleton <IGameLocalizationStore, EmbeddedGameLocalizationDb>();
                services.AddTransient <ITwitchCategoryProvider>(s => s.GetRequiredService <PollingTwitchCategoryProvider>());
                services.AddScoped <PollingTwitchCategoryProvider>();
                services.AddSingleton <IAuthenticated>(s =>
                                                       Twitch.Authenticate()
                                                       .FromAppCredentials(
                                                           s.GetService <IOptions <TwitchApplicationOptions> >().Value.ClientId,
                                                           s.GetService <IOptions <TwitchApplicationOptions> >().Value.ClientSecret)
                                                       .Build()
                                                       );
                services.AddSingleton <IBotAuthenticated>(s =>
                                                          Twitch.AuthenticateBot()
                                                          .FromOAuthToken(
                                                              s.GetService <IOptions <TwitchApplicationOptions> >().Value.IrcOptions.OAuthToken)
                                                          .Build()
                                                          );
                services.AddTransient <ITwitchChatClientBuilder>(s =>
                                                                 TwitchChatClientBuilder.Create()
                                                                 .WithOAuthToken(s.GetRequiredService <IOptions <TwitchApplicationOptions> >().Value.IrcOptions.OAuthToken)
                                                                 .WithLoggerFactory(s.GetRequiredService <ILoggerFactory>())
                                                                 );

                // Configure commands
                services.AddCommand <GameSynopsisCommand>("GameSynopsis");
                services.AddCommand <TracingMessageProcessor>("MessageTracer");

                // Add hosted chatbot service
                services.AddSingleton <TwitchChatBot>();
                services.AddHostedService <TwitchChatBot>(s => s.GetRequiredService <TwitchChatBot>());
                services.AddHostedService(services => services.GetRequiredService <PollingTwitchCategoryProvider>());
            })
                                           .UseConsoleLifetime();

            var host = builder.Build();


            var categoryProvider = host.Services.GetRequiredService <PollingTwitchCategoryProvider>();

            categoryProvider.CheckAndSchedule("miekyld");

            var twitchBot = host.Services.GetRequiredService <TwitchChatBot>();

            twitchBot.SetChannel("158511925");
            await twitchBot.RegisterMessageProcessor <GameSynopsisCommand>(new CommandOptions
            {
                Aliases    = new string[] { "jeu", "game" },
                Parameters = new Dictionary <string, string>
                {
                    { "AsReply", bool.TrueString },
                }
            });

            categoryProvider.OnUpdate += async(sender, gameinfo) =>
            {
                var context = new ProcessorContext
                {
                    CategoryId  = gameinfo.TwitchCategoryId,
                    ChannelId   = "158511925",
                    ChannelName = gameinfo.Name,
                    Language    = gameinfo.Language,
                };
                await twitchBot.UpdateContext(context);
            };


            await host.RunAsync();
        }