Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ public static void Configure(this ITwitchApiProxy api, User user) {
}

await db.UpdateOAuthInDatabase(user.Id, api.OAuth, stoppingToken);
if (Constants.BotId.Equals(user.TwitchId, StringComparison.InvariantCultureIgnoreCase)) {
TwitchClientProxy.Instance.TwitchUsername = Constants.BotUsername;
TwitchClientProxy.Instance.TwitchOAuthToken = api.OAuth.AccessToken;
}

return api;
}

Expand Down
1 change: 1 addition & 0 deletions src/Nullinside.Api.TwitchBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
}), ServiceLifetime.Transient);
builder.Services.AddScoped<IAuthorizationHandler, BasicAuthorizationHandler>();
builder.Services.AddScoped<ITwitchApiProxy, TwitchApiProxy>();
builder.Services.AddSingleton<ITwitchClientProxy>(_ => TwitchClientProxy.Instance);
builder.Services.AddHostedService<MainService>();
builder.Services.AddAuthentication()
.AddScheme<AuthenticationSchemeOptions, BasicAuthenticationHandler>("Bearer", _ => { });
Expand Down
16 changes: 11 additions & 5 deletions src/Nullinside.Api.TwitchBot/Services/MainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ public class MainService : BackgroundService {
/// </summary>
private readonly ITwitchApiProxy _api;

/// <summary>
/// The twitch client for sending/receiving chat messages.
/// </summary>
private readonly ITwitchClientProxy _client;

/// <summary>
/// Initializes a new instance of the <see cref="MainService" /> class.
/// </summary>
Expand All @@ -93,6 +98,7 @@ public MainService(IServiceScopeFactory serviceScopeFactory) {
_scope = _serviceScopeFactory.CreateScope();
_db = _scope.ServiceProvider.GetRequiredService<INullinsideContext>();
_api = _scope.ServiceProvider.GetRequiredService<ITwitchApiProxy>();
_client = _scope.ServiceProvider.GetRequiredService<ITwitchClientProxy>();
_chatMessageConsumer = new TwitchChatMessageMonitorConsumer(_db, _api, _receivedMessageProcessingQueue);
}

Expand All @@ -109,8 +115,8 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken) {
throw new Exception("Unable to log in as bot user");
}

TwitchClientProxy.Instance.TwitchUsername = Constants.BotUsername;
TwitchClientProxy.Instance.TwitchOAuthToken = botApi.OAuth?.AccessToken;
this._client.TwitchUsername = Constants.BotUsername;
this._client.TwitchOAuthToken = botApi.OAuth?.AccessToken;

_botRules = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(a => a.GetTypes())
Expand All @@ -133,7 +139,7 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken) {
/// The starting point for running the bots' functionality.
/// </summary>
/// <param name="stoppingToken">The stopping token.</param>
private async Task Main(CancellationToken stoppingToken) {
private async Task Main(CancellationToken stoppingToken = new()) {
try {
while (!stoppingToken.IsCancellationRequested) {
using (IServiceScope scope = _serviceScopeFactory.CreateAsyncScope()) {
Expand Down Expand Up @@ -174,8 +180,8 @@ private async Task Main(CancellationToken stoppingToken) {
// its chat limits so that "verified bots" like us don't get special treatment anymore. The only thing
// that skips the chat limits is if it's a channel you're a mod in.
foreach (TwitchModeratedChannel channel in moddedChannels) {
await TwitchClientProxy.Instance.AddMessageCallback(channel.broadcaster_login, OnTwitchMessageReceived);
await TwitchClientProxy.Instance.AddBannedCallback(channel.broadcaster_login, OnTwitchBanReceived);
await this._client.AddMessageCallback(channel.broadcaster_login, OnTwitchMessageReceived);
await this._client.AddBannedCallback(channel.broadcaster_login, OnTwitchBanReceived);
}
}

Expand Down
Loading