NadekoBot/NadekoBot.Modules.Utility/RepeatCommands.cs

236 lines
9.0 KiB
C#
Raw Normal View History

2016-08-28 12:19:50 +00:00
using Discord;
using Discord.Commands;
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;
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;
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
{
private readonly DiscordSocketClient _client;
private readonly DbService _db;
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;
}
[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)]
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)
return;
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
{
await ReplyErrorLocalized("repeat_invoke_none").ConfigureAwait(false);
2016-08-28 20:59:12 +00:00
return;
2016-08-28 12:19:50 +00:00
}
var repList = rep.ToList();
if (index >= repList.Count)
{
await ReplyErrorLocalized("index_out_of_range").ConfigureAwait(false);
return;
}
var repeater = repList[index].Repeater;
2017-02-09 20:11:33 +00:00
repList[index].Reset();
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
[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)]
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)
return;
if (index < 1)
return;
index -= 1;
2017-05-22 23:59:31 +00:00
if (!_service.Repeaters.TryGetValue(Context.Guild.Id, out var rep))
return;
var repeaterList = rep.ToList();
if (index >= repeaterList.Count)
2016-08-28 12:19:50 +00:00
{
await ReplyErrorLocalized("index_out_of_range").ConfigureAwait(false);
return;
}
var repeater = repeaterList[index];
repeater.Stop();
repeaterList.RemoveAt(index);
2017-05-22 23:59:31 +00:00
using (var uow = _db.UnitOfWork)
{
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);
2016-08-28 20:59:12 +00:00
}
2017-05-22 23:59:31 +00:00
if (_service.Repeaters.TryUpdate(Context.Guild.Id, new ConcurrentQueue<RepeatRunner>(repeaterList), rep))
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);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageMessages)]
[Priority(0)]
public async Task Repeat(int minutes, [Remainder] string message)
{
2017-05-22 23:59:31 +00:00
if (!_service.RepeaterReady)
return;
if (minutes < 1 || minutes > 10080)
return;
if (string.IsNullOrWhiteSpace(message))
return;
var toAdd = new GuildRepeater()
2016-08-28 12:19:50 +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)
{
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;
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-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
{
old.Enqueue(rep);
2016-08-29 12:34:46 +00:00
return old;
2016-08-28 12:19:50 +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()),
Format.Bold(rep.Repeater.Interval.Days.ToString()),
Format.Bold(rep.Repeater.Interval.Hours.ToString()),
Format.Bold(rep.Repeater.Interval.Minutes.ToString()))).ConfigureAwait(false);
}
[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
[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)
return;
2017-05-22 23:59:31 +00:00
if (!_service.Repeaters.TryGetValue(Context.Guild.Id, out var repRunners))
{
await ReplyConfirmLocalized("repeaters_none").ConfigureAwait(false);
return;
}
var replist = repRunners.ToList();
var sb = new StringBuilder();
for (var i = 0; i < replist.Count; i++)
{
var rep = replist[i];
sb.AppendLine($"`{i + 1}.` {rep}");
}
var desc = sb.ToString();
if (string.IsNullOrWhiteSpace(desc))
desc = GetText("no_active_repeaters");
await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
.WithTitle(GetText("list_of_repeaters"))
.WithDescription(desc))
.ConfigureAwait(false);
}
2016-08-28 20:59:12 +00:00
}
2016-08-28 12:19:50 +00:00
}
}