RegexBot/Module/AutoMod/Responses/Say.cs

46 lines
1.5 KiB
C#
Raw Normal View History

2017-08-26 17:24:37 +00:00
using Discord.WebSocket;
using Noikoio.RegexBot.ConfigItem;
using System;
using System.Threading.Tasks;
2017-11-12 03:12:24 +00:00
namespace Noikoio.RegexBot.Module.AutoMod.Responses
2017-08-26 17:24:37 +00:00
{
/// <summary>
/// Sends a message to the given target.
/// Parameters: say (target) (message)
/// </summary>
2017-09-05 17:18:07 +00:00
class Say : ResponseBase
2017-08-26 17:24:37 +00:00
{
private readonly string _target;
private readonly string _payload;
2017-09-05 17:18:07 +00:00
public Say(ConfigItem rule, string cmdline) : base(rule, cmdline)
2017-08-26 17:24:37 +00:00
{
var line = cmdline.Split(new char[] { ' ' }, 3, StringSplitOptions.RemoveEmptyEntries);
if (line.Length != 3) throw new RuleImportException("Incorrect number of parameters.");
// Very basic target verification. Could be improved?
if (line[1][0] != '@' && line[1][0] != '#')
throw new RuleImportException("The given target is not valid.");
_target = line[1];
_payload = line[2];
if (string.IsNullOrWhiteSpace(_payload))
throw new RuleImportException("Message parameter is blank or missing.");
}
public override async Task Invoke(SocketMessage msg)
{
// 
string reply = ProcessText(_payload, msg);
var target = await GetMessageTargetAsync(_target, msg);
if (target == null)
{
await Log("Error: Unable to resolve the given target.");
}
await target.SendMessageAsync(reply);
}
}
}