2017-03-20 09:53:04 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
2017-05-22 23:59:31 +00:00
|
|
|
|
using Discord.WebSocket;
|
2017-03-20 09:53:04 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using NadekoBot.Extensions;
|
2017-10-13 04:14:54 +00:00
|
|
|
|
using NadekoBot.Core.Services;
|
|
|
|
|
using NadekoBot.Core.Services.Database.Models;
|
2017-03-20 09:53:04 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Common.Attributes;
|
|
|
|
|
using NadekoBot.Modules.Utility.Services;
|
2017-03-20 09:53:04 +00:00
|
|
|
|
|
|
|
|
|
namespace NadekoBot.Modules.Utility
|
|
|
|
|
{
|
|
|
|
|
public partial class Utility
|
|
|
|
|
{
|
|
|
|
|
[Group]
|
2017-07-15 16:34:34 +00:00
|
|
|
|
public class CommandMapCommands : NadekoSubmodule<CommandMapService>
|
2017-03-20 09:53:04 +00:00
|
|
|
|
{
|
2017-05-29 04:13:22 +00:00
|
|
|
|
private readonly DbService _db;
|
2017-06-19 13:42:10 +00:00
|
|
|
|
private readonly DiscordSocketClient _client;
|
2017-03-20 09:53:04 +00:00
|
|
|
|
|
2017-07-15 16:34:34 +00:00
|
|
|
|
public CommandMapCommands(DbService db, DiscordSocketClient client)
|
2017-04-09 20:28:42 +00:00
|
|
|
|
{
|
2017-05-22 23:59:31 +00:00
|
|
|
|
_db = db;
|
|
|
|
|
_client = client;
|
2017-04-09 20:28:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-20 09:53:04 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
|
[RequireUserPermission(GuildPermission.Administrator)]
|
|
|
|
|
[RequireContext(ContextType.Guild)]
|
|
|
|
|
public async Task Alias(string trigger, [Remainder] string mapping = null)
|
|
|
|
|
{
|
|
|
|
|
var channel = (ITextChannel)Context.Channel;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(trigger))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
trigger = trigger.Trim().ToLowerInvariant();
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(mapping))
|
|
|
|
|
{
|
2017-07-25 16:31:30 +00:00
|
|
|
|
if (!_service.AliasMaps.TryGetValue(Context.Guild.Id, out var maps) ||
|
|
|
|
|
!maps.TryRemove(trigger, out _))
|
2017-03-20 09:53:04 +00:00
|
|
|
|
{
|
|
|
|
|
await ReplyErrorLocalized("alias_remove_fail", Format.Code(trigger)).ConfigureAwait(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-22 23:59:31 +00:00
|
|
|
|
using (var uow = _db.UnitOfWork)
|
2017-03-20 09:53:04 +00:00
|
|
|
|
{
|
|
|
|
|
var config = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.CommandAliases));
|
|
|
|
|
var toAdd = new CommandAlias()
|
|
|
|
|
{
|
|
|
|
|
Mapping = mapping,
|
|
|
|
|
Trigger = trigger
|
|
|
|
|
};
|
|
|
|
|
config.CommandAliases.RemoveWhere(x => x.Trigger == trigger);
|
|
|
|
|
uow.Complete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await ReplyConfirmLocalized("alias_removed", Format.Code(trigger)).ConfigureAwait(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-05-22 23:59:31 +00:00
|
|
|
|
_service.AliasMaps.AddOrUpdate(Context.Guild.Id, (_) =>
|
2017-03-20 09:53:04 +00:00
|
|
|
|
{
|
2017-05-22 23:59:31 +00:00
|
|
|
|
using (var uow = _db.UnitOfWork)
|
2017-03-20 09:53:04 +00:00
|
|
|
|
{
|
|
|
|
|
var config = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.CommandAliases));
|
|
|
|
|
config.CommandAliases.Add(new CommandAlias()
|
|
|
|
|
{
|
|
|
|
|
Mapping = mapping,
|
|
|
|
|
Trigger = trigger
|
|
|
|
|
});
|
|
|
|
|
uow.Complete();
|
|
|
|
|
}
|
|
|
|
|
return new ConcurrentDictionary<string, string>(new Dictionary<string, string>() {
|
|
|
|
|
{trigger.Trim().ToLowerInvariant(), mapping.ToLowerInvariant() },
|
|
|
|
|
});
|
|
|
|
|
}, (_, map) =>
|
|
|
|
|
{
|
2017-05-22 23:59:31 +00:00
|
|
|
|
using (var uow = _db.UnitOfWork)
|
2017-03-20 09:53:04 +00:00
|
|
|
|
{
|
|
|
|
|
var config = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.CommandAliases));
|
|
|
|
|
var toAdd = new CommandAlias()
|
|
|
|
|
{
|
|
|
|
|
Mapping = mapping,
|
|
|
|
|
Trigger = trigger
|
|
|
|
|
};
|
|
|
|
|
config.CommandAliases.RemoveWhere(x => x.Trigger == trigger);
|
|
|
|
|
config.CommandAliases.Add(toAdd);
|
|
|
|
|
uow.Complete();
|
|
|
|
|
}
|
|
|
|
|
map.AddOrUpdate(trigger, mapping, (key, old) => mapping);
|
|
|
|
|
return map;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await ReplyConfirmLocalized("alias_added", Format.Code(trigger), Format.Code(mapping)).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
|
[RequireContext(ContextType.Guild)]
|
|
|
|
|
public async Task AliasList(int page = 1)
|
|
|
|
|
{
|
|
|
|
|
var channel = (ITextChannel)Context.Channel;
|
|
|
|
|
page -= 1;
|
|
|
|
|
|
|
|
|
|
if (page < 0)
|
|
|
|
|
return;
|
2017-07-25 16:31:30 +00:00
|
|
|
|
|
|
|
|
|
if (!_service.AliasMaps.TryGetValue(Context.Guild.Id, out var maps) || !maps.Any())
|
2017-03-20 09:53:04 +00:00
|
|
|
|
{
|
|
|
|
|
await ReplyErrorLocalized("aliases_none").ConfigureAwait(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var arr = maps.ToArray();
|
|
|
|
|
|
2017-06-14 15:19:27 +00:00
|
|
|
|
await Context.Channel.SendPaginatedConfirmAsync(_client, page, (curPage) =>
|
2017-03-20 09:53:04 +00:00
|
|
|
|
{
|
|
|
|
|
return new EmbedBuilder().WithOkColor()
|
|
|
|
|
.WithTitle(GetText("alias_list"))
|
|
|
|
|
.WithDescription(string.Join("\n",
|
2017-06-14 15:19:27 +00:00
|
|
|
|
arr.Skip(curPage * 10).Take(10).Select(x => $"`{x.Key}` => `{x.Value}`")));
|
2017-03-20 09:53:04 +00:00
|
|
|
|
|
|
|
|
|
}, arr.Length / 10).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|