1
0
mirror of https://gitlab.com/Deukhoofd/DeukBot4.git synced 2026-04-02 02:30:04 +00:00

Handling for per server toggleable jokes

This commit is contained in:
2018-10-09 17:18:19 +02:00
parent 2fe04c2d1e
commit 771a825d9f
7 changed files with 136 additions and 18 deletions

View File

@@ -10,7 +10,7 @@ namespace DeukBot4.MessageHandlers.JokeHandling
public string Id => "dad";
public string Name => "Hi I'm dad";
private Regex _regex = new Regex(@".*i['| a]*m ([\w\s]{3,15})$",
private readonly Regex _regex = new Regex(@".*i['| a]*m ([\w\s]{3,15})$",
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
public async Task Run(SocketMessage message)

View File

@@ -2,6 +2,8 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DeukBot4.Database.ServerSettings;
using Discord;
using Discord.WebSocket;
namespace DeukBot4.MessageHandlers.JokeHandling
@@ -17,15 +19,21 @@ namespace DeukBot4.MessageHandlers.JokeHandling
foreach (var joke in jokes)
{
var c = (IJokeController)Activator.CreateInstance(joke);
Jokes.Add(c.Id, c);
Jokes.Add(c.Id.ToLowerInvariant(), c);
}
}
public static async Task RunJokes(SocketMessage message)
{
foreach (var jokeController in Jokes)
if (message.Channel is IGuildChannel guildChannel)
{
jokeController.Value.Run(message);
var guildSettings = ServerSettingHandler.GetSettings(guildChannel.GuildId);
var jokes = guildSettings.EnabledJokes;
foreach (var enabledJoke in jokes)
{
var jokeController = Jokes[enabledJoke];
jokeController.Run(message);
}
}
}
}

View File

@@ -9,8 +9,8 @@ namespace DeukBot4.MessageHandlers.JokeHandling
public string Id => "thanks";
public string Name => "Claim credit when people say thanks";
private readonly Regex ThanksMatcher = new Regex(@"(?i)((\b)thanks*(\W|$)+)");
private readonly string[] ThanksResponses = {
private readonly Regex _thanksMatcher = new Regex(@"(?i)((\b)thanks*(\W|$)+)");
private readonly string[] _thanksResponses = {
"You're welcome!", "No problem",
"It was the least I could do, I always do the least I can do!",
"Yes, well, we'd be absolutely nowhere without me.",
@@ -22,10 +22,10 @@ namespace DeukBot4.MessageHandlers.JokeHandling
public async Task Run(SocketMessage message)
{
var content = message.Content;
if (ThanksMatcher.IsMatch(content))
if (_thanksMatcher.IsMatch(content))
{
var response = ThanksResponses[_pos];
_pos = (_pos + 1) % ThanksResponses.Length;
var response = _thanksResponses[_pos];
_pos = (_pos + 1) % _thanksResponses.Length;
message.Channel.SendMessageAsync(response);
}
}