2016-08-28 12:19:50 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
2017-01-09 23:52:25 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2016-12-11 16:18:25 +00:00
|
|
|
|
using NadekoBot.Extensions;
|
2016-08-28 20:59:12 +00:00
|
|
|
|
using NadekoBot.Services;
|
|
|
|
|
using NadekoBot.Services.Database.Models;
|
2016-08-28 12:19:50 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
2016-08-28 20:59:12 +00:00
|
|
|
|
using System.Linq;
|
2017-01-09 23:52:25 +00:00
|
|
|
|
using System.Text;
|
2016-08-28 12:19:50 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2017-02-27 12:23:48 +00:00
|
|
|
|
using Discord.WebSocket;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Common.Attributes;
|
|
|
|
|
using NadekoBot.Common.TypeReaders;
|
|
|
|
|
using NadekoBot.Modules.Utility.Common;
|
|
|
|
|
using NadekoBot.Modules.Utility.Services;
|
2016-03-08 21:42:43 +00:00
|
|
|
|
|
2017-01-11 13:08:50 +00:00
|
|
|
|
namespace NadekoBot.Modules.Utility
|
2016-08-28 12:19:50 +00:00
|
|
|
|
{
|
2017-01-11 13:08:50 +00:00
|
|
|
|
public partial class Utility
|
2016-08-28 12:19:50 +00:00
|
|
|
|
{
|
2016-08-28 20:59:12 +00:00
|
|
|
|
[Group]
|
2017-07-15 16:34:34 +00:00
|
|
|
|
public class RepeatCommands : NadekoSubmodule<MessageRepeaterService>
|
2016-08-28 20:59:12 +00:00
|
|
|
|
{
|
2017-06-19 13:42:10 +00:00
|
|
|
|
private readonly DiscordSocketClient _client;
|
2017-05-29 04:13:22 +00:00
|
|
|
|
private readonly DbService _db;
|
2017-02-23 02:06:37 +00:00
|
|
|
|
|
2017-07-15 16:34:34 +00:00
|
|
|
|
public RepeatCommands(DiscordSocketClient client, DbService db)
|
2016-08-28 12:19:50 +00:00
|
|
|
|
{
|
2017-05-22 23:59:31 +00:00
|
|
|
|
_client = client;
|
|
|
|
|
_db = db;
|
2017-04-09 20:28:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-05 03:09:44 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2016-08-28 20:59:12 +00:00
|
|
|
|
[RequireContext(ContextType.Guild)]
|
2016-12-16 18:43:57 +00:00
|
|
|
|
[RequireUserPermission(GuildPermission.ManageMessages)]
|
2017-01-09 23:52:25 +00:00
|
|
|
|
public async Task RepeatInvoke(int index)
|
2016-08-28 12:19:50 +00:00
|
|
|
|
{
|
2017-05-22 23:59:31 +00:00
|
|
|
|
if (!_service.RepeaterReady)
|
2017-02-23 02:06:37 +00:00
|
|
|
|
return;
|
2017-01-09 23:52:25 +00:00
|
|
|
|
index -= 1;
|
2017-05-22 23:59:31 +00:00
|
|
|
|
if (!_service.Repeaters.TryGetValue(Context.Guild.Id, out var rep))
|
2016-08-28 12:19:50 +00:00
|
|
|
|
{
|
2017-02-23 02:06:37 +00:00
|
|
|
|
await ReplyErrorLocalized("repeat_invoke_none").ConfigureAwait(false);
|
2016-08-28 20:59:12 +00:00
|
|
|
|
return;
|
2016-08-28 12:19:50 +00:00
|
|
|
|
}
|
2017-01-09 23:52:25 +00:00
|
|
|
|
|
|
|
|
|
var repList = rep.ToList();
|
|
|
|
|
|
|
|
|
|
if (index >= repList.Count)
|
|
|
|
|
{
|
2017-02-23 02:06:37 +00:00
|
|
|
|
await ReplyErrorLocalized("index_out_of_range").ConfigureAwait(false);
|
2017-01-09 23:52:25 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var repeater = repList[index].Repeater;
|
2017-02-09 20:11:33 +00:00
|
|
|
|
repList[index].Reset();
|
2017-04-10 00:53:36 +00:00
|
|
|
|
await repList[index].Trigger();
|
|
|
|
|
|
|
|
|
|
await Context.Channel.SendMessageAsync("🔄").ConfigureAwait(false);
|
2016-08-28 12:19:50 +00:00
|
|
|
|
}
|
2016-05-23 20:50:16 +00:00
|
|
|
|
|
2016-10-05 03:09:44 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2016-08-28 20:59:12 +00:00
|
|
|
|
[RequireContext(ContextType.Guild)]
|
2016-12-16 18:43:57 +00:00
|
|
|
|
[RequireUserPermission(GuildPermission.ManageMessages)]
|
2017-01-09 23:52:25 +00:00
|
|
|
|
public async Task RepeatRemove(int index)
|
2016-08-28 20:59:12 +00:00
|
|
|
|
{
|
2017-05-22 23:59:31 +00:00
|
|
|
|
if (!_service.RepeaterReady)
|
2017-02-23 02:06:37 +00:00
|
|
|
|
return;
|
2017-01-09 23:52:25 +00:00
|
|
|
|
if (index < 1)
|
|
|
|
|
return;
|
|
|
|
|
index -= 1;
|
2017-05-22 23:59:31 +00:00
|
|
|
|
|
|
|
|
|
if (!_service.Repeaters.TryGetValue(Context.Guild.Id, out var rep))
|
2017-01-09 23:52:25 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var repeaterList = rep.ToList();
|
|
|
|
|
|
|
|
|
|
if (index >= repeaterList.Count)
|
2016-08-28 12:19:50 +00:00
|
|
|
|
{
|
2017-02-23 02:06:37 +00:00
|
|
|
|
await ReplyErrorLocalized("index_out_of_range").ConfigureAwait(false);
|
2017-01-09 23:52:25 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var repeater = repeaterList[index];
|
|
|
|
|
repeater.Stop();
|
|
|
|
|
repeaterList.RemoveAt(index);
|
|
|
|
|
|
2017-05-22 23:59:31 +00:00
|
|
|
|
using (var uow = _db.UnitOfWork)
|
2017-01-09 23:52:25 +00:00
|
|
|
|
{
|
|
|
|
|
var guildConfig = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(gc => gc.GuildRepeaters));
|
|
|
|
|
|
2017-01-10 12:37:35 +00:00
|
|
|
|
guildConfig.GuildRepeaters.RemoveWhere(r => r.Id == repeater.Repeater.Id);
|
2017-01-09 23:52:25 +00:00
|
|
|
|
await uow.CompleteAsync().ConfigureAwait(false);
|
2016-08-28 20:59:12 +00:00
|
|
|
|
}
|
2017-01-09 23:52:25 +00:00
|
|
|
|
|
2017-05-22 23:59:31 +00:00
|
|
|
|
if (_service.Repeaters.TryUpdate(Context.Guild.Id, new ConcurrentQueue<RepeatRunner>(repeaterList), rep))
|
2017-02-23 02:06:37 +00:00
|
|
|
|
await Context.Channel.SendConfirmAsync(GetText("message_repeater"),
|
2017-02-27 12:23:48 +00:00
|
|
|
|
GetText("repeater_stopped", index + 1) + $"\n\n{repeater}").ConfigureAwait(false);
|
2016-08-29 22:00:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-05 03:09:44 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2016-08-29 22:00:19 +00:00
|
|
|
|
[RequireContext(ContextType.Guild)]
|
2016-12-16 18:43:57 +00:00
|
|
|
|
[RequireUserPermission(GuildPermission.ManageMessages)]
|
2017-07-15 03:04:16 +00:00
|
|
|
|
[Priority(0)]
|
2016-12-21 08:33:47 +00:00
|
|
|
|
public async Task Repeat(int minutes, [Remainder] string message)
|
2016-08-29 22:00:19 +00:00
|
|
|
|
{
|
2017-05-22 23:59:31 +00:00
|
|
|
|
if (!_service.RepeaterReady)
|
2017-02-23 02:06:37 +00:00
|
|
|
|
return;
|
2016-10-17 10:26:26 +00:00
|
|
|
|
if (minutes < 1 || minutes > 10080)
|
2016-08-29 22:00:19 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(message))
|
|
|
|
|
return;
|
|
|
|
|
|
2017-01-10 12:37:35 +00:00
|
|
|
|
var toAdd = new GuildRepeater()
|
2016-08-28 12:19:50 +00:00
|
|
|
|
{
|
2017-01-09 23:52:25 +00:00
|
|
|
|
ChannelId = Context.Channel.Id,
|
|
|
|
|
GuildId = Context.Guild.Id,
|
|
|
|
|
Interval = TimeSpan.FromMinutes(minutes),
|
|
|
|
|
Message = message
|
|
|
|
|
};
|
|
|
|
|
|
2017-05-22 23:59:31 +00:00
|
|
|
|
using (var uow = _db.UnitOfWork)
|
2017-01-10 12:37:35 +00:00
|
|
|
|
{
|
|
|
|
|
var gc = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.GuildRepeaters));
|
|
|
|
|
|
2017-01-10 22:12:52 +00:00
|
|
|
|
if (gc.GuildRepeaters.Count >= 5)
|
|
|
|
|
return;
|
2017-01-10 12:37:35 +00:00
|
|
|
|
gc.GuildRepeaters.Add(toAdd);
|
|
|
|
|
|
|
|
|
|
await uow.CompleteAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-26 16:19:39 +00:00
|
|
|
|
var rep = new RepeatRunner(_client, (SocketGuild)Context.Guild, toAdd);
|
2017-01-09 23:52:25 +00:00
|
|
|
|
|
2017-05-22 23:59:31 +00:00
|
|
|
|
_service.Repeaters.AddOrUpdate(Context.Guild.Id, new ConcurrentQueue<RepeatRunner>(new[] {rep}), (key, old) =>
|
2016-08-28 20:59:12 +00:00
|
|
|
|
{
|
2017-01-09 23:52:25 +00:00
|
|
|
|
old.Enqueue(rep);
|
2016-08-29 12:34:46 +00:00
|
|
|
|
return old;
|
2016-08-28 12:19:50 +00:00
|
|
|
|
});
|
2017-01-10 12:37:35 +00:00
|
|
|
|
|
2017-02-23 02:06:37 +00:00
|
|
|
|
await Context.Channel.SendConfirmAsync(
|
|
|
|
|
"🔁 " + GetText("repeater",
|
2017-03-14 20:02:31 +00:00
|
|
|
|
Format.Bold(((IGuildUser)Context.User).GuildPermissions.MentionEveryone ? rep.Repeater.Message : rep.Repeater.Message.SanitizeMentions()),
|
2017-02-23 02:06:37 +00:00
|
|
|
|
Format.Bold(rep.Repeater.Interval.Days.ToString()),
|
|
|
|
|
Format.Bold(rep.Repeater.Interval.Hours.ToString()),
|
|
|
|
|
Format.Bold(rep.Repeater.Interval.Minutes.ToString()))).ConfigureAwait(false);
|
2016-08-29 22:00:19 +00:00
|
|
|
|
}
|
2017-01-09 23:52:25 +00:00
|
|
|
|
|
2017-10-09 00:52:46 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
|
[RequireContext(ContextType.Guild)]
|
|
|
|
|
[RequireUserPermission(GuildPermission.ManageMessages)]
|
|
|
|
|
[Priority(1)]
|
|
|
|
|
public async Task Repeat(GuildDateTime gt, [Remainder] string message)
|
|
|
|
|
{
|
|
|
|
|
if (!_service.RepeaterReady)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(message))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var toAdd = new GuildRepeater()
|
|
|
|
|
{
|
|
|
|
|
ChannelId = Context.Channel.Id,
|
|
|
|
|
GuildId = Context.Guild.Id,
|
|
|
|
|
Interval = TimeSpan.FromHours(24),
|
|
|
|
|
StartTimeOfDay = gt.InputTimeUtc.TimeOfDay,
|
|
|
|
|
Message = message
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using (var uow = _db.UnitOfWork)
|
|
|
|
|
{
|
|
|
|
|
var gc = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.GuildRepeaters));
|
|
|
|
|
|
|
|
|
|
if (gc.GuildRepeaters.Count >= 5)
|
|
|
|
|
return;
|
|
|
|
|
gc.GuildRepeaters.Add(toAdd);
|
|
|
|
|
|
|
|
|
|
await uow.CompleteAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var rep = new RepeatRunner(_client, (SocketGuild)Context.Guild, toAdd);
|
|
|
|
|
|
|
|
|
|
_service.Repeaters.AddOrUpdate(Context.Guild.Id, new ConcurrentQueue<RepeatRunner>(new[] { rep }), (key, old) =>
|
|
|
|
|
{
|
|
|
|
|
old.Enqueue(rep);
|
|
|
|
|
return old;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var secondPart = GetText("repeater_initial",
|
|
|
|
|
Format.Bold(rep.InitialInterval.Hours.ToString()),
|
|
|
|
|
Format.Bold(rep.InitialInterval.Minutes.ToString()));
|
|
|
|
|
|
|
|
|
|
await Context.Channel.SendConfirmAsync(
|
|
|
|
|
"🔁 " + GetText("repeater",
|
|
|
|
|
Format.Bold(((IGuildUser)Context.User).GuildPermissions.MentionEveryone ? rep.Repeater.Message : rep.Repeater.Message.SanitizeMentions()),
|
|
|
|
|
Format.Bold(rep.Repeater.Interval.Days.ToString()),
|
|
|
|
|
Format.Bold(rep.Repeater.Interval.Hours.ToString()),
|
|
|
|
|
Format.Bold(rep.Repeater.Interval.Minutes.ToString())) + " " + secondPart).ConfigureAwait(false);
|
|
|
|
|
}
|
2017-06-12 23:40:39 +00:00
|
|
|
|
|
2017-01-09 23:52:25 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
|
[RequireContext(ContextType.Guild)]
|
|
|
|
|
[RequireUserPermission(GuildPermission.ManageMessages)]
|
|
|
|
|
public async Task RepeatList()
|
|
|
|
|
{
|
2017-05-22 23:59:31 +00:00
|
|
|
|
if (!_service.RepeaterReady)
|
2017-02-23 02:06:37 +00:00
|
|
|
|
return;
|
2017-05-22 23:59:31 +00:00
|
|
|
|
if (!_service.Repeaters.TryGetValue(Context.Guild.Id, out var repRunners))
|
2017-01-09 23:52:25 +00:00
|
|
|
|
{
|
2017-02-23 02:06:37 +00:00
|
|
|
|
await ReplyConfirmLocalized("repeaters_none").ConfigureAwait(false);
|
2017-01-09 23:52:25 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var replist = repRunners.ToList();
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
|
2017-02-23 02:06:37 +00:00
|
|
|
|
for (var i = 0; i < replist.Count; i++)
|
2017-01-09 23:52:25 +00:00
|
|
|
|
{
|
|
|
|
|
var rep = replist[i];
|
|
|
|
|
|
2017-02-23 02:06:37 +00:00
|
|
|
|
sb.AppendLine($"`{i + 1}.` {rep}");
|
2017-01-09 23:52:25 +00:00
|
|
|
|
}
|
2017-02-23 02:06:37 +00:00
|
|
|
|
var desc = sb.ToString();
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(desc))
|
|
|
|
|
desc = GetText("no_active_repeaters");
|
2017-01-09 23:52:25 +00:00
|
|
|
|
|
|
|
|
|
await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
|
2017-02-23 02:06:37 +00:00
|
|
|
|
.WithTitle(GetText("list_of_repeaters"))
|
|
|
|
|
.WithDescription(desc))
|
|
|
|
|
.ConfigureAwait(false);
|
2017-01-09 23:52:25 +00:00
|
|
|
|
}
|
2016-08-28 20:59:12 +00:00
|
|
|
|
}
|
2016-08-28 12:19:50 +00:00
|
|
|
|
}
|
2017-01-10 12:37:35 +00:00
|
|
|
|
}
|