135 lines
5.0 KiB
C#
135 lines
5.0 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
using DeukBot4.APIHandlers;
|
|
using DeukBot4.Database;
|
|
using DeukBot4.Utilities;
|
|
using Discord;
|
|
using Discord.WebSocket;
|
|
|
|
namespace DeukBot4.MessageHandlers
|
|
{
|
|
public static class MainHandler
|
|
{
|
|
public static async Task HandleMessage(SocketMessage message)
|
|
{
|
|
if (message.Author.Id == Program.Client.CurrentUser.Id)
|
|
{
|
|
return;
|
|
}
|
|
try
|
|
{
|
|
#pragma warning disable 4014
|
|
CommandHandler.CommandHandler.HandleMessage(message);
|
|
HandlePrivateMessage(message);
|
|
HandleReminder(message);
|
|
ImageBackupHandler.Backup(message);
|
|
JokeHandlers.DeltaHandler(message);
|
|
JokeHandlers.DadJokeHandler(message);
|
|
JokeHandlers.ThanksHandler(message);
|
|
#pragma warning restore 4014
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
await Logger.Main.LogError(e.ToString());
|
|
}
|
|
}
|
|
|
|
private static ITextChannel _dmChannel;
|
|
private static async Task HandlePrivateMessage(SocketMessage message)
|
|
{
|
|
if (message.Channel is IPrivateChannel)
|
|
{
|
|
await Logger.Main.Log(($"Private Message: {message.Author.Username}- {message.Content}"));
|
|
if (_dmChannel == null)
|
|
{
|
|
_dmChannel = (ITextChannel) Program.Client.GetChannel(Program.Settings.DmChannel);
|
|
}
|
|
var eb = new EmbedBuilder
|
|
{
|
|
Author = new EmbedAuthorBuilder
|
|
{
|
|
Name = message.Author.Username,
|
|
IconUrl = message.Author.GetAvatarUrl(),
|
|
},
|
|
Description = message.Content,
|
|
Color = Color.Gold,
|
|
Timestamp = message.CreatedAt,
|
|
Footer = new EmbedFooterBuilder
|
|
{
|
|
Text = $"User ID: {message.Author.Id}"
|
|
}
|
|
};
|
|
await _dmChannel.SendMessageAsync("", embed: eb.Build());
|
|
}
|
|
}
|
|
|
|
private static Regex ReminderMatcher =
|
|
new Regex(
|
|
@".*(remind\s*((?<recipient>me)|<@!*(?<recipient>\d*)>)\s*to)(?<action>.*)(in\s*)(?<timeNum>\d)\s*(?<timeId>\w*)",
|
|
RegexOptions.IgnoreCase);
|
|
private static async Task HandleReminder(SocketMessage message)
|
|
{
|
|
var match = ReminderMatcher.Match(message.Content);
|
|
if (!match.Success)
|
|
{
|
|
return;
|
|
}
|
|
var recipient = match.Groups["recipient"].Captures[0].Value;
|
|
var action = match.Groups["action"].Value.Trim();
|
|
if (string.IsNullOrWhiteSpace(action))
|
|
return;
|
|
var timeNumber = double.Parse(match.Groups["timeNum"].Value);
|
|
var timeIdentifier = match.Groups["timeId"].Value.Trim();
|
|
TimeSpan timespan;
|
|
if (timeIdentifier.ToLowerInvariant().StartsWith("minu"))
|
|
{
|
|
timespan = TimeSpan.FromMinutes(timeNumber);
|
|
}
|
|
else if (timeIdentifier.ToLowerInvariant().StartsWith("hour"))
|
|
{
|
|
timespan = TimeSpan.FromHours(timeNumber);
|
|
}
|
|
else if (timeIdentifier.ToLowerInvariant().StartsWith("day"))
|
|
{
|
|
timespan = TimeSpan.FromDays(timeNumber);
|
|
}
|
|
else if (timeIdentifier.ToLowerInvariant().StartsWith("month"))
|
|
{
|
|
var dest = DateTime.UtcNow.AddMonths((int) (timeNumber));
|
|
dest = dest.AddDays(timeNumber % 1 * 30);
|
|
timespan = dest - DateTime.UtcNow;
|
|
}
|
|
else if (timeIdentifier.ToLowerInvariant().StartsWith("year"))
|
|
{
|
|
var dest = DateTime.UtcNow.AddYears((int) (timeNumber));
|
|
dest = dest.AddDays(timeNumber % 1 * 365);
|
|
timespan = dest - DateTime.UtcNow;
|
|
}
|
|
else
|
|
{
|
|
Logger.Main.LogError("Unknown timespan identifier: " + timeIdentifier);
|
|
return;
|
|
}
|
|
|
|
if (timespan.TotalMinutes < 5)
|
|
{
|
|
message.Channel.SendMessageAsync("A reminder should be at least 5 minutes in the future");
|
|
return;
|
|
}
|
|
|
|
if (!ulong.TryParse(recipient, out var recip))
|
|
{
|
|
recip = message.Author.Id;
|
|
}
|
|
|
|
ReminderHandler.Main.AddReminder(timespan, action, message.Channel.Id, message.Author.Id, recip);
|
|
message.Channel.SendMessageAsync(
|
|
message.Author.Id == recip
|
|
? $"Reminder set! I will remind you in {timespan.ToPrettyFormat()} to {action}"
|
|
: $"Reminder set! I will remind <@!{recip}> in {timespan.ToPrettyFormat()} to {action}");
|
|
}
|
|
}
|
|
} |