.tesar now has groups!

This commit is contained in:
Master Kwoth 2017-10-21 12:13:31 +02:00
parent 204cdbfb2b
commit 95ee386475
11 changed files with 2025 additions and 42 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,27 @@
using Microsoft.EntityFrameworkCore.Migrations;
using System;
using System.Collections.Generic;
namespace NadekoBot.Migrations
{
public partial class tesargrouping : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "Group",
table: "SelfAssignableRoles",
type: "INTEGER",
nullable: false,
defaultValue: 0)
.Annotation("Sqlite:Autoincrement", true);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Group",
table: "SelfAssignableRoles");
}
}
}

View File

@ -1076,6 +1076,10 @@ namespace NadekoBot.Migrations
b.Property<DateTime?>("DateAdded");
b.Property<int>("Group")
.ValueGeneratedOnAdd()
.HasDefaultValue(0);
b.Property<ulong>("GuildId");
b.Property<ulong>("RoleId");

View File

@ -11,6 +11,7 @@ using System.Text;
using System.Threading.Tasks;
using NadekoBot.Common.Attributes;
using NadekoBot.Common.Collections;
using Microsoft.EntityFrameworkCore;
namespace NadekoBot.Modules.Administration
{
@ -46,7 +47,15 @@ namespace NadekoBot.Modules.Administration
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.ManageRoles)]
public async Task Asar([Remainder] IRole role)
[Priority(1)]
public Task Asar([Remainder] IRole role) =>
Asar(0, role);
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.ManageRoles)]
[Priority(0)]
public async Task Asar(int group, [Remainder] IRole role)
{
IEnumerable<SelfAssignedRole> roles;
@ -58,7 +67,8 @@ namespace NadekoBot.Modules.Administration
var error = false;
using (var uow = _db.UnitOfWork)
{
roles = uow.SelfAssignedRoles.GetFromGuild(Context.Guild.Id);
roles = uow.SelfAssignedRoles.GetFromGuild(Context.Guild.Id)
.SelectMany(x => x);
if (roles.Any(s => s.RoleId == role.Id && s.GuildId == role.Guild.Id))
{
msg = GetText("role_in_list", Format.Bold(role.Name));
@ -68,11 +78,12 @@ namespace NadekoBot.Modules.Administration
{
uow.SelfAssignedRoles.Add(new SelfAssignedRole
{
Group = group,
RoleId = role.Id,
GuildId = role.Guild.Id
});
await uow.CompleteAsync();
msg = GetText("role_added", Format.Bold(role.Name));
msg = GetText("role_added", Format.Bold(role.Name), Format.Bold(group.ToString()));
}
}
if (error)
@ -113,40 +124,49 @@ namespace NadekoBot.Modules.Administration
var toRemove = new ConcurrentHashSet<SelfAssignedRole>();
var removeMsg = new StringBuilder();
var roles = new List<string>();
var rolesStr = new StringBuilder();
var roleCnt = 0;
var exclusive = false;
using (var uow = _db.UnitOfWork)
{
var roleModels = uow.SelfAssignedRoles.GetFromGuild(Context.Guild.Id).ToList();
foreach (var roleModel in roleModels)
exclusive = uow.GuildConfigs.For(Context.Guild.Id, set => set)
.ExclusiveSelfAssignedRoles;
var roleModels = uow.SelfAssignedRoles.GetFromGuild(Context.Guild.Id)
.ToDictionary(x => x.Key, x => x.AsEnumerable().ToArray())
.OrderBy(x => x.Key);
foreach (var kvp in roleModels)
{
var role = Context.Guild.Roles.FirstOrDefault(r => r.Id == roleModel.RoleId);
if (role == null)
rolesStr.AppendLine("\t\t\t\t『" + Format.Bold(GetText("self_assign_group", kvp.Key)) + "』");
foreach (var roleModel in kvp.Value)
{
toRemove.Add(roleModel);
uow.SelfAssignedRoles.Remove(roleModel);
}
else
{
roles.Add(Format.Bold(role.Name));
roleCnt++;
var role = Context.Guild.Roles.FirstOrDefault(r => r.Id == roleModel.RoleId);
if (role == null)
{
toRemove.Add(roleModel);
uow.SelfAssignedRoles.Remove(roleModel);
}
else
{
rolesStr.AppendLine(Format.Bold(role.Name));
roleCnt++;
}
}
}
if(toRemove.Any())
rolesStr.AppendLine("\t\t\t\t『』");
foreach (var role in toRemove)
{
roles.Add(GetText("role_clean", role.RoleId));
rolesStr.AppendLine(GetText("role_clean", role.RoleId));
}
await uow.CompleteAsync();
}
await Context.Channel.SendPaginatedConfirmAsync((DiscordSocketClient)Context.Client, page, (curPage) =>
{
return new EmbedBuilder()
.WithTitle(GetText("self_assign_list", roleCnt))
.WithDescription(string.Join("\n", roles.Skip(curPage * 10).Take(10)))
.WithOkColor();
}, roles.Count, 10);
await Context.Channel.SendConfirmAsync("",
Format.Bold(GetText("self_assign_list", roleCnt))
+ "\n\n" + rolesStr.ToString(),
footer: exclusive
? GetText("self_assign_are_exclusive")
: GetText("self_assign_are_not_exclusive")).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
@ -162,7 +182,7 @@ namespace NadekoBot.Modules.Administration
areExclusive = config.ExclusiveSelfAssignedRoles = !config.ExclusiveSelfAssignedRoles;
await uow.CompleteAsync();
}
if(areExclusive)
if (areExclusive)
await ReplyConfirmLocalized("self_assign_excl").ConfigureAwait(false);
else
await ReplyConfirmLocalized("self_assign_no_excl").ConfigureAwait(false);
@ -179,9 +199,12 @@ namespace NadekoBot.Modules.Administration
using (var uow = _db.UnitOfWork)
{
conf = uow.GuildConfigs.For(Context.Guild.Id, set => set);
roles = uow.SelfAssignedRoles.GetFromGuild(Context.Guild.Id).ToArray();
roles = uow.SelfAssignedRoles.GetFromGuild(Context.Guild.Id)
.SelectMany(x => x)
.ToArray();
}
if (roles.FirstOrDefault(r=>r.RoleId == role.Id) == null)
var theRoleYouWant = roles.FirstOrDefault(r => r.RoleId == role.Id);
if (theRoleYouWant == null)
{
await ReplyErrorLocalized("self_assign_not").ConfigureAwait(false);
return;
@ -192,10 +215,13 @@ namespace NadekoBot.Modules.Administration
return;
}
var roleIds = roles.Select(x => x.RoleId).ToArray();
var roleIds = roles
.Where(x => x.Group == theRoleYouWant.Group)
.Select(x => x.RoleId).ToArray();
if (conf.ExclusiveSelfAssignedRoles)
{
var sameRoles = guildUser.RoleIds.Where(r => roleIds.Contains(r));
var sameRoles = guildUser.RoleIds
.Where(r => roleIds.Contains(r));
foreach (var roleId in sameRoles)
{
@ -224,7 +250,7 @@ namespace NadekoBot.Modules.Administration
_log.Info(ex);
return;
}
var msg = await ReplyConfirmLocalized("self_assign_success",Format.Bold(role.Name)).ConfigureAwait(false);
var msg = await ReplyConfirmLocalized("self_assign_success", Format.Bold(role.Name)).ConfigureAwait(false);
if (conf.AutoDeleteSelfAssignedRoleMessages)
{
@ -244,7 +270,8 @@ namespace NadekoBot.Modules.Administration
using (var uow = _db.UnitOfWork)
{
autoDeleteSelfAssignedRoleMessages = uow.GuildConfigs.For(Context.Guild.Id, set => set).AutoDeleteSelfAssignedRoleMessages;
roles = uow.SelfAssignedRoles.GetFromGuild(Context.Guild.Id);
roles = uow.SelfAssignedRoles.GetFromGuild(Context.Guild.Id)
.SelectMany(x => x);
}
if (roles.FirstOrDefault(r => r.RoleId == role.Id) == null)
{
@ -253,7 +280,7 @@ namespace NadekoBot.Modules.Administration
}
if (!guildUser.RoleIds.Contains(role.Id))
{
await ReplyErrorLocalized("self_assign_not_have",Format.Bold(role.Name)).ConfigureAwait(false);
await ReplyErrorLocalized("self_assign_not_have", Format.Bold(role.Name)).ConfigureAwait(false);
return;
}
try

View File

@ -65,7 +65,7 @@ namespace NadekoBot.Core.Modules.Gambling.Services
{
var _t = Task.Run(async () =>
{
await Task.Delay(30000).ConfigureAwait(false);
await Task.Delay(60000).ConfigureAwait(false);
await _locker.WaitAsync().ConfigureAwait(false);
try
{

View File

@ -4,5 +4,7 @@
{
public ulong GuildId { get; set; }
public ulong RoleId { get; set; }
public int Group { get; set; }
}
}

View File

@ -177,6 +177,10 @@ namespace NadekoBot.Core.Services.Database
.HasIndex(s => new { s.GuildId, s.RoleId })
.IsUnique();
selfassignableRolesEntity
.Property(x => x.Group)
.HasDefaultValue(0);
#endregion
#region Currency

View File

@ -1,11 +1,12 @@
using NadekoBot.Core.Services.Database.Models;
using System.Collections.Generic;
using System.Linq;
namespace NadekoBot.Core.Services.Database.Repositories
{
public interface ISelfAssignedRolesRepository : IRepository<SelfAssignedRole>
{
bool DeleteByGuildAndRoleId(ulong guildId, ulong roleId);
IEnumerable<SelfAssignedRole> GetFromGuild(ulong guildId);
IEnumerable<IGrouping<int, SelfAssignedRole>> GetFromGuild(ulong guildId);
}
}

View File

@ -22,7 +22,9 @@ namespace NadekoBot.Core.Services.Database.Repositories.Impl
return true;
}
public IEnumerable<SelfAssignedRole> GetFromGuild(ulong guildId) =>
_set.Where(s => s.GuildId == guildId).ToList();
public IEnumerable<IGrouping<int, SelfAssignedRole>> GetFromGuild(ulong guildId)
=> _set.Where(s => s.GuildId == guildId)
.AsEnumerable()
.GroupBy(x => x.Group);
}
}

View File

@ -157,7 +157,7 @@
"administration_renrole_err": "Failed to rename role. I have insufficient permissions.",
"administration_renrole_perms": "You can't edit roles higher than your highest role.",
"administration_reprm": "Removed the playing message: {0}",
"administration_role_added": "Role {0} has been added to the list.",
"administration_role_added": "Role {0} has been added to the list in group {1}.",
"administration_role_clean": "{0} not found.Cleaned up.",
"administration_role_in_list": "Role {0} is already in the list.",
"administration_ropl_added": "Added.",
@ -166,6 +166,8 @@
"administration_ropl_list": "Here is a list of rotating statuses:\n{0}",
"administration_ropl_not_set": "No rotating playing statuses set.",
"administration_self_assign_already": "You already have {0} role.",
"administration_self_assign_are_exclusive": "You can only choose 1 role from each group.",
"administration_self_assign_are_not_exclusive": "You can choose any number of roles from any group.",
"administration_self_assign_already_excl": "You already have {0} exclusive self-assigned role.",
"administration_self_assign_excl": "Self assigned roles are now exclusive!",
"administration_self_assign_list": "There are {0} self assignable roles",
@ -176,6 +178,7 @@
"administration_self_assign_rem": "{0} has been removed from the list of self-assignable roles.",
"administration_self_assign_remove": "You no longer have {0} role.",
"administration_self_assign_success": "You now have {0} role.",
"administration_self_assign_group": "Group {0}",
"administration_setrole": "Successfully added role {0} to user {1}",
"administration_setrole_err": "Failed to add role. I have insufficient permissions.",
"administration_set_avatar": "New avatar set!",

View File

@ -204,9 +204,11 @@
},
"asar": {
"Cmd": "asar",
"Desc": "Adds a role to the list of self-assignable roles.",
"Desc": "Adds a role to the list of self-assignable roles. You can also specify a group. If 'Exclusive self-assignable roles' feature is enabled, users will be able to pick one role per group.",
"Usage": [
"{0}asar Gamer"
"{0}asar Gamer",
"{0}asar 1 Alliance",
"{0}asar 1 Horde"
]
},
"rsar": {
@ -225,7 +227,7 @@
},
"tesar": {
"Cmd": "togglexclsar tesar",
"Desc": "Toggles whether the self-assigned roles are exclusive. (So that any person can have only one of the self assignable roles)",
"Desc": "Toggles whether the self-assigned roles are exclusive. While enabled, users can only have one self-assignable role per group.",
"Usage": [
"{0}tesar"
]