NadekoBot/NadekoBot.Core/Modules/Administration/AutoAssignRoleCommands.cs
2017-10-13 06:14:54 +02:00

62 lines
2.0 KiB
C#

using Discord;
using Discord.Commands;
using NadekoBot.Extensions;
using NadekoBot.Core.Services;
using System.Linq;
using System.Threading.Tasks;
using NadekoBot.Common.Attributes;
using NadekoBot.Modules.Administration.Services;
namespace NadekoBot.Modules.Administration
{
public partial class Administration
{
[Group]
public class AutoAssignRoleCommands : NadekoSubmodule<AutoAssignRoleService>
{
private readonly DbService _db;
public AutoAssignRoleCommands(DbService db)
{
_db = db;
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.ManageRoles)]
public async Task AutoAssignRole([Remainder] IRole role = null)
{
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)
{
var conf = uow.GuildConfigs.For(Context.Guild.Id, set => set);
if (role == null)
{
conf.AutoAssignRoleId = 0;
_service.AutoAssignedRoles.TryRemove(Context.Guild.Id, out _);
}
else
{
conf.AutoAssignRoleId = role.Id;
_service.AutoAssignedRoles.AddOrUpdate(Context.Guild.Id, role.Id, (key, val) => role.Id);
}
await uow.CompleteAsync().ConfigureAwait(false);
}
if (role == null)
{
await ReplyConfirmLocalized("aar_disabled").ConfigureAwait(false);
return;
}
await ReplyConfirmLocalized("aar_enabled").ConfigureAwait(false);
}
}
}
}