Self assigned roles readded

This commit is contained in:
Kwoth 2016-08-26 19:25:54 +02:00
parent e64dd6548f
commit ae5634c9b7
14 changed files with 378 additions and 208 deletions

View File

@ -8,7 +8,7 @@ using NadekoBot.Services.Database.Impl;
namespace NadekoBot.Migrations namespace NadekoBot.Migrations
{ {
[DbContext(typeof(NadekoSqliteContext))] [DbContext(typeof(NadekoSqliteContext))]
[Migration("20160825184527_first")] [Migration("20160826172044_first")]
partial class first partial class first
{ {
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -92,6 +92,8 @@ namespace NadekoBot.Migrations
b.Property<int>("AutoDeleteGreetMessagesTimer"); b.Property<int>("AutoDeleteGreetMessagesTimer");
b.Property<bool>("AutoDeleteSelfAssignedRoleMessages");
b.Property<ulong>("ByeMessageChannelId"); b.Property<ulong>("ByeMessageChannelId");
b.Property<string>("ChannelByeMessageText"); b.Property<string>("ChannelByeMessageText");
@ -102,6 +104,8 @@ namespace NadekoBot.Migrations
b.Property<string>("DmGreetMessageText"); b.Property<string>("DmGreetMessageText");
b.Property<bool>("ExclusiveSelfAssignedRoles");
b.Property<ulong>("GreetMessageChannelId"); b.Property<ulong>("GreetMessageChannelId");
b.Property<ulong>("GuildId"); b.Property<ulong>("GuildId");
@ -165,6 +169,23 @@ namespace NadekoBot.Migrations
b.ToTable("Reminders"); b.ToTable("Reminders");
}); });
modelBuilder.Entity("NadekoBot.Services.Database.Models.SelfAssignedRole", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<ulong>("GuildId");
b.Property<ulong>("RoleId");
b.HasKey("Id");
b.HasIndex("GuildId", "RoleId")
.IsUnique();
b.ToTable("SelfAssignableRoles");
});
modelBuilder.Entity("NadekoBot.Services.Database.Models.ClashCaller", b => modelBuilder.Entity("NadekoBot.Services.Database.Models.ClashCaller", b =>
{ {
b.HasOne("NadekoBot.Services.Database.Models.ClashWar", "ClashWar") b.HasOne("NadekoBot.Services.Database.Models.ClashWar", "ClashWar")

View File

@ -51,11 +51,13 @@ namespace NadekoBot.Migrations
AutoDeleteByeMessages = table.Column<bool>(nullable: false), AutoDeleteByeMessages = table.Column<bool>(nullable: false),
AutoDeleteGreetMessages = table.Column<bool>(nullable: false), AutoDeleteGreetMessages = table.Column<bool>(nullable: false),
AutoDeleteGreetMessagesTimer = table.Column<int>(nullable: false), AutoDeleteGreetMessagesTimer = table.Column<int>(nullable: false),
AutoDeleteSelfAssignedRoleMessages = table.Column<bool>(nullable: false),
ByeMessageChannelId = table.Column<ulong>(nullable: false), ByeMessageChannelId = table.Column<ulong>(nullable: false),
ChannelByeMessageText = table.Column<string>(nullable: true), ChannelByeMessageText = table.Column<string>(nullable: true),
ChannelGreetMessageText = table.Column<string>(nullable: true), ChannelGreetMessageText = table.Column<string>(nullable: true),
DeleteMessageOnCommand = table.Column<bool>(nullable: false), DeleteMessageOnCommand = table.Column<bool>(nullable: false),
DmGreetMessageText = table.Column<string>(nullable: true), DmGreetMessageText = table.Column<string>(nullable: true),
ExclusiveSelfAssignedRoles = table.Column<bool>(nullable: false),
GreetMessageChannelId = table.Column<ulong>(nullable: false), GreetMessageChannelId = table.Column<ulong>(nullable: false),
GuildId = table.Column<ulong>(nullable: false), GuildId = table.Column<ulong>(nullable: false),
SendChannelByeMessage = table.Column<bool>(nullable: false), SendChannelByeMessage = table.Column<bool>(nullable: false),
@ -102,6 +104,20 @@ namespace NadekoBot.Migrations
table.PrimaryKey("PK_Reminders", x => x.Id); table.PrimaryKey("PK_Reminders", x => x.Id);
}); });
migrationBuilder.CreateTable(
name: "SelfAssignableRoles",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("Autoincrement", true),
GuildId = table.Column<ulong>(nullable: false),
RoleId = table.Column<ulong>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_SelfAssignableRoles", x => x.Id);
});
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "ClashCallers", name: "ClashCallers",
columns: table => new columns: table => new
@ -141,6 +157,12 @@ namespace NadekoBot.Migrations
table: "GuildConfigs", table: "GuildConfigs",
column: "GuildId", column: "GuildId",
unique: true); unique: true);
migrationBuilder.CreateIndex(
name: "IX_SelfAssignableRoles_GuildId_RoleId",
table: "SelfAssignableRoles",
columns: new[] { "GuildId", "RoleId" },
unique: true);
} }
protected override void Down(MigrationBuilder migrationBuilder) protected override void Down(MigrationBuilder migrationBuilder)
@ -160,6 +182,9 @@ namespace NadekoBot.Migrations
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Reminders"); name: "Reminders");
migrationBuilder.DropTable(
name: "SelfAssignableRoles");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "ClashOfClans"); name: "ClashOfClans");
} }

View File

@ -91,6 +91,8 @@ namespace NadekoBot.Migrations
b.Property<int>("AutoDeleteGreetMessagesTimer"); b.Property<int>("AutoDeleteGreetMessagesTimer");
b.Property<bool>("AutoDeleteSelfAssignedRoleMessages");
b.Property<ulong>("ByeMessageChannelId"); b.Property<ulong>("ByeMessageChannelId");
b.Property<string>("ChannelByeMessageText"); b.Property<string>("ChannelByeMessageText");
@ -101,6 +103,8 @@ namespace NadekoBot.Migrations
b.Property<string>("DmGreetMessageText"); b.Property<string>("DmGreetMessageText");
b.Property<bool>("ExclusiveSelfAssignedRoles");
b.Property<ulong>("GreetMessageChannelId"); b.Property<ulong>("GreetMessageChannelId");
b.Property<ulong>("GuildId"); b.Property<ulong>("GuildId");
@ -164,6 +168,23 @@ namespace NadekoBot.Migrations
b.ToTable("Reminders"); b.ToTable("Reminders");
}); });
modelBuilder.Entity("NadekoBot.Services.Database.Models.SelfAssignedRole", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<ulong>("GuildId");
b.Property<ulong>("RoleId");
b.HasKey("Id");
b.HasIndex("GuildId", "RoleId")
.IsUnique();
b.ToTable("SelfAssignableRoles");
});
modelBuilder.Entity("NadekoBot.Services.Database.Models.ClashCaller", b => modelBuilder.Entity("NadekoBot.Services.Database.Models.ClashCaller", b =>
{ {
b.HasOne("NadekoBot.Services.Database.Models.ClashWar", "ClashWar") b.HasOne("NadekoBot.Services.Database.Models.ClashWar", "ClashWar")

View File

@ -27,7 +27,7 @@ namespace NadekoBot.Modules.Administration
_client.MessageReceived += async (imsg) => _client.MessageReceived += async (imsg) =>
{ {
var channel = (ITextChannel)imsg.Channel; var channel = imsg.Channel as ITextChannel;
if (channel == null || await imsg.IsAuthor()) if (channel == null || await imsg.IsAuthor())
return; return;

View File

@ -1,207 +1,232 @@
//using Discord.Commands; using Discord;
//using Discord.Net; using Discord.Commands;
//using NadekoBot.Classes; using Discord.Net;
//using NadekoBot.Modules.Permissions.Classes; using NadekoBot.Attributes;
//using System; using NadekoBot.Services;
//using System.Collections.Generic; using NadekoBot.Services.Database;
//using System.Linq; using NadekoBot.Services.Database.Models;
//using System.Text; using System;
//using System.Threading.Tasks; using System.Collections.Generic;
////todo DB using System.Linq;
//namespace NadekoBot.Modules.Administration using System.Text;
//{ using System.Threading.Tasks;
// internal class SelfAssignedRolesCommand : DiscordCommand //todo DB
// { namespace NadekoBot.Modules.Administration
// public SelfAssignedRolesCommand(DiscordModule module) : base(module) { } {
// internal override void Init(CommandGroupBuilder cgb) public partial class Administration
// { {
// cgb.CreateCommand(Module.Prefix + "asar") [Group]
// .Description("Adds a role, or list of roles separated by whitespace" + public class SelfAssignedRolesCommands
// $"(use quotations for multiword roles) to the list of self-assignable roles. **Needs Manage Roles Permissions.**| `{Prefix}asar Gamer`") {
// .Parameter("roles", ParameterType.Multiple)
// .AddCheck(SimpleCheckers.CanManageRoles)
// .Do(async e =>
// {
// var config = SpecificConfigurations.Default.Of(e.Server.Id);
// var msg = new StringBuilder();
// foreach (var arg in e.Args)
// {
// var role = e.Server.FindRoles(arg.Trim()).FirstOrDefault();
// if (role == null)
// msg.AppendLine($":anger:Role **{arg}** not found.");
// else
// {
// if (config.ListOfSelfAssignableRoles.Contains(role.Id))
// {
// msg.AppendLine($":anger:Role **{role.Name}** is already in the list.");
// continue;
// }
// config.ListOfSelfAssignableRoles.Add(role.Id);
// msg.AppendLine($":ok:Role **{role.Name}** added to the list.");
// }
// }
// await channel.SendMessageAsync(msg.ToString()).ConfigureAwait(false);
// });
// cgb.CreateCommand(Module.Prefix + "rsar") [LocalizedCommand, LocalizedDescription, LocalizedSummary]
// .Description($"Removes a specified role from the list of self-assignable roles. | `{Prefix}rsar`") [RequireContext(ContextType.Guild)]
// .Parameter("role", ParameterType.Unparsed) [RequirePermission(GuildPermission.ManageRoles)]
// .AddCheck(SimpleCheckers.CanManageRoles) public async Task Asar(IMessage imsg, [Remainder] IRole role)
// .Do(async e => {
// { var channel = (ITextChannel)imsg.Channel;
// var roleName = role?.Trim();
// if (string.IsNullOrWhiteSpace(roleName))
// return;
// var role = e.Server.FindRoles(roleName).FirstOrDefault();
// if (role == null)
// {
// await channel.SendMessageAsync(":anger:That role does not exist.").ConfigureAwait(false);
// return;
// }
// var config = SpecificConfigurations.Default.Of(e.Server.Id);
// if (!config.ListOfSelfAssignableRoles.Contains(role.Id))
// {
// await channel.SendMessageAsync(":anger:That role is not self-assignable.").ConfigureAwait(false);
// return;
// }
// config.ListOfSelfAssignableRoles.Remove(role.Id);
// await channel.SendMessageAsync($":ok:**{role.Name}** has been removed from the list of self-assignable roles").ConfigureAwait(false);
// });
// cgb.CreateCommand(Module.Prefix + "lsar") IEnumerable<SelfAssignedRole> roles;
// .Description($"Lists all self-assignable roles. | `{Prefix}lsar`")
// .Parameter("roles", ParameterType.Multiple)
// .Do(async e =>
// {
// var config = SpecificConfigurations.Default.Of(e.Server.Id);
// var msg = new StringBuilder($"There are `{config.ListOfSelfAssignableRoles.Count}` self assignable roles:\n");
// var toRemove = new HashSet<ulong>();
// foreach (var roleId in config.ListOfSelfAssignableRoles.OrderBy(r => r.ToString()))
// {
// var role = e.Server.GetRole(roleId);
// if (role == null)
// {
// msg.Append($"`{roleId} not found. Cleaned up.`, ");
// toRemove.Add(roleId);
// }
// else
// {
// msg.Append($"**{role.Name}**, ");
// }
// }
// foreach (var id in toRemove)
// {
// config.ListOfSelfAssignableRoles.Remove(id);
// }
// await channel.SendMessageAsync(msg.ToString()).ConfigureAwait(false);
// });
string msg;
using (var uow = DbHandler.UnitOfWork())
{
roles = uow.SelfAssignedRoles.GetFromGuild(channel.Guild.Id);
if (roles.Any(s => s.RoleId == role.Id && s.GuildId == role.GuildId))
{
msg = $":anger:Role **{role.Name}** is already in the list.";
}
else
{
uow.SelfAssignedRoles.Add(new SelfAssignedRole {
RoleId = role.Id,
GuildId = role.GuildId
});
await uow.CompleteAsync();
msg = $":ok:Role **{role.Name}** added to the list.";
}
}
await channel.SendMessageAsync(msg.ToString()).ConfigureAwait(false);
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
[RequirePermission(GuildPermission.ManageRoles)]
public async Task Rsar(IMessage imsg, [Remainder] IRole role)
{
var channel = (ITextChannel)imsg.Channel;
// cgb.CreateCommand(Module.Prefix + "togglexclsar").Alias(Module.Prefix + "tesar") bool success;
// .Description($"toggle whether the self-assigned roles should be exclusive | `{Prefix}tesar`") using (var uow = DbHandler.UnitOfWork())
// .AddCheck(SimpleCheckers.CanManageRoles) {
// .Do(async e => success = uow.SelfAssignedRoles.DeleteByGuildAndRoleId(role.GuildId, role.Id);
// { await uow.CompleteAsync();
// var config = SpecificConfigurations.Default.Of(e.Server.Id); }
// config.ExclusiveSelfAssignedRoles = !config.ExclusiveSelfAssignedRoles; if (success)
// string exl = config.ExclusiveSelfAssignedRoles ? "exclusive" : "not exclusive"; {
// await channel.SendMessageAsync("Self assigned roles are now " + exl); await channel.SendMessageAsync(":anger:That role is not self-assignable.").ConfigureAwait(false);
// }); return;
}
await channel.SendMessageAsync($":ok:**{role.Name}** has been removed from the list of self-assignable roles").ConfigureAwait(false);
}
// cgb.CreateCommand(Module.Prefix + "iam") [LocalizedCommand, LocalizedDescription, LocalizedSummary]
// .Description("Adds a role to you that you choose. " + [RequireContext(ContextType.Guild)]
// "Role must be on a list of self-assignable roles." + public async Task Lsar(IMessage imsg)
// $" | `{Prefix}iam Gamer`") {
// .Parameter("role", ParameterType.Unparsed) var channel = (ITextChannel)imsg.Channel;
// .Do(async e =>
// {
// var roleName = role?.Trim();
// if (string.IsNullOrWhiteSpace(roleName))
// return;
// var role = e.Server.FindRoles(roleName).FirstOrDefault();
// if (role == null)
// {
// await channel.SendMessageAsync(":anger:That role does not exist.").ConfigureAwait(false);
// return;
// }
// var config = SpecificConfigurations.Default.Of(e.Server.Id);
// if (!config.ListOfSelfAssignableRoles.Contains(role.Id))
// {
// await channel.SendMessageAsync(":anger:That role is not self-assignable.").ConfigureAwait(false);
// return;
// }
// if (imsg.Author.HasRole(role))
// {
// await channel.SendMessageAsync($":anger:You already have {role.Name} role.").ConfigureAwait(false);
// return;
// }
// var sameRoles = imsg.Author.Roles.Where(r => config.ListOfSelfAssignableRoles.Contains(r.Id));
// if (config.ExclusiveSelfAssignedRoles && sameRoles.Any())
// {
// await channel.SendMessageAsync($":anger:You already have {sameRoles.FirstOrDefault().Name} role.").ConfigureAwait(false);
// return;
// }
// try
// {
// await imsg.Author.AddRoles(role).ConfigureAwait(false);
// }
// catch (HttpException ex) when (ex.StatusCode == System.Net.HttpStatusCode.InternalServerError)
// {
// }
// catch (Exception ex)
// {
// await channel.SendMessageAsync($":anger:`I am unable to add that role to you. I can't add roles to owners or other roles higher than my role in the role hierarchy.`").ConfigureAwait(false);
// return;
// }
// var msg = await channel.SendMessageAsync($":ok:You now have {role.Name} role.").ConfigureAwait(false);
// await Task.Delay(3000).ConfigureAwait(false);
// await msg.Delete().ConfigureAwait(false);
// try
// {
// await e.Message.Delete().ConfigureAwait(false);
// }
// catch { }
// });
// cgb.CreateCommand(Module.Prefix + "iamnot") var toRemove = new HashSet<SelfAssignedRole>();
// .Alias(Module.Prefix + "iamn") var removeMsg = new StringBuilder();
// .Description("Removes a role to you that you choose. " + var msg = new StringBuilder();
// "Role must be on a list of self-assignable roles." + using (var uow = DbHandler.UnitOfWork())
// $" | `{Prefix}iamn Gamer`") {
// .Parameter("role", ParameterType.Unparsed) var roleModels = uow.SelfAssignedRoles.GetFromGuild(channel.Guild.Id);
// .Do(async e => msg.AppendLine($"There are `{roleModels.Count()}` self assignable roles:");
// {
// var roleName = role?.Trim(); foreach (var roleModel in roleModels)
// if (string.IsNullOrWhiteSpace(roleName)) {
// return; var role = channel.Guild.Roles.FirstOrDefault(r => r.Id == roleModel.RoleId);
// var role = e.Server.FindRoles(roleName).FirstOrDefault(); if (role == null)
// if (role == null) {
// { uow.SelfAssignedRoles.Remove(roleModel);
// await channel.SendMessageAsync(":anger:That role does not exist.").ConfigureAwait(false); }
// return; else
// } {
// var config = SpecificConfigurations.Default.Of(e.Server.Id); msg.Append($"**{role.Name}**, ");
// if (!config.ListOfSelfAssignableRoles.Contains(role.Id)) }
// { }
// await channel.SendMessageAsync(":anger:That role is not self-assignable.").ConfigureAwait(false); foreach (var role in toRemove)
// return; {
// } removeMsg.AppendLine($"`{role.RoleId} not found. Cleaned up.`");
// if (!imsg.Author.HasRole(role)) }
// { await uow.CompleteAsync();
// await channel.SendMessageAsync($":anger:You don't have {role.Name} role.").ConfigureAwait(false); }
// return; await channel.SendMessageAsync(msg.ToString() + "\n\n" + removeMsg.ToString()).ConfigureAwait(false);
// } }
// await imsg.Author.RemoveRoles(role).ConfigureAwait(false);
// var msg = await channel.SendMessageAsync($":ok:Successfuly removed {role.Name} role from you.").ConfigureAwait(false); [LocalizedCommand, LocalizedDescription, LocalizedSummary]
// await Task.Delay(3000).ConfigureAwait(false); [RequireContext(ContextType.Guild)]
// await msg.Delete().ConfigureAwait(false); [RequirePermission(GuildPermission.ManageRoles)]
// try public async Task Tesar(IMessage imsg)
// { {
// await e.Message.Delete().ConfigureAwait(false); var channel = (ITextChannel)imsg.Channel;
// }
// catch { } bool areExclusive;
// }); using (var uow = DbHandler.UnitOfWork())
// } {
// } var config = uow.GuildConfigs.For(channel.Guild.Id);
//}
areExclusive = config.ExclusiveSelfAssignedRoles = !config.ExclusiveSelfAssignedRoles;
await uow.CompleteAsync();
}
string exl = areExclusive ? "exclusive." : "not exclusive.";
await channel.SendMessageAsync("Self assigned roles are now " + exl);
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
public async Task Iam(IMessage imsg, [Remainder] IRole role)
{
var channel = (ITextChannel)imsg.Channel;
var guildUser = (IGuildUser)imsg.Author;
GuildConfig conf;
IEnumerable<SelfAssignedRole> roles;
using (var uow = DbHandler.UnitOfWork())
{
conf = uow.GuildConfigs.For(channel.Guild.Id);
roles = uow.SelfAssignedRoles.GetFromGuild(channel.Guild.Id);
}
SelfAssignedRole roleModel;
if ((roleModel = roles.FirstOrDefault(r=>r.RoleId == role.Id)) == null)
{
await channel.SendMessageAsync(":anger:That role is not self-assignable.").ConfigureAwait(false);
return;
}
if (guildUser.Roles.Contains(role))
{
await channel.SendMessageAsync($":anger:You already have {role.Name} role.").ConfigureAwait(false);
return;
}
if (conf.ExclusiveSelfAssignedRoles)
{
var sameRoles = guildUser.Roles.Where(r => roles.Any(rm => rm.RoleId == r.Id));
if (sameRoles.Any())
{
await channel.SendMessageAsync($":anger:You already have {sameRoles.FirstOrDefault().Name} exclusive self-assigned role.").ConfigureAwait(false);
return;
}
}
try
{
await guildUser.AddRolesAsync(role).ConfigureAwait(false);
}
catch (Exception)
{
await channel.SendMessageAsync($":anger:`I am unable to add that role to you. I can't add roles to owners or other roles higher than my role in the role hierarchy.`").ConfigureAwait(false);
return;
}
var msg = await channel.SendMessageAsync($":ok:You now have {role.Name} role.").ConfigureAwait(false);
if (conf.AutoDeleteSelfAssignedRoleMessages)
{
var t = Task.Run(async () =>
{
await Task.Delay(3000).ConfigureAwait(false);
try { await msg.DeleteAsync().ConfigureAwait(false); } catch { } // if 502 or something, i don't want bot crashing
try { await imsg.DeleteAsync().ConfigureAwait(false); } catch { }
});
}
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
public async Task Iamnot(IMessage imsg, IRole role)
{
var channel = (ITextChannel)imsg.Channel;
var guildUser = (IGuildUser)imsg.Author;
GuildConfig conf;
IEnumerable<SelfAssignedRole> roles;
using (var uow = DbHandler.UnitOfWork())
{
conf = uow.GuildConfigs.For(channel.Guild.Id);
roles = uow.SelfAssignedRoles.GetFromGuild(channel.Guild.Id);
}
SelfAssignedRole roleModel;
if ((roleModel = roles.FirstOrDefault(r => r.RoleId == role.Id)) == null)
{
await channel.SendMessageAsync(":anger:That role is not self-assignable.").ConfigureAwait(false);
return;
}
if (!guildUser.Roles.Contains(role))
{
await channel.SendMessageAsync($":anger:You don't have {role.Name} role.").ConfigureAwait(false);
return;
}
try
{
await guildUser.RemoveRolesAsync(role).ConfigureAwait(false);
}
catch (Exception)
{
await channel.SendMessageAsync($":anger:`I am unable to add that role to you. I can't remove roles to owners or other roles higher than my role in the role hierarchy.`").ConfigureAwait(false);
return;
}
var msg = await channel.SendMessageAsync($":ok: You no longer have {role.Name} role.").ConfigureAwait(false);
if (conf.AutoDeleteSelfAssignedRoleMessages)
{
var t = Task.Run(async () =>
{
await Task.Delay(3000).ConfigureAwait(false);
try { await msg.DeleteAsync().ConfigureAwait(false); } catch { } // if 502 or something, i don't want bot crashing
try { await imsg.DeleteAsync().ConfigureAwait(false); } catch { }
});
}
}
}
}
}

View File

@ -10,10 +10,11 @@ namespace NadekoBot.Services.Database
public interface IUnitOfWork : IDisposable public interface IUnitOfWork : IDisposable
{ {
IQuoteRepository Quotes { get; } IQuoteRepository Quotes { get; }
IConfigRepository GuildConfigs { get; } IGuildConfigRepository GuildConfigs { get; }
IDonatorsRepository Donators { get; } IDonatorsRepository Donators { get; }
IClashOfClansRepository ClashOfClans { get; } IClashOfClansRepository ClashOfClans { get; }
IReminderRepository Reminders { get; } IReminderRepository Reminders { get; }
ISelfAssignedRolesRepository SelfAssignedRoles { get; }
int Complete(); int Complete();
Task<int> CompleteAsync(); Task<int> CompleteAsync();

View File

@ -28,5 +28,8 @@ namespace NadekoBot.Services.Database.Models
public bool SendChannelByeMessage { get; set; } public bool SendChannelByeMessage { get; set; }
public string ChannelByeMessageText { get; set; } = "%user% has left!"; public string ChannelByeMessageText { get; set; } = "%user% has left!";
//self assignable roles
public bool ExclusiveSelfAssignedRoles { get; set; }
public bool AutoDeleteSelfAssignedRoleMessages { get; set; }
} }
} }

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Services.Database.Models
{
public class SelfAssignedRole : DbEntity
{
public ulong GuildId { get; set; }
public ulong RoleId { get; set; }
}
}

View File

@ -16,6 +16,7 @@ namespace NadekoBot.Services.Database
public DbSet<ClashWar> ClashOfClans { get; set; } public DbSet<ClashWar> ClashOfClans { get; set; }
public DbSet<ClashCaller> ClashCallers { get; set; } public DbSet<ClashCaller> ClashCallers { get; set; }
public DbSet<Reminder> Reminders { get; set; } public DbSet<Reminder> Reminders { get; set; }
public DbSet<SelfAssignedRole> SelfAssignableRoles { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
{ {
@ -52,6 +53,16 @@ namespace NadekoBot.Services.Database
.WithMany(c => c.Bases); .WithMany(c => c.Bases);
#endregion #endregion
#region Self Assignable Roles
var selfassignableRolesEntity = modelBuilder.Entity<SelfAssignedRole>();
selfassignableRolesEntity
.HasIndex(s => new { s.GuildId, s.RoleId })
.IsUnique();
#endregion
} }
protected abstract override void OnConfiguring(DbContextOptionsBuilder optionsBuilder); protected abstract override void OnConfiguring(DbContextOptionsBuilder optionsBuilder);
} }

View File

@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace NadekoBot.Services.Database.Repositories namespace NadekoBot.Services.Database.Repositories
{ {
public interface IConfigRepository : IRepository<GuildConfig> public interface IGuildConfigRepository : IRepository<GuildConfig>
{ {
GuildConfig For(ulong guildId); GuildConfig For(ulong guildId);
} }

View File

@ -0,0 +1,15 @@
using NadekoBot.Services.Database.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Services.Database.Repositories
{
public interface ISelfAssignedRolesRepository : IRepository<SelfAssignedRole>
{
bool DeleteByGuildAndRoleId(ulong guildId, ulong roleId);
IEnumerable<SelfAssignedRole> GetFromGuild(ulong guildId);
}
}

View File

@ -8,9 +8,9 @@ using Microsoft.EntityFrameworkCore;
namespace NadekoBot.Services.Database.Repositories.Impl namespace NadekoBot.Services.Database.Repositories.Impl
{ {
public class ConfigRepository : Repository<GuildConfig>, IConfigRepository public class GuildConfigRepository : Repository<GuildConfig>, IGuildConfigRepository
{ {
public ConfigRepository(DbContext context) : base(context) public GuildConfigRepository(DbContext context) : base(context)
{ {
} }
/// <summary> /// <summary>

View File

@ -0,0 +1,31 @@
using NadekoBot.Services.Database.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace NadekoBot.Services.Database.Repositories.Impl
{
public class SelfAssignedRolesRepository : Repository<SelfAssignedRole>, ISelfAssignedRolesRepository
{
public SelfAssignedRolesRepository(DbContext context) : base(context)
{
}
public bool DeleteByGuildAndRoleId(ulong guildId, ulong roleId)
{
var role = _set.Where(s => s.GuildId == guildId && s.RoleId == roleId).FirstOrDefault();
if (role == null)
return false;
_set.Remove(role);
return true;
}
public IEnumerable<SelfAssignedRole> GetFromGuild(ulong guildId) =>
_set.Where(s => s.GuildId == guildId);
}
}

View File

@ -15,8 +15,8 @@ namespace NadekoBot.Services.Database
private IQuoteRepository _quotes; private IQuoteRepository _quotes;
public IQuoteRepository Quotes => _quotes ?? (_quotes = new QuoteRepository(_context)); public IQuoteRepository Quotes => _quotes ?? (_quotes = new QuoteRepository(_context));
private IConfigRepository _guildConfigs; private IGuildConfigRepository _guildConfigs;
public IConfigRepository GuildConfigs => _guildConfigs ?? (_guildConfigs = new ConfigRepository(_context)); public IGuildConfigRepository GuildConfigs => _guildConfigs ?? (_guildConfigs = new GuildConfigRepository(_context));
private IDonatorsRepository _donators; private IDonatorsRepository _donators;
public IDonatorsRepository Donators => _donators ?? (_donators = new DonatorsRepository(_context)); public IDonatorsRepository Donators => _donators ?? (_donators = new DonatorsRepository(_context));
@ -27,6 +27,9 @@ namespace NadekoBot.Services.Database
private IReminderRepository _reminders; private IReminderRepository _reminders;
public IReminderRepository Reminders => _reminders ?? (_reminders = new ReminderRepository(_context)); public IReminderRepository Reminders => _reminders ?? (_reminders = new ReminderRepository(_context));
private ISelfAssignedRolesRepository _selfAssignedRoles;
public ISelfAssignedRolesRepository SelfAssignedRoles => _selfAssignedRoles ?? (_selfAssignedRoles = new SelfAssignedRolesRepository(_context));
public UnitOfWork(NadekoContext context) public UnitOfWork(NadekoContext context)
{ {
_context = context; _context = context;