2016-08-25 22:05:29 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
2016-10-09 20:40:14 +00:00
|
|
|
|
using NadekoBot.Extensions;
|
2017-10-13 04:14:54 +00:00
|
|
|
|
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
|
|
|
|
{
|
2017-05-29 04:13:22 +00:00
|
|
|
|
private readonly DbService _db;
|
2017-07-05 11:41:50 +00:00
|
|
|
|
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;
|
2017-07-05 11:41:50 +00:00
|
|
|
|
_tz = tz;
|
2016-08-25 22:05:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-24 05:58:18 +00:00
|
|
|
|
public enum MeOrHere
|
|
|
|
|
{
|
|
|
|
|
Me,Here
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-05 03:09:44 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2016-08-25 22:05:29 +00:00
|
|
|
|
[RequireContext(ContextType.Guild)]
|
2017-07-15 03:04:16 +00:00
|
|
|
|
[Priority(1)]
|
2016-12-21 08:33:47 +00:00
|
|
|
|
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)]
|
2017-03-05 22:34:01 +00:00
|
|
|
|
[RequireUserPermission(GuildPermission.ManageMessages)]
|
2017-07-15 03:04:16 +00:00
|
|
|
|
[Priority(0)]
|
2017-05-16 22:42:30 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2017-05-27 23:51:22 +00:00
|
|
|
|
var m = _service.Regex.Match(timeStr);
|
2016-08-25 22:05:29 +00:00
|
|
|
|
|
|
|
|
|
if (m.Length == 0)
|
|
|
|
|
{
|
2017-02-23 02:06:37 +00:00
|
|
|
|
await ReplyErrorLocalized("remind_invalid_format").ConfigureAwait(false);
|
2016-08-25 22:05:29 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string output = "";
|
|
|
|
|
var namesAndValues = new Dictionary<string, int>();
|
|
|
|
|
|
2017-05-27 23:51:22 +00:00
|
|
|
|
foreach (var groupName in _service.Regex.GetGroupNames())
|
2016-08-25 22:05:29 +00:00
|
|
|
|
{
|
|
|
|
|
if (groupName == "0") continue;
|
2017-02-23 02:06:37 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2017-02-23 02:06:37 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2017-02-23 02:06:37 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2017-07-05 11:41:50 +00:00
|
|
|
|
var gTime = TimeZoneInfo.ConvertTime(time, _tz.GetTimeZoneOrUtc(Context.Guild.Id));
|
2017-02-23 02:06:37 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-03-10 06:46:31 +00:00
|
|
|
|
await Context.Channel.SendConfirmAsync(
|
2017-02-23 02:06:37 +00:00
|
|
|
|
"⏰ " + GetText("remind",
|
2017-03-06 17:43:03 +00:00
|
|
|
|
Format.Bold(!isPrivate ? $"<#{targetId}>" : Context.User.Username),
|
2017-02-23 02:06:37 +00:00
|
|
|
|
Format.Bold(message.SanitizeMentions()),
|
|
|
|
|
Format.Bold(output),
|
2017-07-05 11:41:50 +00:00
|
|
|
|
gTime, gTime)).ConfigureAwait(false);
|
2017-02-23 02:06:37 +00:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored
|
|
|
|
|
}
|
2017-05-27 23:51:22 +00:00
|
|
|
|
await _service.StartReminder(rem);
|
2016-08-25 22:05:29 +00:00
|
|
|
|
}
|
2016-10-24 05:58:18 +00:00
|
|
|
|
|
2016-10-05 03:09:44 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2016-09-30 02:20:09 +00:00
|
|
|
|
[OwnerOnly]
|
2016-12-21 08:33:47 +00:00
|
|
|
|
public async Task RemindTemplate([Remainder] string arg)
|
2016-09-30 02:20:09 +00:00
|
|
|
|
{
|
|
|
|
|
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)
|
2016-09-30 02:20:09 +00:00
|
|
|
|
{
|
|
|
|
|
uow.BotConfig.GetOrCreate().RemindMessageFormat = arg.Trim();
|
|
|
|
|
await uow.CompleteAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
2017-02-23 02:06:37 +00:00
|
|
|
|
|
|
|
|
|
await ReplyConfirmLocalized("remind_template").ConfigureAwait(false);
|
2016-09-30 02:20:09 +00:00
|
|
|
|
}
|
2016-08-25 22:05:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-21 14:44:31 +00:00
|
|
|
|
}
|