From 4b080590b28fb0b1f6380a1f08ede72f21ce2679 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Thu, 25 Aug 2016 00:39:02 +0200 Subject: [PATCH] Auto assign roles --- ...20160824223801_FirstMigration.Designer.cs} | 6 +- ...on.cs => 20160824223801_FirstMigration.cs} | 11 +-- .../NadekoSqliteContextModelSnapshot.cs | 4 +- ...inistrationModule.cs => Administration.cs} | 14 ++-- .../Administration/Commands/AutoAssignRole.cs | 54 -------------- .../Commands/AutoAssignRoleCommands.cs | 71 +++++++++++++++++++ .../Services/Database/IUnitOfWork.cs | 2 +- .../Services/Database/Models/Config.cs | 3 +- .../Services/Database/NadekoContext.cs | 7 +- src/NadekoBot/Services/Database/UnitOfWork.cs | 4 +- 10 files changed, 97 insertions(+), 79 deletions(-) rename src/NadekoBot/Migrations/{20160824184118_FirstMigration.Designer.cs => 20160824223801_FirstMigration.Designer.cs} (93%) rename src/NadekoBot/Migrations/{20160824184118_FirstMigration.cs => 20160824223801_FirstMigration.cs} (89%) rename src/NadekoBot/Modules/Administration/{AdministrationModule.cs => Administration.cs} (98%) delete mode 100644 src/NadekoBot/Modules/Administration/Commands/AutoAssignRole.cs create mode 100644 src/NadekoBot/Modules/Administration/Commands/AutoAssignRoleCommands.cs diff --git a/src/NadekoBot/Migrations/20160824184118_FirstMigration.Designer.cs b/src/NadekoBot/Migrations/20160824223801_FirstMigration.Designer.cs similarity index 93% rename from src/NadekoBot/Migrations/20160824184118_FirstMigration.Designer.cs rename to src/NadekoBot/Migrations/20160824223801_FirstMigration.Designer.cs index 2e64b6d5..b482fe7e 100644 --- a/src/NadekoBot/Migrations/20160824184118_FirstMigration.Designer.cs +++ b/src/NadekoBot/Migrations/20160824223801_FirstMigration.Designer.cs @@ -8,7 +8,7 @@ using NadekoBot.Services.Database.Impl; namespace NadekoBot.Migrations { [DbContext(typeof(NadekoSqliteContext))] - [Migration("20160824184118_FirstMigration")] + [Migration("20160824223801_FirstMigration")] partial class FirstMigration { protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -21,6 +21,8 @@ namespace NadekoBot.Migrations b.Property("Id") .ValueGeneratedOnAdd(); + b.Property("AutoAssignRoleId"); + b.Property("DeleteMessageOnCommand"); b.Property("GuildId"); @@ -30,7 +32,7 @@ namespace NadekoBot.Migrations b.HasIndex("GuildId") .IsUnique(); - b.ToTable("Configs"); + b.ToTable("GuildConfigs"); }); modelBuilder.Entity("NadekoBot.Services.Database.Models.Donator", b => diff --git a/src/NadekoBot/Migrations/20160824184118_FirstMigration.cs b/src/NadekoBot/Migrations/20160824223801_FirstMigration.cs similarity index 89% rename from src/NadekoBot/Migrations/20160824184118_FirstMigration.cs rename to src/NadekoBot/Migrations/20160824223801_FirstMigration.cs index ee8a450f..898959d0 100644 --- a/src/NadekoBot/Migrations/20160824184118_FirstMigration.cs +++ b/src/NadekoBot/Migrations/20160824223801_FirstMigration.cs @@ -9,17 +9,18 @@ namespace NadekoBot.Migrations protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateTable( - name: "Configs", + name: "GuildConfigs", columns: table => new { Id = table.Column(nullable: false) .Annotation("Autoincrement", true), + AutoAssignRoleId = table.Column(nullable: false), DeleteMessageOnCommand = table.Column(nullable: false), GuildId = table.Column(nullable: false) }, constraints: table => { - table.PrimaryKey("PK_Configs", x => x.Id); + table.PrimaryKey("PK_GuildConfigs", x => x.Id); }); migrationBuilder.CreateTable( @@ -55,8 +56,8 @@ namespace NadekoBot.Migrations }); migrationBuilder.CreateIndex( - name: "IX_Configs_GuildId", - table: "Configs", + name: "IX_GuildConfigs_GuildId", + table: "GuildConfigs", column: "GuildId", unique: true); @@ -70,7 +71,7 @@ namespace NadekoBot.Migrations protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropTable( - name: "Configs"); + name: "GuildConfigs"); migrationBuilder.DropTable( name: "Donators"); diff --git a/src/NadekoBot/Migrations/NadekoSqliteContextModelSnapshot.cs b/src/NadekoBot/Migrations/NadekoSqliteContextModelSnapshot.cs index 9328de80..39a2dcd2 100644 --- a/src/NadekoBot/Migrations/NadekoSqliteContextModelSnapshot.cs +++ b/src/NadekoBot/Migrations/NadekoSqliteContextModelSnapshot.cs @@ -20,6 +20,8 @@ namespace NadekoBot.Migrations b.Property("Id") .ValueGeneratedOnAdd(); + b.Property("AutoAssignRoleId"); + b.Property("DeleteMessageOnCommand"); b.Property("GuildId"); @@ -29,7 +31,7 @@ namespace NadekoBot.Migrations b.HasIndex("GuildId") .IsUnique(); - b.ToTable("Configs"); + b.ToTable("GuildConfigs"); }); modelBuilder.Entity("NadekoBot.Services.Database.Models.Donator", b => diff --git a/src/NadekoBot/Modules/Administration/AdministrationModule.cs b/src/NadekoBot/Modules/Administration/Administration.cs similarity index 98% rename from src/NadekoBot/Modules/Administration/AdministrationModule.cs rename to src/NadekoBot/Modules/Administration/Administration.cs index 6ec0771a..2811da66 100644 --- a/src/NadekoBot/Modules/Administration/AdministrationModule.cs +++ b/src/NadekoBot/Modules/Administration/Administration.cs @@ -15,7 +15,6 @@ using NadekoBot.Services.Database; using NadekoBot.Services.Database.Models; //todo fix delmsgoncmd -//todo DB namespace NadekoBot.Modules.Administration { [Module(".", AppendSpace = false)] @@ -47,9 +46,9 @@ namespace NadekoBot.Modules.Administration Config conf; using (var uow = DbHandler.UnitOfWork()) { - conf = uow.Configs.For(channel.Guild.Id); + conf = uow.GuildConfigs.For(channel.Guild.Id); conf.DeleteMessageOnCommand = !conf.DeleteMessageOnCommand; - uow.Configs.Update(conf); + uow.GuildConfigs.Update(conf); await uow.CompleteAsync(); } if (conf.DeleteMessageOnCommand) @@ -381,8 +380,8 @@ namespace NadekoBot.Modules.Administration { var channel = (ITextChannel)imsg.Channel; //todo actually print info about created channel - await channel.Guild.CreateVoiceChannelAsync(channelName).ConfigureAwait(false); - await channel.SendMessageAsync($"Created voice channel **{channelName}**.").ConfigureAwait(false); + var ch = await channel.Guild.CreateVoiceChannelAsync(channelName).ConfigureAwait(false); + await channel.SendMessageAsync($"Created voice channel **{ch.Name}**, id `{ch.Id}`.").ConfigureAwait(false); } [LocalizedCommand, LocalizedDescription, LocalizedSummary] @@ -391,7 +390,7 @@ namespace NadekoBot.Modules.Administration public async Task DelTxtChanl(IMessage imsg, [Remainder] ITextChannel channel) { await channel.DeleteAsync().ConfigureAwait(false); - await channel.SendMessageAsync($"Removed text channel **{channel.Name}**.").ConfigureAwait(false); + await channel.SendMessageAsync($"Removed text channel **{channel.Name}**, id `{channel.Id}`.").ConfigureAwait(false); } [LocalizedCommand, LocalizedDescription, LocalizedSummary] @@ -402,7 +401,7 @@ namespace NadekoBot.Modules.Administration var channel = (ITextChannel)imsg.Channel; //todo actually print info about created channel var txtCh = await channel.Guild.CreateTextChannelAsync(channelName).ConfigureAwait(false); - await channel.SendMessageAsync($"Added text channel **{channelName}**.").ConfigureAwait(false); + await channel.SendMessageAsync($"Added text channel **{txtCh.Name}**, id `{txtCh.Id}`.").ConfigureAwait(false); } [LocalizedCommand, LocalizedDescription, LocalizedSummary] @@ -413,7 +412,6 @@ namespace NadekoBot.Modules.Administration var channel = (ITextChannel)imsg.Channel; topic = topic ?? ""; await (channel as ITextChannel).ModifyAsync(c => c.Topic = topic); - //await (channel).ModifyAsync(c => c).ConfigureAwait(false); await channel.SendMessageAsync(":ok: **New channel topic set.**").ConfigureAwait(false); } diff --git a/src/NadekoBot/Modules/Administration/Commands/AutoAssignRole.cs b/src/NadekoBot/Modules/Administration/Commands/AutoAssignRole.cs deleted file mode 100644 index b171b632..00000000 --- a/src/NadekoBot/Modules/Administration/Commands/AutoAssignRole.cs +++ /dev/null @@ -1,54 +0,0 @@ -using Discord; -using Discord.Commands; -using Discord.WebSocket; -using NadekoBot.Attributes; -using System.Threading.Tasks; - -namespace NadekoBot.Modules.Administration -{ - //todo DB - public partial class Administration - { - [Group] - public class AutoAssignRole - { - public AutoAssignRole() - { - var _client = NadekoBot.Client; - _client.UserJoined += (user) => - { - //var config = SpecificConfigurations.Default.Of(e.Server.Id); - - //var role = e.Server.Roles.Where(r => r.Id == config.AutoAssignedRole).FirstOrDefault(); - - //if (role == null) - // return; - - //imsg.Author.AddRoles(role); - return Task.CompletedTask; - }; - } - - //[LocalizedCommand, LocalizedDescription, LocalizedSummary] - //[RequireContext(ContextType.Guild)] - //[RequirePermission(GuildPermission.ManageRoles)] - //public async Task AutoAssignRole(IMessage imsg, IRole role) - //{ - // var channel = (ITextChannel)imsg.Channel; - - // var config = SpecificConfigurations.Default.Of(e.Server.Id); - - // if (string.IsNullOrWhiteSpace(r)) //if role is not specified, disable - // { - // config.AutoAssignedRole = 0; - - // await channel.SendMessageAsync("`Auto assign role on user join is now disabled.`").ConfigureAwait(false); - // return; - // } - - // config.AutoAssignedRole = role.Id; - // await channel.SendMessageAsync("`Auto assigned role is set.`").ConfigureAwait(false); - //} - } - } -} diff --git a/src/NadekoBot/Modules/Administration/Commands/AutoAssignRoleCommands.cs b/src/NadekoBot/Modules/Administration/Commands/AutoAssignRoleCommands.cs new file mode 100644 index 00000000..2be6466a --- /dev/null +++ b/src/NadekoBot/Modules/Administration/Commands/AutoAssignRoleCommands.cs @@ -0,0 +1,71 @@ +using Discord; +using Discord.Commands; +using Discord.WebSocket; +using NadekoBot.Attributes; +using NadekoBot.Services; +using NadekoBot.Services.Database; +using NadekoBot.Services.Database.Models; +using System; +using System.Linq; +using System.Threading.Tasks; + +namespace NadekoBot.Modules.Administration +{ + public partial class Administration + { + [Group] + public class AutoAssignRoleCommands + { + public AutoAssignRoleCommands() + { + var _client = NadekoBot.Client; + _client.UserJoined += async (user) => + { + Config conf; + using (var uow = DbHandler.UnitOfWork()) + { + conf = uow.GuildConfigs.For(user.Guild.Id); + } + var aarType = conf.AutoAssignRoleId.GetType(); + + if (conf.AutoAssignRoleId == 0) + return; + + var role = user.Guild.Roles.Where(r => r.Id == conf.AutoAssignRoleId).FirstOrDefault(); + + if (role != null) + await user.AddRolesAsync(role); + }; + } + + [LocalizedCommand, LocalizedDescription, LocalizedSummary] + [RequireContext(ContextType.Guild)] + [RequirePermission(GuildPermission.ManageRoles)] + public async Task AutoAssignRole(IMessage imsg, [Remainder] IRole role = null) + { + var channel = (ITextChannel)imsg.Channel; + + Config conf; + using (var uow = DbHandler.UnitOfWork()) + { + conf = uow.GuildConfigs.For(channel.Guild.Id); + if (role == null) + conf.AutoAssignRoleId = 0; + else + conf.AutoAssignRoleId = role.Id; + + uow.GuildConfigs.Update(conf); + await uow.CompleteAsync(); + } + + if (role == null) + { + await channel.SendMessageAsync("`Auto assign role on user join is now disabled.`").ConfigureAwait(false); + return; + } + + await channel.SendMessageAsync("`Auto assigned role is set.`").ConfigureAwait(false); + } + } + } +} diff --git a/src/NadekoBot/Services/Database/IUnitOfWork.cs b/src/NadekoBot/Services/Database/IUnitOfWork.cs index c2eafccf..2b37dc5d 100644 --- a/src/NadekoBot/Services/Database/IUnitOfWork.cs +++ b/src/NadekoBot/Services/Database/IUnitOfWork.cs @@ -10,7 +10,7 @@ namespace NadekoBot.Services.Database public interface IUnitOfWork : IDisposable { IQuoteRepository Quotes { get; } - IConfigRepository Configs { get; } + IConfigRepository GuildConfigs { get; } IDonatorsRepository Donators { get; } int Complete(); Task CompleteAsync(); diff --git a/src/NadekoBot/Services/Database/Models/Config.cs b/src/NadekoBot/Services/Database/Models/Config.cs index a1e62e22..05900703 100644 --- a/src/NadekoBot/Services/Database/Models/Config.cs +++ b/src/NadekoBot/Services/Database/Models/Config.cs @@ -9,6 +9,7 @@ namespace NadekoBot.Services.Database.Models public class Config : DbEntity { public ulong GuildId { get; set; } - public bool DeleteMessageOnCommand { get; set; } = false; + public bool DeleteMessageOnCommand { get; set; } + public ulong AutoAssignRoleId { get; set; } } } diff --git a/src/NadekoBot/Services/Database/NadekoContext.cs b/src/NadekoBot/Services/Database/NadekoContext.cs index 8114e6d9..7b698a59 100644 --- a/src/NadekoBot/Services/Database/NadekoContext.cs +++ b/src/NadekoBot/Services/Database/NadekoContext.cs @@ -12,7 +12,7 @@ namespace NadekoBot.Services.Database { public DbSet Quotes { get; set; } public DbSet Donators { get; set; } - public DbSet Configs { get; set; } + public DbSet GuildConfigs { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { @@ -41,9 +41,6 @@ namespace NadekoBot.Services.Database #endregion } - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - optionsBuilder.UseSqlite("Filename=./data/NadekoBot.sqlite"); - } + protected abstract override void OnConfiguring(DbContextOptionsBuilder optionsBuilder); } } diff --git a/src/NadekoBot/Services/Database/UnitOfWork.cs b/src/NadekoBot/Services/Database/UnitOfWork.cs index 9eec7d35..1768f7de 100644 --- a/src/NadekoBot/Services/Database/UnitOfWork.cs +++ b/src/NadekoBot/Services/Database/UnitOfWork.cs @@ -15,8 +15,8 @@ namespace NadekoBot.Services.Database private IQuoteRepository _quotes; public IQuoteRepository Quotes => _quotes ?? (_quotes = new QuoteRepository(_context)); - private IConfigRepository _configs; - public IConfigRepository Configs => _configs ?? (_configs = new ConfigRepository(_context)); + private IConfigRepository _guildConfigs; + public IConfigRepository GuildConfigs => _guildConfigs ?? (_guildConfigs = new ConfigRepository(_context)); private IDonatorsRepository _donators; public IDonatorsRepository Donators => _donators ?? (_donators = new DonatorsRepository(_context));