From 4277c703cb8246b94df32a28197f1f0b97152cfa Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Mon, 24 Aug 2020 15:18:10 +0200 Subject: [PATCH] Set up skeleton for CustomCommands --- ChaosBot/Discord/Services/CommandHandler.cs | 5 +- .../Discord/Services/CustomCommandHandler.cs | 47 +++++ .../20200824124830_CustomCommand.Designer.cs | 182 ++++++++++++++++++ .../20200824124830_CustomCommand.cs | 32 +++ .../ChaosbotContextModelSnapshot.cs | 19 ++ ChaosBot/Models/ChaosbotContext.cs | 3 + ChaosBot/Models/CustomCommand.cs | 27 +++ 7 files changed, 314 insertions(+), 1 deletion(-) create mode 100644 ChaosBot/Discord/Services/CustomCommandHandler.cs create mode 100644 ChaosBot/Migrations/20200824124830_CustomCommand.Designer.cs create mode 100644 ChaosBot/Migrations/20200824124830_CustomCommand.cs create mode 100644 ChaosBot/Models/CustomCommand.cs diff --git a/ChaosBot/Discord/Services/CommandHandler.cs b/ChaosBot/Discord/Services/CommandHandler.cs index f2d2290..73841d7 100644 --- a/ChaosBot/Discord/Services/CommandHandler.cs +++ b/ChaosBot/Discord/Services/CommandHandler.cs @@ -76,8 +76,11 @@ namespace ChaosBot.Discord.Services if(Convert.ToBoolean(ConfigurationRepository.GetValue("Experience:Commands", context.Guild.Id, "false"))) ExperienceHandler.AddXp(context); + + bool customCommandExecuted = await CustomCommandHandler.CheckCommand(context, argPos); - await _commands.ExecuteAsync(context, argPos, _services); + if (!customCommandExecuted) + await _commands.ExecuteAsync(context, argPos, _services); } catch (Exception ex) { diff --git a/ChaosBot/Discord/Services/CustomCommandHandler.cs b/ChaosBot/Discord/Services/CustomCommandHandler.cs new file mode 100644 index 0000000..536b8de --- /dev/null +++ b/ChaosBot/Discord/Services/CustomCommandHandler.cs @@ -0,0 +1,47 @@ +using System; +using System.Linq; +using System.Reflection; +using System.Threading.Tasks; +using ChaosBot.Models; +using Discord.Commands; +using NLog; + +namespace ChaosBot.Discord.Services +{ + public static class CustomCommandHandler + { + private static readonly ILogger Logger = Program.Logger; + + public static async Task CheckCommand(SocketCommandContext context, int argPos) + { + try + { + await using ChaosbotContext dbContext = new ChaosbotContext(); + string command = context.Message.Content.Substring(argPos); + + IQueryable customCommandQuery = dbContext.CustomCommands; + CustomCommand customCommand = customCommandQuery + .Where(cc => cc.DiscordGuildId == context.Guild.Id) + .FirstOrDefault(cc => command.StartsWith((string) cc.Command)); + + if (customCommand == null) return false; + + if (customCommand.Type == CustomCommandType.Basic) + { + await context.Channel.SendMessageAsync(customCommand.Content); + } + else + { + throw new NotImplementedException($"No support for command type ${customCommand.Type}"); + } + + return true; + } + catch (Exception ex) + { + Logger.Error($"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + return false; + } + } +} \ No newline at end of file diff --git a/ChaosBot/Migrations/20200824124830_CustomCommand.Designer.cs b/ChaosBot/Migrations/20200824124830_CustomCommand.Designer.cs new file mode 100644 index 0000000..f256454 --- /dev/null +++ b/ChaosBot/Migrations/20200824124830_CustomCommand.Designer.cs @@ -0,0 +1,182 @@ +// +using System; +using ChaosBot.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +namespace ChaosBot.Migrations +{ + [DbContext(typeof(ChaosbotContext))] + [Migration("20200824124830_CustomCommand")] + partial class CustomCommand + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "3.1.6") + .HasAnnotation("Relational:MaxIdentifierLength", 64); + + modelBuilder.Entity("ChaosBot.Models.CommandPermission", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint unsigned"); + + b.Property("Command") + .IsRequired() + .HasColumnType("varchar(128) CHARACTER SET utf8mb4") + .HasMaxLength(128); + + b.Property("DiscordGuildId") + .HasColumnType("bigint unsigned"); + + b.Property("TargetId") + .HasColumnType("bigint unsigned"); + + b.Property("TargetType") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("CommandPermissions"); + }); + + modelBuilder.Entity("ChaosBot.Models.Configuration", b => + { + b.Property("DiscordGuildId") + .HasColumnType("bigint unsigned"); + + b.Property("Key") + .HasColumnType("varchar(128) CHARACTER SET utf8mb4") + .HasMaxLength(128); + + b.Property("SerializedValue") + .IsRequired() + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.HasKey("DiscordGuildId", "Key"); + + b.ToTable("Configuration"); + }); + + modelBuilder.Entity("ChaosBot.Models.Experience", b => + { + b.Property("DiscordGuildId") + .HasColumnType("bigint unsigned"); + + b.Property("DiscordUserId") + .HasColumnType("bigint unsigned"); + + b.Property("Amount") + .HasColumnType("bigint unsigned"); + + b.Property("LastUpdated") + .HasColumnType("datetime"); + + b.Property("Level") + .HasColumnType("bigint unsigned"); + + b.HasKey("DiscordGuildId", "DiscordUserId"); + + b.ToTable("ExperiencePoints"); + }); + + modelBuilder.Entity("ChaosBot.Models.LodestoneCharacter", b => + { + b.Property("DiscordGuildId") + .HasColumnType("bigint unsigned"); + + b.Property("LodestoneId") + .HasColumnType("bigint unsigned"); + + b.Property("Avatar") + .IsRequired() + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("DiscordUserId") + .HasColumnType("bigint unsigned"); + + b.Property("Name") + .IsRequired() + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.HasKey("DiscordGuildId", "LodestoneId"); + + b.ToTable("LodestoneCharacter"); + }); + + modelBuilder.Entity("ChaosBot.Models.LodestoneFreeCompany", b => + { + b.Property("DiscordGuildId") + .HasColumnType("bigint unsigned"); + + b.Property("LodestoneId") + .HasColumnType("bigint unsigned"); + + b.Property("Name") + .IsRequired() + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.HasKey("DiscordGuildId", "LodestoneId"); + + b.ToTable("LodestoneFreeCompany"); + }); + + modelBuilder.Entity("ChaosBot.Models.Point", b => + { + b.Property("DiscordGuildId") + .HasColumnType("bigint unsigned"); + + b.Property("DiscordUserId") + .HasColumnType("bigint unsigned"); + + b.Property("Amount") + .HasColumnType("bigint unsigned"); + + b.HasKey("DiscordGuildId", "DiscordUserId"); + + b.ToTable("Points"); + }); + + modelBuilder.Entity("ChaosBot.Models.Raffle", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint unsigned"); + + b.Property("DiscordGuildId") + .HasColumnType("bigint unsigned"); + + b.Property("DiscordUserId") + .HasColumnType("bigint unsigned"); + + b.HasKey("Id"); + + b.ToTable("Raffles"); + }); + + modelBuilder.Entity("ChaosBot.Models.RoleReaction", b => + { + b.Property("DiscordGuildId") + .HasColumnType("bigint unsigned"); + + b.Property("DiscordMessageId") + .HasColumnType("bigint unsigned"); + + b.Property("DiscordRoleId") + .HasColumnType("bigint unsigned"); + + b.Property("DiscordEmoteNameEncoded") + .HasColumnType("varchar(95) CHARACTER SET utf8mb4"); + + b.HasKey("DiscordGuildId", "DiscordMessageId", "DiscordRoleId", "DiscordEmoteNameEncoded"); + + b.ToTable("RoleReactions"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/ChaosBot/Migrations/20200824124830_CustomCommand.cs b/ChaosBot/Migrations/20200824124830_CustomCommand.cs new file mode 100644 index 0000000..ec5acb0 --- /dev/null +++ b/ChaosBot/Migrations/20200824124830_CustomCommand.cs @@ -0,0 +1,32 @@ +using ChaosBot.Models; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace ChaosBot.Migrations +{ + public partial class CustomCommand : Migration + { + private readonly string Table = "CustomCommands"; + + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: Table, + columns: table => new + { + DiscordGuildId = table.Column(nullable: false), + Command = table.Column(nullable: false, maxLength: 128), + Type = table.Column(nullable: false), + Content = table.Column(nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_CustomCommands", x => new { x.DiscordGuildId, x.Command }); + }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable(name: Table); + } + } +} diff --git a/ChaosBot/Migrations/ChaosbotContextModelSnapshot.cs b/ChaosBot/Migrations/ChaosbotContextModelSnapshot.cs index 449c819..9abe71a 100644 --- a/ChaosBot/Migrations/ChaosbotContextModelSnapshot.cs +++ b/ChaosBot/Migrations/ChaosbotContextModelSnapshot.cs @@ -155,6 +155,25 @@ namespace ChaosBot.Migrations b.ToTable("Raffles"); }); + + modelBuilder.Entity("ChaosBot.Models.RoleReaction", b => + { + b.Property("DiscordGuildId") + .HasColumnType("bigint unsigned"); + + b.Property("DiscordMessageId") + .HasColumnType("bigint unsigned"); + + b.Property("DiscordRoleId") + .HasColumnType("bigint unsigned"); + + b.Property("DiscordEmoteNameEncoded") + .HasColumnType("varchar(95) CHARACTER SET utf8mb4"); + + b.HasKey("DiscordGuildId", "DiscordMessageId", "DiscordRoleId", "DiscordEmoteNameEncoded"); + + b.ToTable("RoleReactions"); + }); #pragma warning restore 612, 618 } } diff --git a/ChaosBot/Models/ChaosbotContext.cs b/ChaosBot/Models/ChaosbotContext.cs index bb6c866..ced2e6f 100644 --- a/ChaosBot/Models/ChaosbotContext.cs +++ b/ChaosBot/Models/ChaosbotContext.cs @@ -13,6 +13,7 @@ namespace ChaosBot.Models public DbSet Configuration { get; set; } public DbSet ExperiencePoints { get; set; } public DbSet RoleReactions { get; set; } + public DbSet CustomCommands { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { @@ -49,6 +50,8 @@ namespace ChaosBot.Models .HasKey(x => new {x.DiscordGuildId, x.Key}); modelBuilder.Entity() .HasKey(x => new {x.DiscordGuildId, x.DiscordMessageId, x.DiscordRoleId, x.DiscordEmoteNameEncoded}); + modelBuilder.Entity() + .HasKey(x => new {x.DiscordGuildId, x.Command}); } } } \ No newline at end of file diff --git a/ChaosBot/Models/CustomCommand.cs b/ChaosBot/Models/CustomCommand.cs new file mode 100644 index 0000000..d6fa2bb --- /dev/null +++ b/ChaosBot/Models/CustomCommand.cs @@ -0,0 +1,27 @@ +using System; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Text; + +namespace ChaosBot.Models +{ + #region Required + public class CustomCommand + { + [Required] + public ulong DiscordGuildId { get; set; } + [Required] + [MaxLength(128)] + public string Command { get; set; } + [Required] + public CustomCommandType Type { get; set; } + [Required] + public string Content { get; set; } + } + #endregion + + public enum CustomCommandType + { + Basic + } +} \ No newline at end of file