.repeat, .repinv, .replst, .reprm added/improved

This commit is contained in:
Kwoth 2017-01-10 00:52:25 +01:00
parent 00978c293c
commit a48fdd1fdc
4 changed files with 179 additions and 55 deletions

View File

@ -1,14 +1,18 @@
using Discord;
using Discord.Commands;
using Microsoft.EntityFrameworkCore;
using NadekoBot.Attributes;
using NadekoBot.Extensions;
using NadekoBot.Services;
using NadekoBot.Services.Database;
using NadekoBot.Services.Database.Models;
using NLog;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
@ -19,7 +23,8 @@ namespace NadekoBot.Modules.Administration
[Group]
public class RepeatCommands : ModuleBase
{
public static ConcurrentDictionary<ulong, RepeatRunner> repeaters { get; }
//guildid/RepeatRunners
public static ConcurrentDictionary<ulong, ConcurrentQueue<RepeatRunner>> repeaters { get; }
public class RepeatRunner
{
@ -83,7 +88,10 @@ namespace NadekoBot.Modules.Administration
var sw = Stopwatch.StartNew();
using (var uow = DbHandler.UnitOfWork())
{
repeaters = new ConcurrentDictionary<ulong, RepeatRunner>(uow.Repeaters.GetAll().Select(r => new RepeatRunner(r)).Where(r => r != null).ToDictionary(r => r.Repeater.ChannelId));
repeaters = new ConcurrentDictionary<ulong, ConcurrentQueue<RepeatRunner>>(NadekoBot.AllGuildConfigs
.ToDictionary(gc => gc.GuildId,
gc => new ConcurrentQueue<RepeatRunner>(gc.GuildRepeaters.Select(gr => new RepeatRunner(gr))
.Where(gr => gr.Channel != null))));
}
sw.Stop();
@ -93,41 +101,70 @@ namespace NadekoBot.Modules.Administration
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.ManageMessages)]
public async Task RepeatInvoke()
public async Task RepeatInvoke(int index)
{
RepeatRunner rep;
index -= 1;
ConcurrentQueue<RepeatRunner> rep;
if (!repeaters.TryGetValue(Context.Channel.Id, out rep))
{
await Context.Channel.SendErrorAsync(" **No repeating message found on this server.**").ConfigureAwait(false);
return;
}
rep.Reset();
await Context.Channel.SendMessageAsync("🔄 " + rep.Repeater.Message).ConfigureAwait(false);
var repList = rep.ToList();
if (index >= repList.Count)
{
await Context.Channel.SendErrorAsync("Index out of range.").ConfigureAwait(false);
return;
}
var repeater = repList[index].Repeater;
await Context.Channel.SendMessageAsync("🔄 " + repeater.Message).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.ManageMessages)]
public async Task Repeat()
[Priority(0)]
public async Task RepeatRemove(int index)
{
RepeatRunner rep;
if (repeaters.TryRemove(Context.Channel.Id, out rep))
if (index < 1)
return;
index -= 1;
ConcurrentQueue<RepeatRunner> rep;
if (!repeaters.TryGetValue(Context.Guild.Id, out rep))
return;
var repeaterList = rep.ToList();
if (index >= repeaterList.Count)
{
await Context.Channel.SendErrorAsync("Index out of range.").ConfigureAwait(false);
return;
}
var repeater = repeaterList[index];
repeater.Stop();
repeaterList.RemoveAt(index);
using (var uow = DbHandler.UnitOfWork())
{
uow.Repeaters.Remove(rep.Repeater);
await uow.CompleteAsync();
var guildConfig = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(gc => gc.GuildRepeaters));
guildConfig.GuildRepeaters.RemoveWhere(r=>r.Id == repeater.Repeater.Id);
await uow.CompleteAsync().ConfigureAwait(false);
}
rep.Stop();
if (repeaters.TryUpdate(Context.Guild.Id, new ConcurrentQueue<RepeatRunner>(repeaterList), rep))
await Context.Channel.SendConfirmAsync("✅ **Stopped repeating a message.**").ConfigureAwait(false);
}
else
await Context.Channel.SendConfirmAsync(" **No message is repeating.**").ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.ManageMessages)]
[Priority(1)]
public async Task Repeat(int minutes, [Remainder] string message)
{
if (minutes < 1 || minutes > 10080)
@ -136,38 +173,52 @@ namespace NadekoBot.Modules.Administration
if (string.IsNullOrWhiteSpace(message))
return;
RepeatRunner rep;
rep = repeaters.AddOrUpdate(Context.Channel.Id, (cid) =>
{
using (var uow = DbHandler.UnitOfWork())
{
var localRep = new Repeater
var toAdd = new Repeater()
{
ChannelId = Context.Channel.Id,
GuildId = Context.Guild.Id,
Interval = TimeSpan.FromMinutes(minutes),
Message = message,
Message = message
};
uow.Repeaters.Add(localRep);
uow.Complete();
return new RepeatRunner(localRep, (ITextChannel)Context.Channel);
}
}, (cid, old) =>
var rep = new RepeatRunner(toAdd, (ITextChannel)Context.Channel);
repeaters.AddOrUpdate(Context.Guild.Id, new ConcurrentQueue<RepeatRunner>(new[] { rep }), (key, old) =>
{
using (var uow = DbHandler.UnitOfWork())
{
old.Repeater.Message = message;
old.Repeater.Interval = TimeSpan.FromMinutes(minutes);
uow.Repeaters.Update(old.Repeater);
uow.Complete();
}
old.Reset();
old.Enqueue(rep);
return old;
});
await Context.Channel.SendConfirmAsync($"🔁 Repeating **\"{rep.Repeater.Message}\"** every `{rep.Repeater.Interval.Days} day(s), {rep.Repeater.Interval.Hours} hour(s) and {rep.Repeater.Interval.Minutes} minute(s)`.").ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.ManageMessages)]
public async Task RepeatList()
{
ConcurrentQueue<RepeatRunner> repRunners;
if (!repeaters.TryGetValue(Context.Guild.Id, out repRunners))
{
await Context.Channel.SendConfirmAsync("No repeaters running on this server.").ConfigureAwait(false);
return;
}
var replist = repRunners.ToList();
var sb = new StringBuilder();
for (int i = 0; i < replist.Count; i++)
{
var rep = replist[i];
sb.AppendLine($"`{i + 1}.` {rep.Channel.Mention} | {(int)rep.Repeater.Interval.TotalHours}:{rep.Repeater.Interval:mm} | {rep.Repeater.Message.TrimTo(20)}");
}
await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
.WithTitle("List Of Repeaters")
.WithDescription(sb.ToString()))
.ConfigureAwait(false);
}
}
}
}

View File

@ -5712,7 +5712,7 @@ namespace NadekoBot.Resources {
}
/// <summary>
/// Looks up a localized string similar to Repeat a message every X minutes. If no parameters are specified, repeat is disabled..
/// Looks up a localized string similar to Repeat a message every X minutes in the current channel..
/// </summary>
public static string repeat_desc {
get {
@ -5739,7 +5739,7 @@ namespace NadekoBot.Resources {
}
/// <summary>
/// Looks up a localized string similar to Immediately shows the repeat message and restarts the timer..
/// Looks up a localized string similar to Immediately shows the repeat message on a certain index and restarts its timer..
/// </summary>
public static string repeatinvoke_desc {
get {
@ -5748,7 +5748,7 @@ namespace NadekoBot.Resources {
}
/// <summary>
/// Looks up a localized string similar to `{0}repinv`.
/// Looks up a localized string similar to `{0}repinv 1`.
/// </summary>
public static string repeatinvoke_usage {
get {
@ -5756,6 +5756,33 @@ namespace NadekoBot.Resources {
}
}
/// <summary>
/// Looks up a localized string similar to repeatlist replst.
/// </summary>
public static string repeatlist_cmd {
get {
return ResourceManager.GetString("repeatlist_cmd", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Shows currently repeating messages and their indexes..
/// </summary>
public static string repeatlist_desc {
get {
return ResourceManager.GetString("repeatlist_desc", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to `{0}repeatlist`.
/// </summary>
public static string repeatlist_usage {
get {
return ResourceManager.GetString("repeatlist_usage", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to rpeatplaylst rpl.
/// </summary>
@ -5783,6 +5810,33 @@ namespace NadekoBot.Resources {
}
}
/// <summary>
/// Looks up a localized string similar to repeatremove reprm.
/// </summary>
public static string repeatremove_cmd {
get {
return ResourceManager.GetString("repeatremove_cmd", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Removes a repeating message on a specified index. Use `{0}repeatlist` to see indexes..
/// </summary>
public static string repeatremove_desc {
get {
return ResourceManager.GetString("repeatremove_desc", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to `{0}reprm 2`.
/// </summary>
public static string repeatremove_usage {
get {
return ResourceManager.GetString("repeatremove_usage", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to reptcursong rcs.
/// </summary>

View File

@ -265,16 +265,16 @@
<value>repeatinvoke repinv</value>
</data>
<data name="repeatinvoke_desc" xml:space="preserve">
<value>Immediately shows the repeat message and restarts the timer.</value>
<value>Immediately shows the repeat message on a certain index and restarts its timer.</value>
</data>
<data name="repeatinvoke_usage" xml:space="preserve">
<value>`{0}repinv`</value>
<value>`{0}repinv 1`</value>
</data>
<data name="repeat_cmd" xml:space="preserve">
<value>repeat</value>
</data>
<data name="repeat_desc" xml:space="preserve">
<value>Repeat a message every X minutes. If no parameters are specified, repeat is disabled.</value>
<value>Repeat a message every X minutes in the current channel.</value>
</data>
<data name="repeat_usage" xml:space="preserve">
<value>`{0}repeat 5 Hello there`</value>
@ -2889,4 +2889,22 @@
<data name="pollstats_usage" xml:space="preserve">
<value>`{0}pollstats`</value>
</data>
<data name="repeatlist_cmd" xml:space="preserve">
<value>repeatlist replst</value>
</data>
<data name="repeatlist_desc" xml:space="preserve">
<value>Shows currently repeating messages and their indexes.</value>
</data>
<data name="repeatlist_usage" xml:space="preserve">
<value>`{0}repeatlist`</value>
</data>
<data name="repeatremove_cmd" xml:space="preserve">
<value>repeatremove reprm</value>
</data>
<data name="repeatremove_desc" xml:space="preserve">
<value>Removes a repeating message on a specified index. Use `{0}repeatlist` to see indexes.</value>
</data>
<data name="repeatremove_usage" xml:space="preserve">
<value>`{0}reprm 2`</value>
</data>
</root>

View File

@ -58,6 +58,7 @@ namespace NadekoBot.Services.Database.Models
public string MuteRoleName { get; set; }
public bool CleverbotEnabled { get; set; }
public HashSet<Repeater> GuildRepeaters { get; set; }
}
public class FilterChannelId : DbEntity