Command aliasing added - .alias and .aliases commands

This commit is contained in:
Kwoth 2017-03-20 10:53:04 +01:00
parent 0bc839ebe3
commit f680c476a6
11 changed files with 1689 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Migrations;
namespace NadekoBot.Migrations
{
public partial class commandaliasing : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "CommandAlias",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("Sqlite:Autoincrement", true),
DateAdded = table.Column<DateTime>(nullable: true),
GuildConfigId = table.Column<int>(nullable: true),
Mapping = table.Column<string>(nullable: true),
Trigger = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_CommandAlias", x => x.Id);
table.ForeignKey(
name: "FK_CommandAlias_GuildConfigs_GuildConfigId",
column: x => x.GuildConfigId,
principalTable: "GuildConfigs",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex(
name: "IX_CommandAlias_GuildConfigId",
table: "CommandAlias",
column: "GuildConfigId");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "CommandAlias");
}
}
}

View File

@ -207,6 +207,26 @@ namespace NadekoBot.Migrations
b.ToTable("ClashOfClans");
});
modelBuilder.Entity("NadekoBot.Services.Database.Models.CommandAlias", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<DateTime?>("DateAdded");
b.Property<int?>("GuildConfigId");
b.Property<string>("Mapping");
b.Property<string>("Trigger");
b.HasKey("Id");
b.HasIndex("GuildConfigId");
b.ToTable("CommandAlias");
});
modelBuilder.Entity("NadekoBot.Services.Database.Models.CommandCooldown", b =>
{
b.Property<int>("Id")
@ -1078,6 +1098,13 @@ namespace NadekoBot.Migrations
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("NadekoBot.Services.Database.Models.CommandAlias", b =>
{
b.HasOne("NadekoBot.Services.Database.Models.GuildConfig")
.WithMany("CommandAliases")
.HasForeignKey("GuildConfigId");
});
modelBuilder.Entity("NadekoBot.Services.Database.Models.CommandCooldown", b =>
{
b.HasOne("NadekoBot.Services.Database.Models.GuildConfig")

View File

@ -0,0 +1,149 @@
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 System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System;
namespace NadekoBot.Modules.Utility
{
public partial class Utility
{
public class CommandAliasEqualityComparer : IEqualityComparer<CommandAlias>
{
public bool Equals(CommandAlias x, CommandAlias y) => x.Trigger == y.Trigger;
public int GetHashCode(CommandAlias obj) => obj.Trigger.GetHashCode();
}
[Group]
public class CommandMapCommands : NadekoSubmodule
{
//guildId, (trigger, mapping)
public static ConcurrentDictionary<ulong, ConcurrentDictionary<string, string>> AliasMaps { get; } = new ConcurrentDictionary<ulong, ConcurrentDictionary<string, string>>();
static CommandMapCommands()
{
var eq = new CommandAliasEqualityComparer();
AliasMaps = new ConcurrentDictionary<ulong, ConcurrentDictionary<string, string>>(
NadekoBot.AllGuildConfigs.ToDictionary(
x => x.GuildId,
x => new ConcurrentDictionary<string, string>(x.CommandAliases
.Distinct(eq)
.ToDictionary(ca => ca.Trigger, ca => ca.Mapping))));
}
[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))
{
ConcurrentDictionary<string, string> maps;
string throwaway;
if (!AliasMaps.TryGetValue(Context.Guild.Id, out maps) ||
!maps.TryRemove(trigger, out throwaway))
{
await ReplyErrorLocalized("alias_remove_fail", Format.Code(trigger)).ConfigureAwait(false);
return;
}
using (var uow = DbHandler.UnitOfWork())
{
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;
}
AliasMaps.AddOrUpdate(Context.Guild.Id, (_) =>
{
using (var uow = DbHandler.UnitOfWork())
{
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) =>
{
using (var uow = DbHandler.UnitOfWork())
{
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;
ConcurrentDictionary<string, string> maps;
if (!AliasMaps.TryGetValue(Context.Guild.Id, out maps) || !maps.Any())
{
await ReplyErrorLocalized("aliases_none").ConfigureAwait(false);
return;
}
var arr = maps.ToArray();
await Context.Channel.SendPaginatedConfirmAsync(page + 1, (curPage) =>
{
return new EmbedBuilder().WithOkColor()
.WithTitle(GetText("alias_list"))
.WithDescription(string.Join("\n",
arr.Skip((curPage - 1) * 10).Take(10).Select(x => $"`{x.Key}` => `{x.Value}`")));
}, arr.Length / 10).ConfigureAwait(false);
}
}
}
}

View File

@ -248,6 +248,60 @@ namespace NadekoBot.Resources {
}
}
/// <summary>
/// Looks up a localized string similar to alias cmdmap.
/// </summary>
public static string alias_cmd {
get {
return ResourceManager.GetString("alias_cmd", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Create a custom alias for a certain Nadeko command. Provide no alias to remove the existing one..
/// </summary>
public static string alias_desc {
get {
return ResourceManager.GetString("alias_desc", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to `{0}alias allin $bf 100 h` or `{0}alias &quot;linux thingy&quot; &gt;loonix Spyware Windows`.
/// </summary>
public static string alias_usage {
get {
return ResourceManager.GetString("alias_usage", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to aliaslist cmdmaplist aliases.
/// </summary>
public static string aliaslist_cmd {
get {
return ResourceManager.GetString("aliaslist_cmd", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Shows the list of currently set aliases. Paginated..
/// </summary>
public static string aliaslist_desc {
get {
return ResourceManager.GetString("aliaslist_desc", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to `{0}aliaslist` or `{0}aliaslist 3`.
/// </summary>
public static string aliaslist_usage {
get {
return ResourceManager.GetString("aliaslist_usage", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to allchnlmdls acm.
/// </summary>

View File

@ -3204,4 +3204,22 @@
<data name="crdm_usage" xml:space="preserve">
<value>`{0}crad 44`</value>
</data>
<data name="aliaslist_cmd" xml:space="preserve">
<value>aliaslist cmdmaplist aliases</value>
</data>
<data name="aliaslist_desc" xml:space="preserve">
<value>Shows the list of currently set aliases. Paginated.</value>
</data>
<data name="aliaslist_usage" xml:space="preserve">
<value>`{0}aliaslist` or `{0}aliaslist 3`</value>
</data>
<data name="alias_cmd" xml:space="preserve">
<value>alias cmdmap</value>
</data>
<data name="alias_desc" xml:space="preserve">
<value>Create a custom alias for a certain Nadeko command. Provide no alias to remove the existing one.</value>
</data>
<data name="alias_usage" xml:space="preserve">
<value>`{0}alias allin $bf 100 h` or `{0}alias "linux thingy" &gt;loonix Spyware Windows`</value>
</data>
</root>

View File

@ -5656,6 +5656,51 @@ namespace NadekoBot.Resources {
}
}
/// <summary>
/// Looks up a localized string similar to Typing {0} will now be an alias of {1}..
/// </summary>
public static string utility_alias_added {
get {
return ResourceManager.GetString("utility_alias_added", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to List of aliases.
/// </summary>
public static string utility_alias_list {
get {
return ResourceManager.GetString("utility_alias_list", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Trigger {0} didn&apos;t have an alias..
/// </summary>
public static string utility_alias_remove_fail {
get {
return ResourceManager.GetString("utility_alias_remove_fail", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Trigger {0} no longer has an alias..
/// </summary>
public static string utility_alias_removed {
get {
return ResourceManager.GetString("utility_alias_removed", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to No alias found.
/// </summary>
public static string utility_aliases_none {
get {
return ResourceManager.GetString("utility_aliases_none", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Author.
/// </summary>

View File

@ -2260,4 +2260,19 @@ Owner ID: {2}</value>
<data name="customreactions_crdm_enabled" xml:space="preserve">
<value>Response message for the custom reaction with id {0} will be sent as a DM.</value>
</data>
<data name="utility_aliases_none" xml:space="preserve">
<value>No alias found</value>
</data>
<data name="utility_alias_added" xml:space="preserve">
<value>Typing {0} will now be an alias of {1}.</value>
</data>
<data name="utility_alias_list" xml:space="preserve">
<value>List of aliases</value>
</data>
<data name="utility_alias_removed" xml:space="preserve">
<value>Trigger {0} no longer has an alias.</value>
</data>
<data name="utility_alias_remove_fail" xml:space="preserve">
<value>Trigger {0} didn't have an alias.</value>
</data>
</root>

View File

@ -320,6 +320,26 @@ namespace NadekoBot.Services
var exec3 = Environment.TickCount - execTime;
string messageContent = usrMsg.Content;
if (guild != null)
{
ConcurrentDictionary<string, string> maps;
if (Modules.Utility.Utility.CommandMapCommands.AliasMaps.TryGetValue(guild.Id, out maps))
{
string newMessageContent;
if (maps.TryGetValue(messageContent.Trim().ToLowerInvariant(), out newMessageContent))
{
_log.Info(@"--Mapping Command--
GuildId: {0}
Trigger: {1}
Mapping: {2}", guild.Id, messageContent, newMessageContent);
var oldMessageContent = messageContent;
messageContent = newMessageContent;
try { await usrMsg.Channel.SendConfirmAsync($"{oldMessageContent} => {newMessageContent}").ConfigureAwait(false); } catch { }
}
}
}
// execute the command and measure the time it took
var exec = await Task.Run(() => ExecuteCommand(new CommandContext(_client, usrMsg), messageContent, DependencyMap.Empty, MultiMatchHandling.Best)).ConfigureAwait(false);

View File

@ -71,10 +71,34 @@ namespace NadekoBot.Services.Database.Models
public HashSet<UnmuteTimer> UnmuteTimers { get; set; } = new HashSet<UnmuteTimer>();
public HashSet<VcRoleInfo> VcRoleInfos { get; set; }
public HashSet<CommandAlias> CommandAliases { get; set; } = new HashSet<CommandAlias>();
//public List<ProtectionIgnoredChannel> ProtectionIgnoredChannels { get; set; } = new List<ProtectionIgnoredChannel>();
}
public class CommandAlias : DbEntity
{
public string Trigger { get; set; }
public string Mapping { get; set; }
//// override object.Equals
//public override bool Equals(object obj)
//{
// if (obj == null || GetType() != obj.GetType())
// {
// return false;
// }
// return ((CommandAlias)obj).Trigger.Trim().ToLowerInvariant() == Trigger.Trim().ToLowerInvariant();
//}
//// override object.GetHashCode
//public override int GetHashCode()
//{
// return Trigger.Trim().ToLowerInvariant().GetHashCode();
//}
}
public class VcRoleInfo : DbEntity
{
public ulong VoiceChannelId { get; set; }

View File

@ -16,6 +16,7 @@ namespace NadekoBot.Services.Database.Repositories.Impl
_set.Include(gc => gc.LogSetting)
.ThenInclude(ls => ls.IgnoredChannels)
.Include(gc => gc.MutedUsers)
.Include(gc => gc.CommandAliases)
.Include(gc => gc.UnmuteTimers)
.Include(gc => gc.VcRoleInfos)
.Include(gc => gc.GenerateCurrencyChannelIds)