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