diff --git a/NadekoBot/Classes/ServerSpecificConfig.cs b/NadekoBot/Classes/ServerSpecificConfig.cs new file mode 100644 index 00000000..5d89034e --- /dev/null +++ b/NadekoBot/Classes/ServerSpecificConfig.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Concurrent; +using System.ComponentModel; +using System.IO; +using System.Runtime.CompilerServices; +using Newtonsoft.Json; + +namespace NadekoBot.Classes { + internal class SpecificConfigurations { + public static SpecificConfigurations Default { get; } = new SpecificConfigurations(); + + private const string filePath = "data/ServerSpecificConfigs.json"; + + static SpecificConfigurations() { } + + private SpecificConfigurations() { + + if (File.Exists(filePath)) { + try { + configs = JsonConvert + .DeserializeObject>( + File.ReadAllText(filePath)); + } + catch (Exception ex) { + Console.WriteLine($"Deserialization failing: {ex}"); + } + } + if(configs == null) + configs = new ConcurrentDictionary(); + } + + private readonly ConcurrentDictionary configs; + + public ServerSpecificConfig Of(ulong id) => + configs.GetOrAdd(id, _ => new ServerSpecificConfig()); + + private readonly object saveLock = new object(); + + public void Save() { + lock (saveLock) { + File.WriteAllText(filePath, JsonConvert.SerializeObject(configs, Formatting.Indented)); + } + } + } + + internal class ServerSpecificConfig : INotifyPropertyChanged { + [JsonProperty("VoicePlusTextEnabled")] + private bool? voicePlusTextEnabled; + [JsonIgnore] + public bool? VoicePlusTextEnabled { + get { return voicePlusTextEnabled; } + set { + voicePlusTextEnabled = value; + OnPropertyChanged(); + } + } + + public event PropertyChangedEventHandler PropertyChanged = delegate { SpecificConfigurations.Default.Save(); }; + + private void OnPropertyChanged([CallerMemberName] string propertyName = null) { + PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); + } + } +} diff --git a/NadekoBot/Commands/ServerGreetCommand.cs b/NadekoBot/Commands/ServerGreetCommand.cs index 5adf260d..f744f127 100644 --- a/NadekoBot/Commands/ServerGreetCommand.cs +++ b/NadekoBot/Commands/ServerGreetCommand.cs @@ -172,10 +172,6 @@ namespace NadekoBot.Commands { } } - public Func DoFunc() { - throw new NotImplementedException(); - } - public void Init(CommandGroupBuilder cgb) { cgb.CreateCommand(".greet") diff --git a/NadekoBot/Commands/VoicePlusTextCommand.cs b/NadekoBot/Commands/VoicePlusTextCommand.cs index fde95a33..cbaf7ae5 100644 --- a/NadekoBot/Commands/VoicePlusTextCommand.cs +++ b/NadekoBot/Commands/VoicePlusTextCommand.cs @@ -6,6 +6,7 @@ using System.Text; using System.Threading.Tasks; using Discord; using Discord.Commands; +using NadekoBot.Classes; using NadekoBot.Classes.Permissions; using ChPermOverride = Discord.ChannelPermissionOverrides; @@ -18,22 +19,24 @@ namespace NadekoBot.Commands { /// sowwy googie ;( /// internal class VoicePlusTextCommand : IDiscordCommand { - public static readonly HashSet Subscribers = new HashSet(); public VoicePlusTextCommand() { + // changing servers may cause bugs NadekoBot.Client.UserUpdated += async (sender, e) => { try { + var config = SpecificConfigurations.Default.Of(e.Server.Id); if (e.Before.VoiceChannel == e.After.VoiceChannel) return; + if (!(config.VoicePlusTextEnabled ?? false)) + return; var beforeVch = e.Before.VoiceChannel; if (beforeVch != null) { var textChannel = e.Server.FindChannels(beforeVch.Name + "-voice", ChannelType.Text).FirstOrDefault(); - if (textChannel == null) - return; - await textChannel.AddPermissionsRule(e.Before, - new ChPermOverride(readMessages: PermValue.Deny, - sendMessages: PermValue.Deny)); + if (textChannel != null) + await textChannel.AddPermissionsRule(e.Before, + new ChPermOverride(readMessages: PermValue.Deny, + sendMessages: PermValue.Deny)); } var afterVch = e.After.VoiceChannel; if (afterVch != null) { @@ -60,28 +63,35 @@ namespace NadekoBot.Commands { public void Init(CommandGroupBuilder cgb) { cgb.CreateCommand(".v+t") .Alias(".voice+text") - .Description("Creates a text channel for each voice channel only users in that voice channel can see.") + .Description("Creates a text channel for each voice channel only users in that voice channel can see." + + "If you are server owner, keep in mind you will see them all the time regardless.") .AddCheck(SimpleCheckers.ManageChannels()) .AddCheck(SimpleCheckers.CanManageRoles) .Do(async e => { - if (Subscribers.Contains(e.Server.Id)) { - Subscribers.Remove(e.Server.Id); - foreach (var textChannel in e.Server.TextChannels.Where(c => c.Name.EndsWith("-voice"))) { - var deleteTask = textChannel?.Delete(); - try { - if (deleteTask != null) - await deleteTask; - } catch { - await e.Channel.SendMessage(":anger: Error: Most likely i don't have permissions to do this."); - return; + try { + var config = SpecificConfigurations.Default.Of(e.Server.Id); + if (config.VoicePlusTextEnabled == true) { + config.VoicePlusTextEnabled = false; + foreach (var textChannel in e.Server.TextChannels.Where(c => c.Name.EndsWith("-voice"))) { + try { + await textChannel.Delete(); + } catch { + await + e.Channel.SendMessage( + ":anger: Error: Most likely i don't have permissions to do this."); + return; + } } + await e.Channel.SendMessage("Successfuly removed voice + text feature."); + return; } - await e.Channel.SendMessage("Successfuly removed voice + text feature."); - return; + config.VoicePlusTextEnabled = true; + await e.Channel.SendMessage("Successfuly enabled voice + text feature. " + + "**Make sure the bot has manage roles and manage channels permissions**"); + + } catch (Exception ex) { + await e.Channel.SendMessage(ex.ToString()); } - Subscribers.Add(e.Server.Id); - await e.Channel.SendMessage("Successfuly enabled voice + text feature. " + - "**Make sure the bot has manage roles and manage channels permissions**"); }); } } diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index 1ff487db..3078111d 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -131,6 +131,7 @@ + diff --git a/NadekoBot/bin/Debug/data/ServerSpecificConfigs.json b/NadekoBot/bin/Debug/data/ServerSpecificConfigs.json new file mode 100644 index 00000000..0f967be4 --- /dev/null +++ b/NadekoBot/bin/Debug/data/ServerSpecificConfigs.json @@ -0,0 +1,14 @@ +{ + "117523346618318850": { + "VoicePlusTextEnabled": false + }, + "156382989004046337": { + "VoicePlusTextEnabled": true + }, + "105010597954871296": { + "VoicePlusTextEnabled": null + }, + "81384788765712384": { + "VoicePlusTextEnabled": null + } +} \ No newline at end of file