NadekoBot/NadekoBot.Modules.Utility/RemindCommands.cs

157 lines
6.0 KiB
C#
Raw Normal View History

2016-08-25 22:05:29 +00:00
using Discord;
using Discord.Commands;
using NadekoBot.Extensions;
using NadekoBot.Core.Services;
using NadekoBot.Core.Services.Database.Models;
2016-08-25 22:05:29 +00:00
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common.Attributes;
using NadekoBot.Modules.Administration.Services;
using NadekoBot.Modules.Utility.Services;
2016-08-25 22:05:29 +00:00
namespace NadekoBot.Modules.Utility
{
public partial class Utility
{
2016-08-25 22:07:15 +00:00
[Group]
2017-07-15 16:34:34 +00:00
public class RemindCommands : NadekoSubmodule<RemindService>
2016-08-25 22:05:29 +00:00
{
private readonly DbService _db;
private readonly GuildTimezoneService _tz;
2016-08-25 22:05:29 +00:00
2017-07-15 16:34:34 +00:00
public RemindCommands(DbService db, GuildTimezoneService tz)
2016-08-25 22:05:29 +00:00
{
2017-05-22 23:59:31 +00:00
_db = db;
_tz = tz;
2016-08-25 22:05:29 +00:00
}
2016-10-24 05:58:18 +00:00
public enum MeOrHere
{
Me,Here
}
[NadekoCommand, Usage, Description, Aliases]
2016-08-25 22:05:29 +00:00
[RequireContext(ContextType.Guild)]
[Priority(1)]
public async Task Remind(MeOrHere meorhere, string timeStr, [Remainder] string message)
2016-08-25 22:05:29 +00:00
{
2017-03-06 17:43:03 +00:00
ulong target;
target = meorhere == MeOrHere.Me ? Context.User.Id : Context.Channel.Id;
2017-03-10 06:46:31 +00:00
await RemindInternal(target, meorhere == MeOrHere.Me, timeStr, message).ConfigureAwait(false);
2016-10-24 05:58:18 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.ManageMessages)]
[Priority(0)]
public async Task Remind(ITextChannel channel, string timeStr, [Remainder] string message)
{
var perms = ((IGuildUser)Context.User).GetPermissions((ITextChannel)channel);
if (!perms.SendMessages || !perms.ReadMessages)
{
await ReplyErrorLocalized("cant_read_or_send").ConfigureAwait(false);
return;
}
else
{
var _ = RemindInternal(channel.Id, false, timeStr, message).ConfigureAwait(false);
}
}
2016-08-25 22:05:29 +00:00
2017-03-10 06:46:31 +00:00
public async Task RemindInternal(ulong targetId, bool isPrivate, string timeStr, [Remainder] string message)
{
var m = _service.Regex.Match(timeStr);
2016-08-25 22:05:29 +00:00
if (m.Length == 0)
{
await ReplyErrorLocalized("remind_invalid_format").ConfigureAwait(false);
2016-08-25 22:05:29 +00:00
return;
}
string output = "";
var namesAndValues = new Dictionary<string, int>();
foreach (var groupName in _service.Regex.GetGroupNames())
2016-08-25 22:05:29 +00:00
{
if (groupName == "0") continue;
int value;
2016-08-25 22:05:29 +00:00
int.TryParse(m.Groups[groupName].Value, out value);
if (string.IsNullOrEmpty(m.Groups[groupName].Value))
{
namesAndValues[groupName] = 0;
continue;
}
if (value < 1 ||
2016-11-21 20:46:24 +00:00
(groupName == "months" && value > 1) ||
(groupName == "weeks" && value > 4) ||
(groupName == "days" && value >= 7) ||
(groupName == "hours" && value > 23) ||
(groupName == "minutes" && value > 59))
2016-08-25 22:05:29 +00:00
{
2017-03-10 06:46:31 +00:00
await Context.Channel.SendErrorAsync($"Invalid {groupName} value.").ConfigureAwait(false);
2016-08-25 22:05:29 +00:00
return;
}
namesAndValues[groupName] = value;
2016-08-25 22:05:29 +00:00
output += m.Groups[groupName].Value + " " + groupName + " ";
}
2017-06-12 23:40:39 +00:00
var time = DateTime.UtcNow + new TimeSpan(30 * namesAndValues["months"] +
2016-11-21 20:46:24 +00:00
7 * namesAndValues["weeks"] +
namesAndValues["days"],
namesAndValues["hours"],
namesAndValues["minutes"],
2016-08-25 22:05:29 +00:00
0);
var rem = new Reminder
{
2017-03-06 17:43:03 +00:00
ChannelId = targetId,
IsPrivate = isPrivate,
2016-08-25 22:05:29 +00:00
When = time,
Message = message,
2016-12-16 18:43:57 +00:00
UserId = Context.User.Id,
2017-03-10 06:46:31 +00:00
ServerId = Context.Guild.Id
2016-08-25 22:05:29 +00:00
};
2017-05-22 23:59:31 +00:00
using (var uow = _db.UnitOfWork)
2016-08-25 22:05:29 +00:00
{
uow.Reminders.Add(rem);
2016-08-26 01:35:10 +00:00
await uow.CompleteAsync();
2016-08-25 22:05:29 +00:00
}
var gTime = TimeZoneInfo.ConvertTime(time, _tz.GetTimeZoneOrUtc(Context.Guild.Id));
try
{
2017-03-10 06:46:31 +00:00
await Context.Channel.SendConfirmAsync(
"⏰ " + GetText("remind",
2017-03-06 17:43:03 +00:00
Format.Bold(!isPrivate ? $"<#{targetId}>" : Context.User.Username),
Format.Bold(message.SanitizeMentions()),
Format.Bold(output),
gTime, gTime)).ConfigureAwait(false);
}
catch
{
// ignored
}
await _service.StartReminder(rem);
2016-08-25 22:05:29 +00:00
}
2016-10-24 05:58:18 +00:00
[NadekoCommand, Usage, Description, Aliases]
[OwnerOnly]
public async Task RemindTemplate([Remainder] string arg)
{
if (string.IsNullOrWhiteSpace(arg))
return;
2016-08-25 22:05:29 +00:00
2017-05-22 23:59:31 +00:00
using (var uow = _db.UnitOfWork)
{
uow.BotConfig.GetOrCreate().RemindMessageFormat = arg.Trim();
await uow.CompleteAsync().ConfigureAwait(false);
}
await ReplyConfirmLocalized("remind_template").ConfigureAwait(false);
}
2016-08-25 22:05:29 +00:00
}
}
2016-11-21 14:44:31 +00:00
}