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) {
cgb.CreateCommand(".greet")

View File

@ -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 ;(
/// </summary>
internal class VoicePlusTextCommand : IDiscordCommand {
public static readonly HashSet<ulong> Subscribers = new HashSet<ulong>();
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**");
});
}
}

View File

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