Merge branch 'role-reactions' into develop

This commit is contained in:
Daniel_I_Am 2020-08-21 22:52:27 +02:00
commit edda01bae1
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84
8 changed files with 556 additions and 1 deletions

View File

@ -0,0 +1,205 @@
using System;
using Discord;
using Discord.Commands;
using System.Threading.Tasks;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using ChaosBot.Discord.PreConditions;
using ChaosBot.Models;
using ChaosBot.Repositories;
using Microsoft.EntityFrameworkCore;
using NLog;
namespace ChaosBot.Discord.Modules.Admin
{
public class Role : ModuleBase
{
private static readonly ILogger Logger = Program.Logger;
[Command("role")]
[Alias("role help", "role info")]
[CheckCommandPerm("Admin")]
public async Task RoleInfoCommand()
{
try
{
var sb = new StringBuilder();
var embed = new EmbedBuilder();
embed.WithColor(new Color(255, 255, 0));
embed.Title = $"Role Management Help";
sb.AppendLine();
sb.AppendLine("To add a role-reaction to a message:");
sb.AppendLine($"{ConfigurationRepository.GetValue<string>("Discord:Prefix", Context.Guild.Id, "!")}role add <emote> <role>");
sb.AppendLine("To add many role-reactions to a message add more sets of emote and role:");
sb.AppendLine($"{ConfigurationRepository.GetValue<string>("Discord:Prefix", Context.Guild.Id, "!")}role add <emote> <role> <emote> <role> <emote> <role>");
sb.AppendLine("To remove a role-reaction from a message:");
sb.AppendLine($"{ConfigurationRepository.GetValue<string>("Discord:Prefix", Context.Guild.Id, "!")}role remove <emote>");
sb.AppendLine();
sb.AppendLine("To view this help:");
sb.AppendLine($"{ConfigurationRepository.GetValue<string>("Discord:Prefix", Context.Guild.Id, "!")}role help");
/*
* Add the string to the Embed
*/
embed.Description = sb.ToString();
/*
* Reply with the Embed created above
*/
await ReplyAsync(null, false, embed.Build());
}
catch (Exception ex)
{
Logger.Error($"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
}
}
[Command("role add")]
[CheckCommandPerm("Admin")]
public async Task RoleAddCommand(params string[] parameters)
{
try
{
await using (ChaosbotContext dbContext = new ChaosbotContext())
{
string parameterString = String.Join("", parameters);
// Fetch last message
IMessage message = (await Context.Channel.GetMessagesAsync(2).FlattenAsync()).Last();
// Parse parameters
string pattern = @"((<a?:\w+:\d+>|[^<]+)<@&(\d+)>)";
string input = parameterString;
foreach (Match m in Regex.Matches(input, pattern))
{
try
{
IEmote emote;
string emoteString = m.Groups[2].Value;
string roleString = m.Groups[3].Value;
IRole role = Context.Guild.Roles.First(r => r.Id.ToString() == roleString);
if (Emote.TryParse(emoteString, out Emote tempEmote))
{
if (tempEmote.Animated && Context.Client.CurrentUser.PremiumType != PremiumType.Nitro)
throw new NotSupportedException("No support for animated icons");
if (Context.Guild.Emotes.All(e => e.Id != tempEmote.Id) &&
Context.Client.CurrentUser.PremiumType != PremiumType.Nitro)
throw new NotSupportedException($"No support for emotes from other servers");
emote = tempEmote;
}
else
{
emote = new Emoji(emoteString);
}
// Register DB entry
RoleReaction roleReaction = new RoleReaction
{
DiscordGuildId = Context.Guild.Id,
DiscordMessageId = message.Id,
DiscordRoleId = role.Id,
DiscordEmoteName = emote.ToString()
};
await dbContext.RoleReactions.Upsert(roleReaction)
.On(r => new {r.DiscordGuildId, r.DiscordEmoteNameEncoded, r.DiscordMessageId, r.DiscordRoleId})
.RunAsync();
// Add reaction to message
await message.AddReactionAsync(emote);
}
catch (Exception ex)
{
await ReplyAsync($"Something went wrong trying to process {m.Value}: {ex.Message}");
Logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
}
}
await dbContext.SaveChangesAsync();
}
}
catch (Exception ex)
{
Logger.Error($"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
}
}
[Command("role remove")]
[CheckCommandPerm("Admin")]
public async Task RoleRemoveCommand(params string[] parameters)
{
try
{
await using (ChaosbotContext dbContext = new ChaosbotContext())
{
string parameterString = String.Join("", parameters);
// Fetch last message
IMessage message = (await Context.Channel.GetMessagesAsync(2).FlattenAsync()).Last();
// Parse parameters
string pattern = @"(<a?:\w+:\d+>|..)";
string input = parameterString;
foreach (Match m in Regex.Matches(input, pattern))
{
Logger.Info(m.Value);
try
{
IEmote emote;
string emoteString = m.Value;
if (Emote.TryParse(emoteString, out Emote tempEmote))
{
if (tempEmote.Animated && Context.Client.CurrentUser.PremiumType != PremiumType.Nitro)
throw new NotSupportedException("No support for animated icons");
if (Context.Guild.Emotes.All(e => e.Id != tempEmote.Id) &&
Context.Client.CurrentUser.PremiumType != PremiumType.Nitro)
throw new NotSupportedException($"No support for emotes from other servers");
emote = tempEmote;
}
else
{
emote = new Emoji(emoteString);
}
// Delete DB entries
IQueryable<RoleReaction> roleReactions = dbContext.RoleReactions;
dbContext.RemoveRange(roleReactions
.Where(r => r.DiscordGuildId == Context.Guild.Id)
.ToList()
.Where(r => r.DiscordEmoteName == emote.ToString())
.Where(r => r.DiscordMessageId == message.Id)
.ToList());
// Remove reaction from message
await message.RemoveReactionAsync(emote, Context.Client.CurrentUser);
}
catch (Exception ex)
{
await ReplyAsync($"Something went wrong trying to process {m.Value}: {ex.Message}");
Logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
}
}
await dbContext.SaveChangesAsync();
}
}
catch (Exception ex)
{
Logger.Error($"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
}
}
}
}

View File

@ -33,6 +33,10 @@ namespace ChaosBot.Discord.Services
_client.MessageReceived += MessageReceivedAsync;
_client.ReactionAdded += ReactionAddedAsync;
_client.ReactionRemoved += ReactionRemovedAsync;
_client.UserJoined += AnnounceJoinedUser;
_client.UserLeft += AnnounceLeftUser;
@ -81,6 +85,16 @@ namespace ChaosBot.Discord.Services
}
}
public async Task ReactionAddedAsync(Cacheable<IUserMessage, ulong> cacheableMessage, ISocketMessageChannel socketMessageChannel, SocketReaction reaction)
{
RoleReactionHandler.HandleReactionAdded(cacheableMessage, socketMessageChannel, reaction);
}
public async Task ReactionRemovedAsync(Cacheable<IUserMessage, ulong> cacheableMessage, ISocketMessageChannel socketMessageChannel, SocketReaction reaction)
{
RoleReactionHandler.HandleReactionRemoved(cacheableMessage, socketMessageChannel, reaction);
}
public async Task AnnounceJoinedUser(SocketGuildUser user)
{
try

View File

@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ChaosBot.Models;
using Discord;
using Discord.WebSocket;
using NLog;
namespace ChaosBot.Discord.Services
{
public static class RoleReactionHandler
{
private static readonly ILogger Logger = Program.Logger;
public static async void HandleReactionAdded(Cacheable<IUserMessage, ulong> cacheableMessage, ISocketMessageChannel socketMessageChannel, SocketReaction reaction)
{
Optional<IUser> optionalUser = reaction.User;
if (!optionalUser.IsSpecified) return;
if (!(optionalUser.Value is IGuildUser user)) return;
if (!(socketMessageChannel is SocketGuildChannel channel)) return;
await using ChaosbotContext dbContext = new ChaosbotContext();
IQueryable<RoleReaction> roleReactionsQueryable = dbContext.RoleReactions;
List<RoleReaction> roleReactions = roleReactionsQueryable
.Where(r => r.DiscordMessageId == cacheableMessage.Id)
.ToList()
.Where(r => r.DiscordEmoteName == reaction.Emote.ToString())
.ToList();
foreach (RoleReaction roleReaction in roleReactions)
{
try
{
SocketRole role = channel.Guild.Roles.FirstOrDefault(r => r.Id == roleReaction.DiscordRoleId);
await user.AddRoleAsync(role);
}
catch (Exception ex)
{
Logger.Error($"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
}
}
}
public static async void HandleReactionRemoved(Cacheable<IUserMessage, ulong> cacheableMessage, ISocketMessageChannel socketMessageChannel, SocketReaction reaction)
{
Optional<IUser> optionalUser = reaction.User;
if (!optionalUser.IsSpecified) return;
if (!(optionalUser.Value is IGuildUser user)) return;
if (!(socketMessageChannel is SocketGuildChannel channel)) return;
await using ChaosbotContext dbContext = new ChaosbotContext();
IQueryable<RoleReaction> roleReactionsQueryable = dbContext.RoleReactions;
List<RoleReaction> roleReactions = roleReactionsQueryable
.Where(r => r.DiscordMessageId == cacheableMessage.Id)
.ToList()
.Where(r => r.DiscordEmoteName == reaction.Emote.ToString())
.ToList();
foreach (RoleReaction roleReaction in roleReactions)
{
try
{
SocketRole role = channel.Guild.Roles.FirstOrDefault(r => r.Id == roleReaction.DiscordRoleId);
await user.RemoveRoleAsync(role);
}
catch (Exception ex)
{
Logger.Error($"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
}
}
}
}
}

View File

@ -0,0 +1,163 @@
// <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("20200819203430_RoleReaction")]
partial class RoleReaction
{
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");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,33 @@
using System;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
namespace ChaosBot.Migrations
{
public partial class RoleReaction : Migration
{
protected readonly string Table = "RoleReactions";
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: Table,
columns: table => new
{
DiscordGuildId = table.Column<ulong>(nullable: false),
DiscordMessageId = table.Column<ulong>(nullable: false),
DiscordRoleId = table.Column<ulong>(nullable: false),
DiscordEmoteNameEncoded = table.Column<string>(nullable: false, maxLength: 128)
},
constraints: table =>
{
table.PrimaryKey("PK_RoleReactions", x => new { x.DiscordGuildId, x.DiscordRoleId, x.DiscordMessageId, x.DiscordEmoteNameEncoded });
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(name: Table);
}
}
}

View File

@ -1,4 +1,5 @@
// <auto-generated />
using System;
using ChaosBot.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
@ -24,7 +25,8 @@ namespace ChaosBot.Migrations
b.Property<string>("Command")
.IsRequired()
.HasColumnType("longtext CHARACTER SET utf8mb4");
.HasColumnType("varchar(128) CHARACTER SET utf8mb4")
.HasMaxLength(128);
b.Property<ulong>("DiscordGuildId")
.HasColumnType("bigint unsigned");
@ -58,6 +60,28 @@ namespace ChaosBot.Migrations
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")

View File

@ -12,6 +12,7 @@ namespace ChaosBot.Models
public DbSet<CommandPermission> CommandPermissions { get; set; }
public DbSet<Configuration> Configuration { get; set; }
public DbSet<Experience> ExperiencePoints { get; set; }
public DbSet<RoleReaction> RoleReactions { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
@ -46,6 +47,8 @@ namespace ChaosBot.Models
.HasKey(x => new {x.DiscordGuildId, x.DiscordUserId});
modelBuilder.Entity<Configuration>()
.HasKey(x => new {x.DiscordGuildId, x.Key});
modelBuilder.Entity<RoleReaction>()
.HasKey(x => new {x.DiscordGuildId, x.DiscordMessageId, x.DiscordRoleId, x.DiscordEmoteNameEncoded});
}
}
}

View File

@ -0,0 +1,37 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
namespace ChaosBot.Models
{
#region Required
public class RoleReaction
{
[Required]
public ulong DiscordGuildId { get; set; }
[Required]
public ulong DiscordMessageId { get; set; }
[Required]
public ulong DiscordRoleId { get; set; }
[NotMapped]
public string DiscordEmoteName
{
get
{
if (DiscordEmoteNameEncoded == null) return null;
return Encoding.UTF8.GetString(Convert.FromBase64String(DiscordEmoteNameEncoded));
}
set
{
if (value != null)
DiscordEmoteNameEncoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(value));
}
}
[Required]
public string DiscordEmoteNameEncoded { get; set; }
}
#endregion
}