ffa5b5754b
Fixed the following compilation errors: -Moderators collection not initialized -Outdated method signatures for ban and kick in ModuleBase -Update author name in manifests -Fixed incorrect method signature in AutoScriptResponder Minor improvements: -Updated external dependencies -Remove unused variables in ConfDefinition of RegexModerator -Improve parallel execution of matches? -Send exception message on logging failure to reporting channel -Slightly change ModuleLoader logging output -Add Discord.Net unhandled exception output to logging -Updated link to Github -Changed GuildState exception handling message for conciseness Fixes: -SQL index creation in LoggingService -SQL view creation in UserCacheService -Add casts from ulong to long in SQL inserts -External modules no longer loaded twice -Non-Info messages from Discord.Net will now be reported -User data had not been recorded at proper times -Some modules had been returning null instead of Task with null -Guild state exception handling should not have handled config errors
61 lines
2.3 KiB
C#
61 lines
2.3 KiB
C#
using Discord.WebSocket;
|
|
using Newtonsoft.Json.Linq;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Kerobot.Modules.AutoResponder
|
|
{
|
|
/// <summary>
|
|
/// Provides the capability to define text responses to pattern-based triggers for fun or informational
|
|
/// purposes. Although in essence similar to <see cref="RegexModerator.RegexModerator"/>, it is a better
|
|
/// fit for non-moderation use cases and has specific features suitable to that end.
|
|
/// </summary>
|
|
[KerobotModule]
|
|
class AutoResponder : ModuleBase
|
|
{
|
|
public AutoResponder(Kerobot kb) : base(kb)
|
|
{
|
|
DiscordClient.MessageReceived += DiscordClient_MessageReceived;
|
|
}
|
|
|
|
private async Task DiscordClient_MessageReceived(SocketMessage arg)
|
|
{
|
|
if (!(arg.Channel is SocketGuildChannel ch)) return;
|
|
if (arg.Author.IsBot || arg.Author.IsWebhook) return;
|
|
|
|
var definitions = GetGuildState<IEnumerable<Definition>>(ch.Guild.Id);
|
|
if (definitions == null) return; // No configuration in this guild; do no further processing
|
|
|
|
var tasks = new List<Task>();
|
|
foreach (var def in definitions)
|
|
{
|
|
tasks.Add(Task.Run(async () => await ProcessMessageAsync(arg, def)));
|
|
}
|
|
|
|
await Task.WhenAll(tasks);
|
|
}
|
|
|
|
public override Task<object> CreateGuildStateAsync(ulong guild, JToken config)
|
|
{
|
|
// Guild state is a read-only IEnumerable<Definition>
|
|
if (config == null) return Task.FromResult<object>(null);
|
|
var guildDefs = new List<Definition>();
|
|
foreach (var defconf in config.Children<JProperty>())
|
|
{
|
|
// Getting all JProperties in the section.
|
|
// Validation of data is left to the Definition constructor. ModuleLoadException thrown here:
|
|
var def = new Definition(defconf);
|
|
guildDefs.Add(def);
|
|
// TODO global options
|
|
}
|
|
|
|
return Task.FromResult<object>(guildDefs.AsReadOnly());
|
|
}
|
|
|
|
private async Task ProcessMessageAsync(SocketMessage msg, Definition def)
|
|
{
|
|
if (!def.Match(msg)) return;
|
|
await msg.Channel.SendMessageAsync(def.GetResponse());
|
|
}
|
|
}
|
|
}
|