Skip to content
Open
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
9 changes: 8 additions & 1 deletion StravaDiscordBot/AppOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class AppOptions
public LokiOptions Loki { get; set; }
public string BaseUrl { get; set; }
public bool LogToConsole { get; set; }
public ContainsReplyOptions[] ContainsReplies { get; set; }
}

public class DiscordOptions
Expand All @@ -29,4 +30,10 @@ public class LokiOptions
public string User { get; set; }
public string Password { get; set; }
}
}

public class ContainsReplyOptions
{
public string Contains { get; set; }
public string Response { get; set; }
}
}
58 changes: 58 additions & 0 deletions StravaDiscordBot/Services/ContainsReplyService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using BetterHostedServices;
using Discord.WebSocket;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace StravaDiscordBot.Services
{
public class ContainsReplyService : CriticalBackgroundService
{
private readonly AppOptions _appOptions;
private readonly ILogger<ContainsReplyService> _logger;
private readonly DiscordSocketClient _discordSocketClient;

public ContainsReplyService(
IApplicationEnder applicationEnder,
AppOptions appOptions,
ILogger<ContainsReplyService> logger,
DiscordSocketClient discordSocketClient
)
: base(applicationEnder)
{
_appOptions = appOptions;
_logger = logger;
_discordSocketClient = discordSocketClient;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_discordSocketClient.MessageReceived += MessageReceivedHandler;
}

private async Task MessageReceivedHandler(SocketMessage message)
{
if (message.Author.IsBot)
{
return;
}

foreach (var containsReply in _appOptions.ContainsReplies)
{
string messageContent = message.Content.ToLower();
string partSearchingFor = containsReply.Contains.ToLower();

if (!messageContent.Contains(partSearchingFor))
{
continue;
}

await message.Channel.SendMessageAsync(containsReply.Response);
}
}
}
}
1 change: 1 addition & 0 deletions StravaDiscordBot/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddHostedService<WeeklyLeaderboardHostedService>();
//services.AddHostedService<ParticipantCleanupHostedService>();
services.AddHostedService<DiscordServerHostedService>();
services.AddHostedService<ContainsReplyService>();

// API

Expand Down