NadekoBot/NadekoBot.Core/Modules/Administration/AutoAssignRoleCommands.cs

62 lines
2.0 KiB
C#
Raw Normal View History

2016-08-24 22:39:02 +00:00
using Discord;
using Discord.Commands;
using NadekoBot.Extensions;
2016-08-24 22:39:02 +00:00
using NadekoBot.Services;
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
{
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
{
_db = db;
2016-08-24 22:39:02 +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)]
public async Task AutoAssignRole([Remainder] IRole role = null)
2016-08-24 22:39:02 +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;
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;
_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
}
}
}
}