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 namespace NadekoBot.Migrations
{ {
[DbContext(typeof(NadekoSqliteContext))] [DbContext(typeof(NadekoSqliteContext))]
[Migration("20160825172257_first")] [Migration("20160825184527_first")]
partial class first partial class first
{ {
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -143,6 +143,28 @@ namespace NadekoBot.Migrations
b.ToTable("Quotes"); 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 => modelBuilder.Entity("NadekoBot.Services.Database.Models.ClashCaller", b =>
{ {
b.HasOne("NadekoBot.Services.Database.Models.ClashWar", "ClashWar") b.HasOne("NadekoBot.Services.Database.Models.ClashWar", "ClashWar")

View File

@ -84,6 +84,24 @@ namespace NadekoBot.Migrations
table.PrimaryKey("PK_Quotes", x => x.Id); 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( migrationBuilder.CreateTable(
name: "ClashCallers", name: "ClashCallers",
columns: table => new columns: table => new
@ -139,6 +157,9 @@ namespace NadekoBot.Migrations
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Quotes"); name: "Quotes");
migrationBuilder.DropTable(
name: "Reminders");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "ClashOfClans"); name: "ClashOfClans");
} }

View File

@ -142,6 +142,28 @@ namespace NadekoBot.Migrations
b.ToTable("Quotes"); 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 => modelBuilder.Entity("NadekoBot.Services.Database.Models.ClashCaller", b =>
{ {
b.HasOne("NadekoBot.Services.Database.Models.ClashWar", "ClashWar") b.HasOne("NadekoBot.Services.Database.Models.ClashWar", "ClashWar")

View File

@ -1,197 +1,204 @@
//using Discord; using Discord;
//using Discord.Commands; using Discord.Commands;
//using NadekoBot.Classes; using Discord.WebSocket;
//using NadekoBot.DataModels; using NadekoBot.Attributes;
//using NadekoBot.Modules.Permissions.Classes; using NadekoBot.Services;
//using System; using NadekoBot.Services.Database;
//using System.Collections.Generic; using NadekoBot.Services.Database.Models;
//using System.Linq; using System;
//using System.Text.RegularExpressions; using System.Collections.Generic;
//using System.Timers; using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
//namespace NadekoBot.Modules.Utility namespace NadekoBot.Modules.Utility
//{ {
// class Remind : DiscordCommand 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)?$", 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); 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>> public RemindCommands()
// { {
// { "%message%" , (r) => r.Message }, List<Reminder> reminders;
// { "%user%", (r) => $"<@!{r.UserId}>" }, using (var uow = DbHandler.UnitOfWork())
// { "%target%", (r) => r.IsPrivate ? "Direct Message" : $"<#{r.ChannelId}>"} {
// }; reminders = uow.Reminders.GetAll().ToList();
}
// public Remind(DiscordModule module) : base(module) foreach (var r in reminders)
// { {
// var remList = DbHandler.Instance.GetAllRows<Reminder>(); 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) await Task.Delay(time);
// { try
// var now = DateTime.Now; {
// var twoMins = new TimeSpan(0, 2, 0); IMessageChannel ch;
// TimeSpan time = (r.When - now) < twoMins if (r.IsPrivate)
// ? twoMins //if the time is less than 2 minutes, {
// : r.When - now; //it will send the message 2 minutes after start ch = await NadekoBot.Client.GetDMChannelAsync(r.ChannelId).ConfigureAwait(false);
// //To account for high bot startup times }
// if (time.TotalMilliseconds > int.MaxValue) else
// return null; {
// var t = new Timer(time.TotalMilliseconds); ch = NadekoBot.Client.GetGuilds()
// t.Elapsed += async (s, e) => .Where(g => g.Id == r.ServerId)
// { .FirstOrDefault()
// try .GetTextChannels()
// { .Where(c => c.Id == r.ChannelId)
// Channel ch; .FirstOrDefault();
// if (r.IsPrivate) }
// { if (ch == null)
// ch = NadekoBot.Client.PrivateChannels.FirstOrDefault(c => (long)c.Id == r.ChannelId); return;
// 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);
// if (ch == null) await ch.SendMessageAsync(
// return; 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, catch (Exception ex)
// (cur, replace) => cur.Replace(replace.Key, replace.Value(r))) {
// ).ConfigureAwait(false); //it works trust me Console.WriteLine($"Timer error! {ex}");
}
finally
{
using (var uow = DbHandler.UnitOfWork())
{
uow.Reminders.Remove(r);
await uow.CompleteAsync();
}
}
}
// } [LocalizedCommand, LocalizedDescription, LocalizedSummary]
// catch (Exception ex) [RequireContext(ContextType.Guild)]
// { public async Task Remind(IMessage imsg, string meorchannel, string timeStr, [Remainder] string message)
// Console.WriteLine($"Timer error! {ex}"); {
// } var channel = (ITextChannel)imsg.Channel;
// finally
// {
// DbHandler.Instance.Delete<Reminder>(r.Id.Value);
// t.Stop();
// t.Dispose();
// }
// };
// t.Start();
// return t;
// }
// internal override void Init(CommandGroupBuilder cgb) var meorchStr = meorchannel.ToUpperInvariant();
// { IMessageChannel ch;
// cgb.CreateCommand(Module.Prefix + "remind") bool isPrivate = false;
// .Description("Sends a message to you or a channel after certain amount of time. " + if (meorchStr == "ME")
// "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. " + isPrivate = true;
// $" | `{Prefix}remind me 1d5h Do something` or `{Prefix}remind #general Start now!`") ch = await ((IGuildUser)imsg.Author).CreateDMChannelAsync().ConfigureAwait(false);
// .Parameter("meorchannel", ParameterType.Required) }
// .Parameter("time", ParameterType.Required) else if (meorchStr == "HERE")
// .Parameter("message", ParameterType.Unparsed) {
// .Do(async e => ch = channel;
// { }
// var meorchStr = meorchannel.ToUpperInvariant(); else
// Channel ch; {
// bool isPrivate = false; ch = channel.Guild.GetTextChannels().FirstOrDefault(c => c.Name == meorchStr || c.Id.ToString() == meorchStr);
// 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();
// }
// if (ch == null) if (ch == null)
{
await channel.SendMessageAsync($"{imsg.Author.Mention} Something went wrong (channel cannot be found) ;(").ConfigureAwait(false);
return;
}
var m = regex.Match(timeStr);
if (m.Length == 0)
{
await channel.SendMessageAsync("Not a valid time format blablabla").ConfigureAwait(false);
return;
}
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);
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
};
using (var uow = DbHandler.UnitOfWork())
{
uow.Reminders.Add(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);
}
////todo owner only
//[LocalizedCommand, LocalizedDescription, LocalizedSummary]
//[RequireContext(ContextType.Guild)]
//public async Task RemindTemplate(IMessage imsg, [Remainder] string arg)
//{ //{
// await channel.SendMessageAsync($"{imsg.Author.Mention} Something went wrong (channel cannot be found) ;(").ConfigureAwait(false); // var channel = (ITextChannel)imsg.Channel;
// return;
// }
// var timeStr = time;
// var m = regex.Match(timeStr); // arg = arg?.Trim();
// if (m.Length == 0)
// {
// await channel.SendMessageAsync("Not a valid time format blablabla").ConfigureAwait(false);
// return;
// }
// 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);
// 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 = (long)ch.Id,
// IsPrivate = isPrivate,
// When = time,
// Message = message,
// UserId = (long)imsg.Author.Id,
// ServerId = (long)e.Server.Id
// };
// DbHandler.Instance.Connection.Insert(rem);
// reminders.Add(StartNewReminder(rem));
// 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)) // if (string.IsNullOrWhiteSpace(arg))
// return; // return;
// NadekoBot.Config.RemindMessageFormat = arg; // NadekoBot.Config.RemindMessageFormat = arg;
// await channel.SendMessageAsync("`New remind message set.`"); // await channel.SendMessageAsync("`New remind message set.`");
// });
// }
// }
//} //}
}
}
}

View File

@ -13,6 +13,7 @@ namespace NadekoBot.Services.Database
IConfigRepository GuildConfigs { get; } IConfigRepository GuildConfigs { get; }
IDonatorsRepository Donators { get; } IDonatorsRepository Donators { get; }
IClashOfClansRepository ClashOfClans { get; } IClashOfClansRepository ClashOfClans { get; }
IReminderRepository Reminders { get; }
int Complete(); int Complete();
Task<int> CompleteAsync(); 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<GuildConfig> GuildConfigs { get; set; }
public DbSet<ClashWar> ClashOfClans { get; set; } public DbSet<ClashWar> ClashOfClans { get; set; }
public DbSet<ClashCaller> ClashCallers { get; set; } public DbSet<ClashCaller> ClashCallers { get; set; }
public DbSet<Reminder> Reminders { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder) 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; private IClashOfClansRepository _clashOfClans;
public IClashOfClansRepository ClashOfClans => _clashOfClans ?? (_clashOfClans = new ClashOfClansRepository(_context)); public IClashOfClansRepository ClashOfClans => _clashOfClans ?? (_clashOfClans = new ClashOfClansRepository(_context));
private IReminderRepository _reminders;
public IReminderRepository Reminders => _reminders ?? (_reminders = new ReminderRepository(_context));
public UnitOfWork(NadekoContext context) public UnitOfWork(NadekoContext context)
{ {
_context = 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; }
}
}