2016-08-30 14:43:54 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
|
|
|
|
using NadekoBot.Extensions;
|
2017-10-13 04:14:54 +00:00
|
|
|
|
using NadekoBot.Core.Services;
|
2016-08-30 14:43:54 +00:00
|
|
|
|
using System;
|
2017-02-02 20:00:38 +00:00
|
|
|
|
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
|
|
|
|
{
|
2017-05-29 04:13:22 +00:00
|
|
|
|
private readonly DbService _db;
|
2016-12-28 05:31:39 +00:00
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2016-10-05 03:09:44 +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
|
|
|
|
|
{
|
2017-02-14 21:48:53 +00:00
|
|
|
|
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
|
|
|
|
{
|
2016-12-03 13:14:37 +00:00
|
|
|
|
var conf = uow.GuildConfigs.For(guild.Id, set => set);
|
2016-08-30 14:43:54 +00:00
|
|
|
|
isEnabled = conf.VoicePlusTextEnabled = !conf.VoicePlusTextEnabled;
|
2016-10-20 02:19:01 +00:00
|
|
|
|
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 { }
|
2017-02-02 20:00:38 +00:00
|
|
|
|
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
|
|
|
|
}
|
2017-02-14 21:48:53 +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);
|
2017-02-14 21:48:53 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-05 03:09:44 +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)]
|
2017-02-02 20:00:38 +00:00
|
|
|
|
[RequireBotPermission(GuildPermission.ManageChannels)]
|
2016-12-16 18:43:57 +00:00
|
|
|
|
[RequireUserPermission(GuildPermission.ManageRoles)]
|
2017-02-02 20:00:38 +00:00
|
|
|
|
//[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
|
|
|
|
{
|
2017-02-14 21:48:53 +00:00
|
|
|
|
await ReplyErrorLocalized("need_admin").ConfigureAwait(false);
|
2016-08-30 14:43:54 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-02 20:00:38 +00:00
|
|
|
|
var textChannels = await guild.GetTextChannelsAsync().ConfigureAwait(false);
|
|
|
|
|
var voiceChannels = await guild.GetVoiceChannelsAsync().ConfigureAwait(false);
|
2016-08-30 14:43:54 +00:00
|
|
|
|
|
2017-02-02 20:00:38 +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()));
|
2017-02-02 20:00:38 +00:00
|
|
|
|
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 { }
|
2017-02-02 20:00:38 +00:00
|
|
|
|
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()));
|
2017-02-02 20:00:38 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2017-02-14 21:48:53 +00:00
|
|
|
|
await ReplyConfirmLocalized("cleaned_up").ConfigureAwait(false);
|
2016-08-30 14:43:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-28 05:31:39 +00:00
|
|
|
|
}
|