diff --git a/NadekoBot/Classes/ServerSpecificConfig.cs b/NadekoBot/Classes/ServerSpecificConfig.cs index 43b0cbd1..15da9d4b 100644 --- a/NadekoBot/Classes/ServerSpecificConfig.cs +++ b/NadekoBot/Classes/ServerSpecificConfig.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Collections.ObjectModel; using System.ComponentModel; using System.IO; using System.Runtime.CompilerServices; @@ -8,6 +10,7 @@ using Newtonsoft.Json; namespace NadekoBot.Classes { internal class SpecificConfigurations { public static SpecificConfigurations Default { get; } = new SpecificConfigurations(); + public static bool Instantiated { get; set; } = false; private const string filePath = "data/ServerSpecificConfigs.json"; @@ -26,6 +29,7 @@ namespace NadekoBot.Classes { } if (configs == null) configs = new ConcurrentDictionary(); + Instantiated = true; } private readonly ConcurrentDictionary configs; @@ -64,9 +68,28 @@ namespace NadekoBot.Classes { } } + [JsonIgnore] + private ObservableCollection listOfSelfAssignableRoles; + public ObservableCollection ListOfSelfAssignableRoles { + get { return listOfSelfAssignableRoles; } + set { + listOfSelfAssignableRoles = value; + if (value != null) + listOfSelfAssignableRoles.CollectionChanged += (s, e) => { + if (!SpecificConfigurations.Instantiated) return; + OnPropertyChanged(); + }; + } + } + + public ServerSpecificConfig() { + ListOfSelfAssignableRoles = new ObservableCollection(); + } + public event PropertyChangedEventHandler PropertyChanged = delegate { SpecificConfigurations.Default.Save(); }; private void OnPropertyChanged([CallerMemberName] string propertyName = null) { + Console.WriteLine("property changed"); PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } diff --git a/NadekoBot/Commands/SelfAssignedRolesCommand.cs b/NadekoBot/Commands/SelfAssignedRolesCommand.cs new file mode 100644 index 00000000..ea22197e --- /dev/null +++ b/NadekoBot/Commands/SelfAssignedRolesCommand.cs @@ -0,0 +1,113 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.Eventing.Reader; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Discord; +using Discord.Commands; +using NadekoBot.Classes; +using NadekoBot.Classes.Permissions; +using NadekoBot.Modules; + +namespace NadekoBot.Commands { + internal class SelfAssignedRolesCommand : DiscordCommand { + public SelfAssignedRolesCommand(DiscordModule module) : base(module) { } + internal override void Init(CommandGroupBuilder cgb) { + cgb.CreateCommand(".asar") + .Description("Adds a role, or list of roles separated by whitespace" + + "(use quotations for multiword roles) to the list of self-assignable roles.\n**Usage**: .asar Gamer") + .Parameter("roles", ParameterType.Multiple) + .AddCheck(SimpleCheckers.CanManageRoles) + .Do(async e => { + var config = SpecificConfigurations.Default.Of(e.Server.Id); + var msg = new StringBuilder(); + foreach (var arg in e.Args) { + var role = e.Server.FindRoles(arg.Trim()).FirstOrDefault(); + if (role == null) + msg.AppendLine($":anger:Role **{arg}** not found."); + else { + if (config.ListOfSelfAssignableRoles.Contains(role.Id)) { + msg.AppendLine($":anger:Role **{role.Name}** is already in the list."); + continue; + } + config.ListOfSelfAssignableRoles.Add(role.Id); + msg.AppendLine($":ok:Role **{role.Name}** added to the list."); + } + } + await e.Channel.SendMessage(msg.ToString()); + }); + + cgb.CreateCommand(".rsar") + .Description("Removes a specified role from the list of self-assignable roles.") + .Parameter("role", ParameterType.Unparsed) + .AddCheck(SimpleCheckers.CanManageRoles) + .Do(async e => { + var roleName = e.GetArg("role")?.Trim(); + if (string.IsNullOrWhiteSpace(roleName)) + return; + var role = e.Server.FindRoles(roleName).FirstOrDefault(); + if (role == null) { + await e.Channel.SendMessage(":anger:That role does not exist."); + return; + } + var config = SpecificConfigurations.Default.Of(e.Server.Id); + if (!config.ListOfSelfAssignableRoles.Contains(role.Id)) { + await e.Channel.SendMessage(":anger:That role is not self-assignable."); + return; + } + config.ListOfSelfAssignableRoles.Remove(role.Id); + await e.Channel.SendMessage($":ok:**{role.Name}** has been removed from the list of self-assignable roles"); + }); + + cgb.CreateCommand(".lsar") + .Description("Lits all self-assignable roles.") + .Parameter("roles", ParameterType.Multiple) + .Do(async e => { + var config = SpecificConfigurations.Default.Of(e.Server.Id); + var msg = new StringBuilder($"There are `{config.ListOfSelfAssignableRoles.Count}` self assignable roles:\n"); + var toRemove = new HashSet(); + foreach (var roleId in config.ListOfSelfAssignableRoles) { + var role = e.Server.GetRole(roleId); + if (role == null) { + msg.Append($"`{roleId} not found. Cleaned up.`, "); + toRemove.Add(roleId); + } else { + msg.Append($"**{role.Name}**, "); + } + } + foreach (var id in toRemove) { + config.ListOfSelfAssignableRoles.Remove(id); + } + await e.Channel.SendMessage(msg.ToString()); + }); + + cgb.CreateCommand(".iam") + .Description("Adds a role to you that you choose. " + + "Role must be on a list of self-assignable roles." + + "\n**Usage**: .iam Gamer") + .Parameter("role", ParameterType.Unparsed) + .Do(async e => { + var roleName = e.GetArg("role")?.Trim(); + if (string.IsNullOrWhiteSpace(roleName)) + return; + var role = e.Server.FindRoles(roleName).FirstOrDefault(); + if (role == null) { + await e.Channel.SendMessage(":anger:That role does not exist."); + return; + } + var config = SpecificConfigurations.Default.Of(e.Server.Id); + if (!config.ListOfSelfAssignableRoles.Contains(role.Id)) { + await e.Channel.SendMessage(":anger:That role is not self-assignable."); + return; + } + if (e.User.HasRole(role)) { + await e.Channel.SendMessage($":anger:You already have {role.Name} role."); + return; + } + await e.User.AddRoles(role); + await e.Channel.SendMessage($":ok:You now have {role.Name} role."); + }); + } + } +} diff --git a/NadekoBot/Modules/Administration.cs b/NadekoBot/Modules/Administration.cs index e8babf0a..ccb59abf 100644 --- a/NadekoBot/Modules/Administration.cs +++ b/NadekoBot/Modules/Administration.cs @@ -22,6 +22,7 @@ namespace NadekoBot.Modules { commands.Add(new RatelimitCommand(this)); commands.Add(new VoicePlusTextCommand(this)); commands.Add(new CrossServerTextChannel(this)); + commands.Add(new SelfAssignedRolesCommand(this)); } public override string Prefix { get; } = NadekoBot.Config.CommandPrefixes.Administration; diff --git a/NadekoBot/NadekoBot.cs b/NadekoBot/NadekoBot.cs index 2be8faad..52b04cbc 100644 --- a/NadekoBot/NadekoBot.cs +++ b/NadekoBot/NadekoBot.cs @@ -25,7 +25,7 @@ namespace NadekoBot { private static void Main() { Console.OutputEncoding = Encoding.Unicode; - + // generate credentials example so people can know about the changes i make try { File.WriteAllText("credentials_example.json", JsonConvert.SerializeObject(new Credentials(), Formatting.Indented)); diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index 0cc311c7..5999d683 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -146,6 +146,7 @@ + diff --git a/NadekoBot/bin/Debug/data/ServerSpecificConfigs.json b/NadekoBot/bin/Debug/data/ServerSpecificConfigs.json deleted file mode 100644 index 61f8f887..00000000 --- a/NadekoBot/bin/Debug/data/ServerSpecificConfigs.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "143857445461164032": { - "VoicePlusTextEnabled": false, - "SendPrivateMessageOnMention": false - }, - "117523346618318850": { - "VoicePlusTextEnabled": false, - "SendPrivateMessageOnMention": true - }, - "81384788765712384": { - "VoicePlusTextEnabled": false, - "SendPrivateMessageOnMention": false - } -} \ No newline at end of file