RegexBot/Module/AutoMod/Responses/Say.cs
2017-11-11 19:12:24 -08:00

45 lines
1.5 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Discord.WebSocket;
using Noikoio.RegexBot.ConfigItem;
using System;
using System.Threading.Tasks;
namespace Noikoio.RegexBot.Module.AutoMod.Responses
{
/// <summary>
/// Sends a message to the given target.
/// Parameters: say (target) (message)
/// </summary>
class Say : ResponseBase
{
private readonly string _target;
private readonly string _payload;
public Say(ConfigItem rule, string cmdline) : base(rule, cmdline)
{
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);
}
}
}