Set up skeleton for CustomCommands

This commit is contained in:
Daniel_I_Am 2020-08-24 15:18:10 +02:00
parent c0f7357cc9
commit 4277c703cb
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84
7 changed files with 314 additions and 1 deletions

View File

@ -77,7 +77,10 @@ namespace ChaosBot.Discord.Services
if(Convert.ToBoolean(ConfigurationRepository.GetValue<string>("Experience:Commands", context.Guild.Id, "false"))) if(Convert.ToBoolean(ConfigurationRepository.GetValue<string>("Experience:Commands", context.Guild.Id, "false")))
ExperienceHandler.AddXp(context); ExperienceHandler.AddXp(context);
await _commands.ExecuteAsync(context, argPos, _services); bool customCommandExecuted = await CustomCommandHandler.CheckCommand(context, argPos);
if (!customCommandExecuted)
await _commands.ExecuteAsync(context, argPos, _services);
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@ -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<bool> CheckCommand(SocketCommandContext context, int argPos)
{
try
{
await using ChaosbotContext dbContext = new ChaosbotContext();
string command = context.Message.Content.Substring(argPos);
IQueryable<CustomCommand> 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;
}
}
}

View File

@ -0,0 +1,182 @@
// <auto-generated />
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<ulong>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint unsigned");
b.Property<string>("Command")
.IsRequired()
.HasColumnType("varchar(128) CHARACTER SET utf8mb4")
.HasMaxLength(128);
b.Property<ulong>("DiscordGuildId")
.HasColumnType("bigint unsigned");
b.Property<ulong>("TargetId")
.HasColumnType("bigint unsigned");
b.Property<int>("TargetType")
.HasColumnType("int");
b.HasKey("Id");
b.ToTable("CommandPermissions");
});
modelBuilder.Entity("ChaosBot.Models.Configuration", b =>
{
b.Property<ulong>("DiscordGuildId")
.HasColumnType("bigint unsigned");
b.Property<string>("Key")
.HasColumnType("varchar(128) CHARACTER SET utf8mb4")
.HasMaxLength(128);
b.Property<string>("SerializedValue")
.IsRequired()
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.HasKey("DiscordGuildId", "Key");
b.ToTable("Configuration");
});
modelBuilder.Entity("ChaosBot.Models.Experience", b =>
{
b.Property<ulong>("DiscordGuildId")
.HasColumnType("bigint unsigned");
b.Property<ulong>("DiscordUserId")
.HasColumnType("bigint unsigned");
b.Property<ulong>("Amount")
.HasColumnType("bigint unsigned");
b.Property<DateTime>("LastUpdated")
.HasColumnType("datetime");
b.Property<ulong>("Level")
.HasColumnType("bigint unsigned");
b.HasKey("DiscordGuildId", "DiscordUserId");
b.ToTable("ExperiencePoints");
});
modelBuilder.Entity("ChaosBot.Models.LodestoneCharacter", b =>
{
b.Property<ulong>("DiscordGuildId")
.HasColumnType("bigint unsigned");
b.Property<ulong>("LodestoneId")
.HasColumnType("bigint unsigned");
b.Property<string>("Avatar")
.IsRequired()
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.Property<ulong>("DiscordUserId")
.HasColumnType("bigint unsigned");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.HasKey("DiscordGuildId", "LodestoneId");
b.ToTable("LodestoneCharacter");
});
modelBuilder.Entity("ChaosBot.Models.LodestoneFreeCompany", b =>
{
b.Property<ulong>("DiscordGuildId")
.HasColumnType("bigint unsigned");
b.Property<ulong>("LodestoneId")
.HasColumnType("bigint unsigned");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.HasKey("DiscordGuildId", "LodestoneId");
b.ToTable("LodestoneFreeCompany");
});
modelBuilder.Entity("ChaosBot.Models.Point", b =>
{
b.Property<ulong>("DiscordGuildId")
.HasColumnType("bigint unsigned");
b.Property<ulong>("DiscordUserId")
.HasColumnType("bigint unsigned");
b.Property<ulong>("Amount")
.HasColumnType("bigint unsigned");
b.HasKey("DiscordGuildId", "DiscordUserId");
b.ToTable("Points");
});
modelBuilder.Entity("ChaosBot.Models.Raffle", b =>
{
b.Property<ulong>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint unsigned");
b.Property<ulong>("DiscordGuildId")
.HasColumnType("bigint unsigned");
b.Property<ulong>("DiscordUserId")
.HasColumnType("bigint unsigned");
b.HasKey("Id");
b.ToTable("Raffles");
});
modelBuilder.Entity("ChaosBot.Models.RoleReaction", b =>
{
b.Property<ulong>("DiscordGuildId")
.HasColumnType("bigint unsigned");
b.Property<ulong>("DiscordMessageId")
.HasColumnType("bigint unsigned");
b.Property<ulong>("DiscordRoleId")
.HasColumnType("bigint unsigned");
b.Property<string>("DiscordEmoteNameEncoded")
.HasColumnType("varchar(95) CHARACTER SET utf8mb4");
b.HasKey("DiscordGuildId", "DiscordMessageId", "DiscordRoleId", "DiscordEmoteNameEncoded");
b.ToTable("RoleReactions");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -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<ulong>(nullable: false),
Command = table.Column<string>(nullable: false, maxLength: 128),
Type = table.Column<CustomCommandType>(nullable: false),
Content = table.Column<string>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_CustomCommands", x => new { x.DiscordGuildId, x.Command });
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(name: Table);
}
}
}

View File

@ -155,6 +155,25 @@ namespace ChaosBot.Migrations
b.ToTable("Raffles"); b.ToTable("Raffles");
}); });
modelBuilder.Entity("ChaosBot.Models.RoleReaction", b =>
{
b.Property<ulong>("DiscordGuildId")
.HasColumnType("bigint unsigned");
b.Property<ulong>("DiscordMessageId")
.HasColumnType("bigint unsigned");
b.Property<ulong>("DiscordRoleId")
.HasColumnType("bigint unsigned");
b.Property<string>("DiscordEmoteNameEncoded")
.HasColumnType("varchar(95) CHARACTER SET utf8mb4");
b.HasKey("DiscordGuildId", "DiscordMessageId", "DiscordRoleId", "DiscordEmoteNameEncoded");
b.ToTable("RoleReactions");
});
#pragma warning restore 612, 618 #pragma warning restore 612, 618
} }
} }

View File

@ -13,6 +13,7 @@ namespace ChaosBot.Models
public DbSet<Configuration> Configuration { get; set; } public DbSet<Configuration> Configuration { get; set; }
public DbSet<Experience> ExperiencePoints { get; set; } public DbSet<Experience> ExperiencePoints { get; set; }
public DbSet<RoleReaction> RoleReactions { get; set; } public DbSet<RoleReaction> RoleReactions { get; set; }
public DbSet<CustomCommand> CustomCommands { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{ {
@ -49,6 +50,8 @@ namespace ChaosBot.Models
.HasKey(x => new {x.DiscordGuildId, x.Key}); .HasKey(x => new {x.DiscordGuildId, x.Key});
modelBuilder.Entity<RoleReaction>() modelBuilder.Entity<RoleReaction>()
.HasKey(x => new {x.DiscordGuildId, x.DiscordMessageId, x.DiscordRoleId, x.DiscordEmoteNameEncoded}); .HasKey(x => new {x.DiscordGuildId, x.DiscordMessageId, x.DiscordRoleId, x.DiscordEmoteNameEncoded});
modelBuilder.Entity<CustomCommand>()
.HasKey(x => new {x.DiscordGuildId, x.Command});
} }
} }
} }

View File

@ -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
}
}