Self assignable roles are implemented.

This commit is contained in:
Master Kwoth 2016-03-15 16:32:03 +01:00
parent 2c17c7b223
commit 19deee6597
6 changed files with 139 additions and 15 deletions

View File

@ -1,5 +1,7 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.IO; using System.IO;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@ -8,6 +10,7 @@ using Newtonsoft.Json;
namespace NadekoBot.Classes { namespace NadekoBot.Classes {
internal class SpecificConfigurations { internal class SpecificConfigurations {
public static SpecificConfigurations Default { get; } = new SpecificConfigurations(); public static SpecificConfigurations Default { get; } = new SpecificConfigurations();
public static bool Instantiated { get; set; } = false;
private const string filePath = "data/ServerSpecificConfigs.json"; private const string filePath = "data/ServerSpecificConfigs.json";
@ -26,6 +29,7 @@ namespace NadekoBot.Classes {
} }
if (configs == null) if (configs == null)
configs = new ConcurrentDictionary<ulong, ServerSpecificConfig>(); configs = new ConcurrentDictionary<ulong, ServerSpecificConfig>();
Instantiated = true;
} }
private readonly ConcurrentDictionary<ulong, ServerSpecificConfig> configs; private readonly ConcurrentDictionary<ulong, ServerSpecificConfig> configs;
@ -64,9 +68,28 @@ namespace NadekoBot.Classes {
} }
} }
[JsonIgnore]
private ObservableCollection<ulong> listOfSelfAssignableRoles;
public ObservableCollection<ulong> ListOfSelfAssignableRoles {
get { return listOfSelfAssignableRoles; }
set {
listOfSelfAssignableRoles = value;
if (value != null)
listOfSelfAssignableRoles.CollectionChanged += (s, e) => {
if (!SpecificConfigurations.Instantiated) return;
OnPropertyChanged();
};
}
}
public ServerSpecificConfig() {
ListOfSelfAssignableRoles = new ObservableCollection<ulong>();
}
public event PropertyChangedEventHandler PropertyChanged = delegate { SpecificConfigurations.Default.Save(); }; public event PropertyChangedEventHandler PropertyChanged = delegate { SpecificConfigurations.Default.Save(); };
private void OnPropertyChanged([CallerMemberName] string propertyName = null) { private void OnPropertyChanged([CallerMemberName] string propertyName = null) {
Console.WriteLine("property changed");
PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
} }
} }

View File

@ -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<ulong>();
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.");
});
}
}
}

View File

@ -22,6 +22,7 @@ namespace NadekoBot.Modules {
commands.Add(new RatelimitCommand(this)); commands.Add(new RatelimitCommand(this));
commands.Add(new VoicePlusTextCommand(this)); commands.Add(new VoicePlusTextCommand(this));
commands.Add(new CrossServerTextChannel(this)); commands.Add(new CrossServerTextChannel(this));
commands.Add(new SelfAssignedRolesCommand(this));
} }
public override string Prefix { get; } = NadekoBot.Config.CommandPrefixes.Administration; public override string Prefix { get; } = NadekoBot.Config.CommandPrefixes.Administration;

View File

@ -25,7 +25,7 @@ namespace NadekoBot {
private static void Main() { private static void Main() {
Console.OutputEncoding = Encoding.Unicode; Console.OutputEncoding = Encoding.Unicode;
// generate credentials example so people can know about the changes i make // generate credentials example so people can know about the changes i make
try { try {
File.WriteAllText("credentials_example.json", JsonConvert.SerializeObject(new Credentials(), Formatting.Indented)); File.WriteAllText("credentials_example.json", JsonConvert.SerializeObject(new Credentials(), Formatting.Indented));

View File

@ -146,6 +146,7 @@
<Compile Include="Classes\_DataModels\UserQuoteModel.cs" /> <Compile Include="Classes\_DataModels\UserQuoteModel.cs" />
<Compile Include="Commands\BetrayGame.cs" /> <Compile Include="Commands\BetrayGame.cs" />
<Compile Include="Commands\CrossServerTextChannel.cs" /> <Compile Include="Commands\CrossServerTextChannel.cs" />
<Compile Include="Commands\SelfAssignedRolesCommand.cs" />
<Compile Include="Modules\ClashOfClans.cs" /> <Compile Include="Modules\ClashOfClans.cs" />
<Compile Include="Commands\FilterWordsCommand.cs" /> <Compile Include="Commands\FilterWordsCommand.cs" />
<Compile Include="Commands\FilterInvitesCommand.cs" /> <Compile Include="Commands\FilterInvitesCommand.cs" />

View File

@ -1,14 +0,0 @@
{
"143857445461164032": {
"VoicePlusTextEnabled": false,
"SendPrivateMessageOnMention": false
},
"117523346618318850": {
"VoicePlusTextEnabled": false,
"SendPrivateMessageOnMention": true
},
"81384788765712384": {
"VoicePlusTextEnabled": false,
"SendPrivateMessageOnMention": false
}
}