Add webportal and set up custom commands #minor

This commit is contained in:
Daniel_I_Am 2020-08-24 15:20:14 +02:00
commit 8f20924df4
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84
13 changed files with 347 additions and 5 deletions

1
.gitignore vendored
View File

@ -5,6 +5,7 @@ obj/
/ChaosBot/obj/ /ChaosBot/obj/
.idea/ .idea/
/ChaosBot/appsettings.json /ChaosBot/appsettings.json
/ChaosBot/appsettings.*.json
/ChaosBot/ChaosBotSQL.db /ChaosBot/ChaosBotSQL.db
*.sln.DotSettings.user *.sln.DotSettings.user
/.vs /.vs

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "ChaosBot/wwwroot"]
path = ChaosBot/wwwroot
url = ssh://git@git.chaoticlogic.us:2302/discord-bots/chaosbot-frontend.git

View File

@ -30,6 +30,15 @@
<ItemGroup> <ItemGroup>
<Folder Include="WebServer\App" /> <Folder Include="WebServer\App" />
<Folder Include="wwwroot" />
</ItemGroup>
<ItemGroup>
<Content Update="appsettings.json">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -77,6 +77,9 @@ 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);
bool customCommandExecuted = await CustomCommandHandler.CheckCommand(context, argPos);
if (!customCommandExecuted)
await _commands.ExecuteAsync(context, argPos, _services); 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
}
}

View File

@ -1,3 +1,4 @@
using System.IO;
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
@ -36,12 +37,20 @@ namespace ChaosBot.WebServer
{ {
_logger.Info("Initializing Kestrel Startup and Configuration"); _logger.Info("Initializing Kestrel Startup and Configuration");
app.UseForwardedHeaders(); if (Program.AppSettingsHandler.GetValue<bool>("WebServer:Debug", false))
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();
app.UseForwardedHeaders();
app.UseMiddleware<AuthenticationMiddleware>(); app.UseMiddleware<AuthenticationMiddleware>();
app.UseRouting(); app.UseRouting();
app.UseAuthorization(); app.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
DefaultFilesOptions options = new DefaultFilesOptions();
options.DefaultFileNames.Clear();
options.DefaultFileNames.Add("index.html");
app.UseDefaultFiles(options);
app.UseStaticFiles();
} }
} }
} }

View File

@ -1,3 +1,4 @@
using System.IO;
using System.Net; using System.Net;
using NLog.Extensions.Logging; using NLog.Extensions.Logging;
@ -19,6 +20,11 @@ namespace ChaosBot.WebServer
Host.CreateDefaultBuilder(args) Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => .ConfigureWebHostDefaults(webBuilder =>
{ {
string contentRoot = Directory.GetCurrentDirectory();
string webRoot = Path.Combine(contentRoot, "wwwroot/dist");
webBuilder.UseContentRoot(contentRoot);
webBuilder.UseWebRoot(webRoot);
webBuilder.ConfigureKestrel(serverOptions => webBuilder.ConfigureKestrel(serverOptions =>
{ {
serverOptions.Listen(IPAddress.Any, Program.AppSettingsHandler.GetValue<int>("WebServer:Port"), serverOptions.Listen(IPAddress.Any, Program.AppSettingsHandler.GetValue<int>("WebServer:Port"),

1
ChaosBot/wwwroot Submodule

@ -0,0 +1 @@
Subproject commit ad857be54bdaf13813a00458c242bcc808db89ec