NadekoBot/NadekoBot.Core/Modules/Administration/VoicePlusTextCommands.cs

131 lines
5.5 KiB
C#
Raw Normal View History

2016-08-30 14:43:54 +00:00
using Discord;
using Discord.Commands;
using NadekoBot.Extensions;
using NadekoBot.Core.Services;
2016-08-30 14:43:54 +00:00
using System;
using System.Collections.Generic;
2016-08-30 14:43:54 +00:00
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common.Attributes;
using NadekoBot.Modules.Administration.Services;
2016-08-30 14:43:54 +00:00
namespace NadekoBot.Modules.Administration
{
public partial class Administration
{
[Group]
2017-07-15 16:34:34 +00:00
public class VoicePlusTextCommands : NadekoSubmodule<VplusTService>
2016-08-30 14:43:54 +00:00
{
private readonly DbService _db;
2017-07-15 16:34:34 +00:00
public VoicePlusTextCommands(DbService db)
2016-08-30 14:43:54 +00:00
{
2017-05-27 08:19:27 +00:00
_db = db;
2016-08-30 14:43:54 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
2016-08-30 14:43:54 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageRoles)]
[RequireUserPermission(GuildPermission.ManageChannels)]
public async Task VoicePlusText()
2016-08-30 14:43:54 +00:00
{
2016-12-16 21:44:26 +00:00
var guild = Context.Guild;
2016-08-30 14:43:54 +00:00
2016-10-28 10:44:40 +00:00
var botUser = await guild.GetCurrentUserAsync().ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
if (!botUser.GuildPermissions.ManageRoles || !botUser.GuildPermissions.ManageChannels)
{
2017-03-18 08:33:13 +00:00
await ReplyErrorLocalized("vt_perms").ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
return;
}
2016-10-25 11:10:05 +00:00
if (!botUser.GuildPermissions.Administrator)
{
try
{
await ReplyErrorLocalized("vt_no_admin").ConfigureAwait(false);
}
catch
{
// ignored
2016-10-25 11:10:05 +00:00
}
}
2016-08-30 14:43:54 +00:00
try
{
bool isEnabled;
2017-05-27 08:19:27 +00:00
using (var uow = _db.UnitOfWork)
2016-08-30 14:43:54 +00:00
{
var conf = uow.GuildConfigs.For(guild.Id, set => set);
2016-08-30 14:43:54 +00:00
isEnabled = conf.VoicePlusTextEnabled = !conf.VoicePlusTextEnabled;
await uow.CompleteAsync().ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
}
2016-10-20 03:09:05 +00:00
if (!isEnabled)
2016-08-30 14:43:54 +00:00
{
2017-05-27 08:19:27 +00:00
_service.VoicePlusTextCache.TryRemove(guild.Id);
2016-12-17 00:16:14 +00:00
foreach (var textChannel in (await guild.GetTextChannelsAsync().ConfigureAwait(false)).Where(c => c.Name.EndsWith("-voice")))
2016-08-30 14:43:54 +00:00
{
try { await textChannel.DeleteAsync().ConfigureAwait(false); } catch { }
await Task.Delay(500).ConfigureAwait(false);
}
foreach (var role in guild.Roles.Where(c => c.Name.StartsWith("nvoice-")))
{
try { await role.DeleteAsync().ConfigureAwait(false); } catch { }
await Task.Delay(500).ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
}
await ReplyConfirmLocalized("vt_disabled").ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
return;
}
2017-05-27 08:19:27 +00:00
_service.VoicePlusTextCache.Add(guild.Id);
await ReplyConfirmLocalized("vt_enabled").ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
}
catch (Exception ex)
{
2016-12-16 21:44:26 +00:00
await Context.Channel.SendErrorAsync(ex.ToString()).ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
}
}
[NadekoCommand, Usage, Description, Aliases]
2016-08-30 14:43:54 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageChannels)]
[RequireBotPermission(GuildPermission.ManageChannels)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageRoles)]
//[RequireBotPermission(GuildPermission.ManageRoles)]
2016-12-16 18:43:57 +00:00
public async Task CleanVPlusT()
2016-08-30 14:43:54 +00:00
{
2016-12-16 21:44:26 +00:00
var guild = Context.Guild;
2016-10-28 10:44:40 +00:00
var botUser = await guild.GetCurrentUserAsync().ConfigureAwait(false);
if (!botUser.GuildPermissions.Administrator)
2016-08-30 14:43:54 +00:00
{
await ReplyErrorLocalized("need_admin").ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
return;
}
var textChannels = await guild.GetTextChannelsAsync().ConfigureAwait(false);
var voiceChannels = await guild.GetVoiceChannelsAsync().ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
var boundTextChannels = textChannels.Where(c => c.Name.EndsWith("-voice"));
2017-05-27 08:19:27 +00:00
var validTxtChannelNames = new HashSet<string>(voiceChannels.Select(c => _service.GetChannelName(c.Name).ToLowerInvariant()));
var invalidTxtChannels = boundTextChannels.Where(c => !validTxtChannelNames.Contains(c.Name));
2016-08-30 14:43:54 +00:00
foreach (var c in invalidTxtChannels)
{
try { await c.DeleteAsync().ConfigureAwait(false); } catch { }
await Task.Delay(500).ConfigureAwait(false);
}
var boundRoles = guild.Roles.Where(r => r.Name.StartsWith("nvoice-"));
2017-05-27 08:19:27 +00:00
var validRoleNames = new HashSet<string>(voiceChannels.Select(c => _service.GetRoleName(c).ToLowerInvariant()));
var invalidRoles = boundRoles.Where(r => !validRoleNames.Contains(r.Name));
foreach (var r in invalidRoles)
{
try { await r.DeleteAsync().ConfigureAwait(false); } catch { }
await Task.Delay(500).ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
}
await ReplyConfirmLocalized("cleaned_up").ConfigureAwait(false);
2016-08-30 14:43:54 +00:00
}
}
}
}