Reminders converted

This commit is contained in:
Kwoth 2016-08-26 00:05:29 +02:00
parent 26f79063c9
commit 6ca96d025d
11 changed files with 302 additions and 190 deletions

View File

@ -8,7 +8,7 @@ using NadekoBot.Services.Database.Impl;
namespace NadekoBot.Migrations
{
[DbContext(typeof(NadekoSqliteContext))]
[Migration("20160825172257_first")]
[Migration("20160825184527_first")]
partial class first
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -143,6 +143,28 @@ namespace NadekoBot.Migrations
b.ToTable("Quotes");
});
modelBuilder.Entity("NadekoBot.Services.Database.Models.Reminder", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<ulong>("ChannelId");
b.Property<bool>("IsPrivate");
b.Property<string>("Message");
b.Property<ulong>("ServerId");
b.Property<ulong>("UserId");
b.Property<DateTime>("When");
b.HasKey("Id");
b.ToTable("Reminders");
});
modelBuilder.Entity("NadekoBot.Services.Database.Models.ClashCaller", b =>
{
b.HasOne("NadekoBot.Services.Database.Models.ClashWar", "ClashWar")

View File

@ -84,6 +84,24 @@ namespace NadekoBot.Migrations
table.PrimaryKey("PK_Quotes", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Reminders",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("Autoincrement", true),
ChannelId = table.Column<ulong>(nullable: false),
IsPrivate = table.Column<bool>(nullable: false),
Message = table.Column<string>(nullable: true),
ServerId = table.Column<ulong>(nullable: false),
UserId = table.Column<ulong>(nullable: false),
When = table.Column<DateTime>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Reminders", x => x.Id);
});
migrationBuilder.CreateTable(
name: "ClashCallers",
columns: table => new
@ -139,6 +157,9 @@ namespace NadekoBot.Migrations
migrationBuilder.DropTable(
name: "Quotes");
migrationBuilder.DropTable(
name: "Reminders");
migrationBuilder.DropTable(
name: "ClashOfClans");
}

View File

@ -142,6 +142,28 @@ namespace NadekoBot.Migrations
b.ToTable("Quotes");
});
modelBuilder.Entity("NadekoBot.Services.Database.Models.Reminder", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<ulong>("ChannelId");
b.Property<bool>("IsPrivate");
b.Property<string>("Message");
b.Property<ulong>("ServerId");
b.Property<ulong>("UserId");
b.Property<DateTime>("When");
b.HasKey("Id");
b.ToTable("Reminders");
});
modelBuilder.Entity("NadekoBot.Services.Database.Models.ClashCaller", b =>
{
b.HasOne("NadekoBot.Services.Database.Models.ClashWar", "ClashWar")

View File

@ -1,197 +1,204 @@
//using Discord;
//using Discord.Commands;
//using NadekoBot.Classes;
//using NadekoBot.DataModels;
//using NadekoBot.Modules.Permissions.Classes;
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text.RegularExpressions;
//using System.Timers;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using NadekoBot.Attributes;
using NadekoBot.Services;
using NadekoBot.Services.Database;
using NadekoBot.Services.Database.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
//namespace NadekoBot.Modules.Utility
//{
// class Remind : DiscordCommand
// {
namespace NadekoBot.Modules.Utility
{
public partial class Utility
{
public class RemindCommands
{
// Regex regex = new Regex(@"^(?:(?<months>\d)mo)?(?:(?<weeks>\d)w)?(?:(?<days>\d{1,2})d)?(?:(?<hours>\d{1,2})h)?(?:(?<minutes>\d{1,2})m)?$",
// RegexOptions.Compiled | RegexOptions.Multiline);
Regex regex = new Regex(@"^(?:(?<months>\d)mo)?(?:(?<weeks>\d)w)?(?:(?<days>\d{1,2})d)?(?:(?<hours>\d{1,2})h)?(?:(?<minutes>\d{1,2})m)?$",
RegexOptions.Compiled | RegexOptions.Multiline);
// List<Timer> reminders = new List<Timer>();
IDictionary<string, Func<Reminder, string>> replacements = new Dictionary<string, Func<Reminder, string>>
{
{ "%message%" , (r) => r.Message },
{ "%user%", (r) => $"<@!{r.UserId}>" },
{ "%target%", (r) => r.IsPrivate ? "Direct Message" : $"<#{r.ChannelId}>"}
};
// IDictionary<string, Func<Reminder, string>> replacements = new Dictionary<string, Func<Reminder, string>>
// {
// { "%message%" , (r) => r.Message },
// { "%user%", (r) => $"<@!{r.UserId}>" },
// { "%target%", (r) => r.IsPrivate ? "Direct Message" : $"<#{r.ChannelId}>"}
// };
public RemindCommands()
{
List<Reminder> reminders;
using (var uow = DbHandler.UnitOfWork())
{
reminders = uow.Reminders.GetAll().ToList();
}
// public Remind(DiscordModule module) : base(module)
// {
// var remList = DbHandler.Instance.GetAllRows<Reminder>();
foreach (var r in reminders)
{
var t = StartReminder(r);
}
}
// reminders = remList.Select(StartNewReminder).ToList();
// }
private async Task StartReminder(Reminder r)
{
var now = DateTime.Now;
var twoMins = new TimeSpan(0, 2, 0);
TimeSpan time = (r.When - now) < twoMins
? twoMins //if the time is less than 2 minutes,
: r.When - now; //it will send the message 2 minutes after start
//To account for high bot startup times
if (time.TotalMilliseconds > int.MaxValue)
return;
// private Timer StartNewReminder(Reminder r)
// {
// var now = DateTime.Now;
// var twoMins = new TimeSpan(0, 2, 0);
// TimeSpan time = (r.When - now) < twoMins
// ? twoMins //if the time is less than 2 minutes,
// : r.When - now; //it will send the message 2 minutes after start
// //To account for high bot startup times
// if (time.TotalMilliseconds > int.MaxValue)
// return null;
// var t = new Timer(time.TotalMilliseconds);
// t.Elapsed += async (s, e) =>
// {
// try
// {
// Channel ch;
// if (r.IsPrivate)
// {
// ch = NadekoBot.Client.PrivateChannels.FirstOrDefault(c => (long)c.Id == r.ChannelId);
// if (ch == null)
// ch = await NadekoBot.Client.CreatePrivateChannel((ulong)r.ChannelId).ConfigureAwait(false);
// }
// else
// ch = NadekoBot.Client.GetServer((ulong)r.ServerId)?.GetChannel((ulong)r.ChannelId);
await Task.Delay(time);
try
{
IMessageChannel ch;
if (r.IsPrivate)
{
ch = await NadekoBot.Client.GetDMChannelAsync(r.ChannelId).ConfigureAwait(false);
}
else
{
ch = NadekoBot.Client.GetGuilds()
.Where(g => g.Id == r.ServerId)
.FirstOrDefault()
.GetTextChannels()
.Where(c => c.Id == r.ChannelId)
.FirstOrDefault();
}
if (ch == null)
return;
// if (ch == null)
// return;
await ch.SendMessageAsync(
replacements.Aggregate(NadekoBot.Config.RemindMessageFormat,
(cur, replace) => cur.Replace(replace.Key, replace.Value(r)))
).ConfigureAwait(false); //it works trust me
// await ch.SendMessageAsync(
// replacements.Aggregate(NadekoBot.Config.RemindMessageFormat,
// (cur, replace) => cur.Replace(replace.Key, replace.Value(r)))
// ).ConfigureAwait(false); //it works trust me
}
catch (Exception ex)
{
Console.WriteLine($"Timer error! {ex}");
}
finally
{
using (var uow = DbHandler.UnitOfWork())
{
uow.Reminders.Remove(r);
await uow.CompleteAsync();
}
}
}
// }
// catch (Exception ex)
// {
// Console.WriteLine($"Timer error! {ex}");
// }
// finally
// {
// DbHandler.Instance.Delete<Reminder>(r.Id.Value);
// t.Stop();
// t.Dispose();
// }
// };
// t.Start();
// return t;
// }
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
public async Task Remind(IMessage imsg, string meorchannel, string timeStr, [Remainder] string message)
{
var channel = (ITextChannel)imsg.Channel;
// internal override void Init(CommandGroupBuilder cgb)
// {
// cgb.CreateCommand(Module.Prefix + "remind")
// .Description("Sends a message to you or a channel after certain amount of time. " +
// "First argument is me/here/'channelname'. Second argument is time in a descending order (mo>w>d>h>m) example: 1w5d3h10m. " +
// "Third argument is a (multiword)message. " +
// $" | `{Prefix}remind me 1d5h Do something` or `{Prefix}remind #general Start now!`")
// .Parameter("meorchannel", ParameterType.Required)
// .Parameter("time", ParameterType.Required)
// .Parameter("message", ParameterType.Unparsed)
// .Do(async e =>
// {
// var meorchStr = meorchannel.ToUpperInvariant();
// Channel ch;
// bool isPrivate = false;
// if (meorchStr == "ME")
// {
// isPrivate = true;
// ch = await imsg.Author.CreatePMChannel().ConfigureAwait(false);
// }
// else if (meorchStr == "HERE")
// {
// ch = e.Channel;
// }
// else
// {
// ch = e.Server.FindChannels(meorchStr).FirstOrDefault();
// }
var meorchStr = meorchannel.ToUpperInvariant();
IMessageChannel ch;
bool isPrivate = false;
if (meorchStr == "ME")
{
isPrivate = true;
ch = await ((IGuildUser)imsg.Author).CreateDMChannelAsync().ConfigureAwait(false);
}
else if (meorchStr == "HERE")
{
ch = channel;
}
else
{
ch = channel.Guild.GetTextChannels().FirstOrDefault(c => c.Name == meorchStr || c.Id.ToString() == meorchStr);
}
// if (ch == null)
// {
// await channel.SendMessageAsync($"{imsg.Author.Mention} Something went wrong (channel cannot be found) ;(").ConfigureAwait(false);
// return;
// }
if (ch == null)
{
await channel.SendMessageAsync($"{imsg.Author.Mention} Something went wrong (channel cannot be found) ;(").ConfigureAwait(false);
return;
}
// var timeStr = time;
var m = regex.Match(timeStr);
// var m = regex.Match(timeStr);
if (m.Length == 0)
{
await channel.SendMessageAsync("Not a valid time format blablabla").ConfigureAwait(false);
return;
}
// if (m.Length == 0)
// {
// await channel.SendMessageAsync("Not a valid time format blablabla").ConfigureAwait(false);
// return;
// }
string output = "";
var namesAndValues = new Dictionary<string, int>();
// string output = "";
// var namesAndValues = new Dictionary<string, int>();
foreach (var groupName in regex.GetGroupNames())
{
if (groupName == "0") continue;
int value = 0;
int.TryParse(m.Groups[groupName].Value, out value);
// foreach (var groupName in regex.GetGroupNames())
// {
// if (groupName == "0") continue;
// int value = 0;
// int.TryParse(m.Groups[groupName].Value, out value);
if (string.IsNullOrEmpty(m.Groups[groupName].Value))
{
namesAndValues[groupName] = 0;
continue;
}
else if (value < 1 ||
(groupName == "months" && value > 1) ||
(groupName == "weeks" && value > 4) ||
(groupName == "days" && value >= 7) ||
(groupName == "hours" && value > 23) ||
(groupName == "minutes" && value > 59))
{
await channel.SendMessageAsync($"Invalid {groupName} value.").ConfigureAwait(false);
return;
}
else
namesAndValues[groupName] = value;
output += m.Groups[groupName].Value + " " + groupName + " ";
}
var time = DateTime.Now + new TimeSpan(30 * namesAndValues["months"] +
7 * namesAndValues["weeks"] +
namesAndValues["days"],
namesAndValues["hours"],
namesAndValues["minutes"],
0);
// if (string.IsNullOrEmpty(m.Groups[groupName].Value))
// {
// namesAndValues[groupName] = 0;
// continue;
// }
// else if (value < 1 ||
// (groupName == "months" && value > 1) ||
// (groupName == "weeks" && value > 4) ||
// (groupName == "days" && value >= 7) ||
// (groupName == "hours" && value > 23) ||
// (groupName == "minutes" && value > 59))
// {
// await channel.SendMessageAsync($"Invalid {groupName} value.").ConfigureAwait(false);
// return;
// }
// else
// namesAndValues[groupName] = value;
// output += m.Groups[groupName].Value + " " + groupName + " ";
// }
// var time = DateTime.Now + new TimeSpan(30 * namesAndValues["months"] +
// 7 * namesAndValues["weeks"] +
// namesAndValues["days"],
// namesAndValues["hours"],
// namesAndValues["minutes"],
// 0);
var rem = new Reminder
{
ChannelId = ch.Id,
IsPrivate = isPrivate,
When = time,
Message = message,
UserId = imsg.Author.Id,
ServerId = channel.Guild.Id
};
// var rem = new Reminder
// {
// ChannelId = (long)ch.Id,
// IsPrivate = isPrivate,
// When = time,
// Message = message,
// UserId = (long)imsg.Author.Id,
// ServerId = (long)e.Server.Id
// };
// DbHandler.Instance.Connection.Insert(rem);
using (var uow = DbHandler.UnitOfWork())
{
uow.Reminders.Add(rem);
}
// reminders.Add(StartNewReminder(rem));
await channel.SendMessageAsync($"⏰ I will remind \"{(ch is ITextChannel ? ((ITextChannel)ch).Name : imsg.Author.Username)}\" to \"{message.ToString()}\" in {output}. ({time:d.M.yyyy.} at {time:HH:mm})").ConfigureAwait(false);
}
// await channel.SendMessageAsync($"⏰ I will remind \"{ch.Name}\" to \"{message.ToString()}\" in {output}. ({time:d.M.yyyy.} at {time:HH:mm})").ConfigureAwait(false);
// });
// cgb.CreateCommand(Module.Prefix + "remindmsg")
// .Description("Sets message for when the remind is triggered. " +
// " Available placeholders are %user% - user who ran the command, %message% -" +
// $" Message specified in the remind, %target% - target channel of the remind. **Bot Owner Only!** | `{Prefix}remindmsg do something else`")
// .Parameter("msg", ParameterType.Unparsed)
// .AddCheck(SimpleCheckers.OwnerOnly())
// .Do(async e =>
// {
// var arg = msg?.Trim();
// if (string.IsNullOrWhiteSpace(arg))
// return;
////todo owner only
//[LocalizedCommand, LocalizedDescription, LocalizedSummary]
//[RequireContext(ContextType.Guild)]
//public async Task RemindTemplate(IMessage imsg, [Remainder] string arg)
//{
// var channel = (ITextChannel)imsg.Channel;
// NadekoBot.Config.RemindMessageFormat = arg;
// await channel.SendMessageAsync("`New remind message set.`");
// });
// }
// }
//}
// arg = arg?.Trim();
// if (string.IsNullOrWhiteSpace(arg))
// return;
// NadekoBot.Config.RemindMessageFormat = arg;
// await channel.SendMessageAsync("`New remind message set.`");
//}
}
}
}

View File

@ -13,6 +13,7 @@ namespace NadekoBot.Services.Database
IConfigRepository GuildConfigs { get; }
IDonatorsRepository Donators { get; }
IClashOfClansRepository ClashOfClans { get; }
IReminderRepository Reminders { get; }
int Complete();
Task<int> CompleteAsync();

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Services.Database.Models
{
public class Reminder : DbEntity
{
public DateTime When { get; set; }
public ulong ChannelId { get; set; }
public ulong ServerId { get; set; }
public ulong UserId { get; set; }
public string Message { get; set; }
public bool IsPrivate { get; set; }
}
}

View File

@ -15,6 +15,7 @@ namespace NadekoBot.Services.Database
public DbSet<GuildConfig> GuildConfigs { get; set; }
public DbSet<ClashWar> ClashOfClans { get; set; }
public DbSet<ClashCaller> ClashCallers { get; set; }
public DbSet<Reminder> Reminders { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{

View File

@ -0,0 +1,14 @@
using NadekoBot.Services.Database.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Services.Database.Repositories
{
public interface IReminderRepository : IRepository<Reminder>
{
}
}

View File

@ -0,0 +1,17 @@
using NadekoBot.Services.Database.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace NadekoBot.Services.Database.Repositories.Impl
{
public class ReminderRepository : Repository<Reminder>, IReminderRepository
{
public ReminderRepository(DbContext context) : base(context)
{
}
}
}

View File

@ -24,6 +24,9 @@ namespace NadekoBot.Services.Database
private IClashOfClansRepository _clashOfClans;
public IClashOfClansRepository ClashOfClans => _clashOfClans ?? (_clashOfClans = new ClashOfClansRepository(_context));
private IReminderRepository _reminders;
public IReminderRepository Reminders => _reminders ?? (_reminders = new ReminderRepository(_context));
public UnitOfWork(NadekoContext context)
{
_context = context;

View File

@ -1,14 +0,0 @@
using System;
namespace NadekoBot.DataModels
{
class Reminder : IDataModel
{
public DateTime When { get; set; }
public long ChannelId { get; set; }
public long ServerId { get; set; }
public long UserId { get; set; }
public string Message { get; set; }
public bool IsPrivate { get; set; }
}
}