fixed v+t and made a class/file for server specific settings (data/ServerSpecificConfig.json)

This commit is contained in:
Master Kwoth 2016-03-10 17:46:58 +01:00
parent ff67e35d37
commit 655a4920fb
5 changed files with 111 additions and 26 deletions

View File

@ -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<ConcurrentDictionary<ulong, ServerSpecificConfig>>(
File.ReadAllText(filePath));
}
catch (Exception ex) {
Console.WriteLine($"Deserialization failing: {ex}");
}
}
if(configs == null)
configs = new ConcurrentDictionary<ulong, ServerSpecificConfig>();
}
private readonly ConcurrentDictionary<ulong, ServerSpecificConfig> 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));
}
}
}

View File

@ -172,10 +172,6 @@ namespace NadekoBot.Commands {
} }
} }
public Func<CommandEventArgs, Task> DoFunc() {
throw new NotImplementedException();
}
public void Init(CommandGroupBuilder cgb) { public void Init(CommandGroupBuilder cgb) {
cgb.CreateCommand(".greet") cgb.CreateCommand(".greet")

View File

@ -6,6 +6,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord; using Discord;
using Discord.Commands; using Discord.Commands;
using NadekoBot.Classes;
using NadekoBot.Classes.Permissions; using NadekoBot.Classes.Permissions;
using ChPermOverride = Discord.ChannelPermissionOverrides; using ChPermOverride = Discord.ChannelPermissionOverrides;
@ -18,22 +19,24 @@ namespace NadekoBot.Commands {
/// sowwy googie ;( /// sowwy googie ;(
/// </summary> /// </summary>
internal class VoicePlusTextCommand : IDiscordCommand { internal class VoicePlusTextCommand : IDiscordCommand {
public static readonly HashSet<ulong> Subscribers = new HashSet<ulong>();
public VoicePlusTextCommand() { public VoicePlusTextCommand() {
// changing servers may cause bugs
NadekoBot.Client.UserUpdated += async (sender, e) => { NadekoBot.Client.UserUpdated += async (sender, e) => {
try { try {
var config = SpecificConfigurations.Default.Of(e.Server.Id);
if (e.Before.VoiceChannel == e.After.VoiceChannel) return; if (e.Before.VoiceChannel == e.After.VoiceChannel) return;
if (!(config.VoicePlusTextEnabled ?? false))
return;
var beforeVch = e.Before.VoiceChannel; var beforeVch = e.Before.VoiceChannel;
if (beforeVch != null) { if (beforeVch != null) {
var textChannel = var textChannel =
e.Server.FindChannels(beforeVch.Name + "-voice", ChannelType.Text).FirstOrDefault(); e.Server.FindChannels(beforeVch.Name + "-voice", ChannelType.Text).FirstOrDefault();
if (textChannel == null) if (textChannel != null)
return; await textChannel.AddPermissionsRule(e.Before,
await textChannel.AddPermissionsRule(e.Before, new ChPermOverride(readMessages: PermValue.Deny,
new ChPermOverride(readMessages: PermValue.Deny, sendMessages: PermValue.Deny));
sendMessages: PermValue.Deny));
} }
var afterVch = e.After.VoiceChannel; var afterVch = e.After.VoiceChannel;
if (afterVch != null) { if (afterVch != null) {
@ -60,28 +63,35 @@ namespace NadekoBot.Commands {
public void Init(CommandGroupBuilder cgb) { public void Init(CommandGroupBuilder cgb) {
cgb.CreateCommand(".v+t") cgb.CreateCommand(".v+t")
.Alias(".voice+text") .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.ManageChannels())
.AddCheck(SimpleCheckers.CanManageRoles) .AddCheck(SimpleCheckers.CanManageRoles)
.Do(async e => { .Do(async e => {
if (Subscribers.Contains(e.Server.Id)) { try {
Subscribers.Remove(e.Server.Id); var config = SpecificConfigurations.Default.Of(e.Server.Id);
foreach (var textChannel in e.Server.TextChannels.Where(c => c.Name.EndsWith("-voice"))) { if (config.VoicePlusTextEnabled == true) {
var deleteTask = textChannel?.Delete(); config.VoicePlusTextEnabled = false;
try { foreach (var textChannel in e.Server.TextChannels.Where(c => c.Name.EndsWith("-voice"))) {
if (deleteTask != null) try {
await deleteTask; await textChannel.Delete();
} catch { } catch {
await e.Channel.SendMessage(":anger: Error: Most likely i don't have permissions to do this."); await
return; 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."); config.VoicePlusTextEnabled = true;
return; 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**");
}); });
} }
} }

View File

@ -131,6 +131,7 @@
<Compile Include="Classes\Permissions\PermissionsHandler.cs" /> <Compile Include="Classes\Permissions\PermissionsHandler.cs" />
<Compile Include="Classes\Permissions\SimpleCheckers.cs" /> <Compile Include="Classes\Permissions\SimpleCheckers.cs" />
<Compile Include="Classes\SearchHelper.cs" /> <Compile Include="Classes\SearchHelper.cs" />
<Compile Include="Classes\ServerSpecificConfig.cs" />
<Compile Include="Classes\_DataModels\AnnouncementModel.cs" /> <Compile Include="Classes\_DataModels\AnnouncementModel.cs" />
<Compile Include="Classes\_DataModels\CommandModel.cs" /> <Compile Include="Classes\_DataModels\CommandModel.cs" />
<Compile Include="Classes\_DataModels\CurrencyStateModel.cs" /> <Compile Include="Classes\_DataModels\CurrencyStateModel.cs" />

View File

@ -0,0 +1,14 @@
{
"117523346618318850": {
"VoicePlusTextEnabled": false
},
"156382989004046337": {
"VoicePlusTextEnabled": true
},
"105010597954871296": {
"VoicePlusTextEnabled": null
},
"81384788765712384": {
"VoicePlusTextEnabled": null
}
}