NadekoBot/NadekoBot.Core/Modules/Administration/VcRoleCommands.cs

108 lines
4.2 KiB
C#
Raw Normal View History

using System.Collections.Concurrent;
2017-03-11 07:00:58 +00:00
using System.Linq;
using Discord;
using Discord.Commands;
using System.Threading.Tasks;
using Discord.WebSocket;
using Microsoft.EntityFrameworkCore;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common.Attributes;
2017-03-11 07:00:58 +00:00
using NadekoBot.Extensions;
2017-07-17 19:42:36 +00:00
using NadekoBot.Modules.Administration.Services;
using NadekoBot.Core.Services;
using NadekoBot.Core.Services.Database.Models;
2017-03-11 07:00:58 +00:00
namespace NadekoBot.Modules.Administration
{
public partial class Administration
{
[Group]
2017-07-15 16:34:34 +00:00
public class VcRoleCommands : NadekoSubmodule<VcRoleService>
2017-03-11 07:00:58 +00:00
{
private readonly DbService _db;
2017-03-11 07:00:58 +00:00
2017-07-15 16:34:34 +00:00
public VcRoleCommands(DbService db)
2017-03-11 07:00:58 +00:00
{
2017-05-27 08:19:27 +00:00
_db = db;
2017-03-11 07:00:58 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
[RequireUserPermission(GuildPermission.ManageRoles)]
[RequireUserPermission(GuildPermission.ManageChannels)]
[RequireBotPermission(GuildPermission.ManageRoles)]
//todo 999 discord.net [RequireBotPermission(GuildPermission.ManageChannels)]
2017-03-11 07:00:58 +00:00
[RequireContext(ContextType.Guild)]
public async Task VcRole([Remainder]IRole role = null)
{
var user = (IGuildUser) Context.User;
var vc = user.VoiceChannel;
if (vc == null || vc.GuildId != user.GuildId)
{
await ReplyErrorLocalized("must_be_in_voice").ConfigureAwait(false);
return;
}
2017-05-27 08:19:27 +00:00
var guildVcRoles = _service.VcRoles.GetOrAdd(user.GuildId, new ConcurrentDictionary<ulong, IRole>());
2017-03-11 07:00:58 +00:00
if (role == null)
{
if (guildVcRoles.TryRemove(vc.Id, out role))
{
await ReplyConfirmLocalized("vcrole_removed", Format.Bold(vc.Name)).ConfigureAwait(false);
2017-05-27 08:19:27 +00:00
using (var uow = _db.UnitOfWork)
2017-03-11 07:00:58 +00:00
{
var conf = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.VcRoleInfos));
conf.VcRoleInfos.RemoveWhere(x => x.VoiceChannelId == vc.Id);
uow.Complete();
}
}
}
else
{
guildVcRoles.AddOrUpdate(vc.Id, role, (key, old) => role);
2017-05-27 08:19:27 +00:00
using (var uow = _db.UnitOfWork)
2017-03-11 07:00:58 +00:00
{
var conf = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.VcRoleInfos));
conf.VcRoleInfos.RemoveWhere(x => x.VoiceChannelId == vc.Id); // remove old one
conf.VcRoleInfos.Add(new VcRoleInfo()
{
VoiceChannelId = vc.Id,
RoleId = role.Id,
}); // add new one
uow.Complete();
}
await ReplyConfirmLocalized("vcrole_added", Format.Bold(vc.Name), Format.Bold(role.Name)).ConfigureAwait(false);
}
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task VcRoleList()
{
var guild = (SocketGuild) Context.Guild;
string text;
2017-05-27 08:19:27 +00:00
if (_service.VcRoles.TryGetValue(Context.Guild.Id, out ConcurrentDictionary<ulong, IRole> roles))
2017-03-11 07:00:58 +00:00
{
if (!roles.Any())
{
text = GetText("no_vcroles");
}
else
{
text = string.Join("\n", roles.Select(x =>
$"{Format.Bold(guild.GetVoiceChannel(x.Key)?.Name ?? x.Key.ToString())} => {x.Value}"));
}
}
else
{
text = GetText("no_vcroles");
}
await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
.WithTitle(GetText("vc_role_list"))
.WithDescription(text))
.ConfigureAwait(false);
}
}
}
}