2016-08-25 00:39:02 +02:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
2017-04-03 23:18:18 +02:00
|
|
|
|
using NadekoBot.Extensions;
|
2017-10-13 06:14:54 +02:00
|
|
|
|
using NadekoBot.Core.Services;
|
2016-08-25 00:39:02 +02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-07-17 21:42:36 +02:00
|
|
|
|
using NadekoBot.Common.Attributes;
|
|
|
|
|
using NadekoBot.Modules.Administration.Services;
|
2016-08-25 00:39:02 +02:00
|
|
|
|
|
|
|
|
|
namespace NadekoBot.Modules.Administration
|
|
|
|
|
{
|
|
|
|
|
public partial class Administration
|
|
|
|
|
{
|
|
|
|
|
[Group]
|
2017-07-15 18:34:34 +02:00
|
|
|
|
public class AutoAssignRoleCommands : NadekoSubmodule<AutoAssignRoleService>
|
2016-08-25 00:39:02 +02:00
|
|
|
|
{
|
2017-05-29 06:13:22 +02:00
|
|
|
|
private readonly DbService _db;
|
2016-10-18 03:03:46 +02:00
|
|
|
|
|
2017-07-15 18:34:34 +02:00
|
|
|
|
public AutoAssignRoleCommands(DbService db)
|
2016-08-25 00:39:02 +02:00
|
|
|
|
{
|
2017-05-27 19:42:23 +02:00
|
|
|
|
_db = db;
|
2016-08-25 00:39:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-05 05:09:44 +02:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2016-08-25 00:39:02 +02:00
|
|
|
|
[RequireContext(ContextType.Guild)]
|
2016-12-16 19:43:57 +01:00
|
|
|
|
[RequireUserPermission(GuildPermission.ManageRoles)]
|
2016-12-21 09:33:47 +01:00
|
|
|
|
public async Task AutoAssignRole([Remainder] IRole role = null)
|
2016-08-25 00:39:02 +02:00
|
|
|
|
{
|
2017-04-03 23:18:18 +02:00
|
|
|
|
var guser = (IGuildUser)Context.User;
|
|
|
|
|
if (role != null)
|
|
|
|
|
if (Context.User.Id != guser.Guild.OwnerId && guser.GetRoles().Max(x => x.Position) <= role.Position)
|
|
|
|
|
return;
|
|
|
|
|
|
2017-05-27 19:42:23 +02:00
|
|
|
|
using (var uow = _db.UnitOfWork)
|
2016-08-25 00:39:02 +02:00
|
|
|
|
{
|
2017-02-14 16:55:59 +01:00
|
|
|
|
var conf = uow.GuildConfigs.For(Context.Guild.Id, set => set);
|
2016-08-25 00:39:02 +02:00
|
|
|
|
if (role == null)
|
2016-12-22 06:32:39 +01:00
|
|
|
|
{
|
2016-08-25 00:39:02 +02:00
|
|
|
|
conf.AutoAssignRoleId = 0;
|
2017-07-25 18:31:30 +02:00
|
|
|
|
_service.AutoAssignedRoles.TryRemove(Context.Guild.Id, out _);
|
2016-12-22 06:32:39 +01:00
|
|
|
|
}
|
2016-08-25 00:39:02 +02:00
|
|
|
|
else
|
2016-12-22 06:32:39 +01:00
|
|
|
|
{
|
2016-08-25 00:39:02 +02:00
|
|
|
|
conf.AutoAssignRoleId = role.Id;
|
2017-05-27 19:42:23 +02:00
|
|
|
|
_service.AutoAssignedRoles.AddOrUpdate(Context.Guild.Id, role.Id, (key, val) => role.Id);
|
2016-12-22 06:32:39 +01:00
|
|
|
|
}
|
2016-08-25 00:39:02 +02:00
|
|
|
|
|
2016-08-25 03:19:06 +02:00
|
|
|
|
await uow.CompleteAsync().ConfigureAwait(false);
|
2016-08-25 00:39:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (role == null)
|
|
|
|
|
{
|
2017-02-14 16:55:59 +01:00
|
|
|
|
await ReplyConfirmLocalized("aar_disabled").ConfigureAwait(false);
|
2016-08-25 00:39:02 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-14 16:55:59 +01:00
|
|
|
|
await ReplyConfirmLocalized("aar_enabled").ConfigureAwait(false);
|
2016-08-25 00:39:02 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|