private static JToken writePermissions(PermissionDictionary <ulong> permissions) { var entries = new JObject(); foreach (var p in permissions) { entries.Add(p.Key.ToString(), string.Join(" ", p.Value)); } return(entries); }
public Permission Check(TKey key, string value, HashSet <string> @default = null) { if (@default != null) { var parent = new PermissionDictionary <object>(); parent.Add("", @default); return(Check(key, value, () => parent.GetMode(""), x => parent.Check("", x))); } else { return(Check(key, value, null, null)); } }
private static PermissionDictionary <ulong> readPermissions(JObject node) { var result = new PermissionDictionary <ulong>(); foreach (var entry in node) { ulong key = ulong.Parse(entry.Key); var values = new HashSet <string>(); foreach (var value in entry.Value.Value <string>().Split(' ')) { values.Add(value); } result.Add(key, values); } return(result); }
public async Task AssignPermissions(string user_or_role, [Remainder] string permissions) { if (Context.Guild != null) { // assign role permissions var role = Utils.ParseRole(Context.Guild, user_or_role); if (role != null) { PermissionDictionary <ulong> permissions_; if (Context.Bot.Config.commandsRolePermissions.ContainsKey(Context.Guild.Id)) { permissions_ = Context.Bot.Config.commandsRolePermissions[Context.Guild.Id]; } else { permissions_ = new PermissionDictionary <ulong>(); Context.Bot.Config.commandsRolePermissions.Add(Context.Guild.Id, permissions_); } foreach (var permission in permissions.Split(' ')) { permissions_.Assign(role.Id, permission); } Context.Bot.UpdateConfig(); return; } } // assign user permissions var user = Utils.ParseUser(Context.Discord, user_or_role); if (user == null) { await ReplyAsync(Say(BotString.warning_badUser)); return; } foreach (var command in permissions.Split(' ')) { Context.Bot.Config.commandsUserPermissions.Assign(user.Id, command); } Context.Bot.UpdateConfig(); }
public void Load(JToken root) { var stack = new JTokenStack(root); // #general stack.Push("general"); botToken = (string)stack.Get("botToken"); stack.Pop(); // # // #voice stack.Push("voice"); // #voice - speak stack.Push("speak"); speakEnabled = (bool)stack.Get("enabled"); speakRecordingDevice = (string)stack.Get("recordingDevice"); speakAudioType = (AudioApplication)Enum.Parse(typeof(AudioApplication), (string)stack.Get("audioType")); speakBitRate = (int?)stack.Get("bitRate"); speakBufferMillis = (int)stack.Get("bufferMillis"); stack.Pop(); // #voice // #voice - listen stack.Push("listen"); listenEnabled = (bool)stack.Get("enabled"); listenPlaybackDevice = (string)stack.Get("playbackDevice"); stack.Pop(); // #voice voiceAutoJoinVoiceChannels = readVoiceChannels((JObject)stack.Get("autoJoinVoiceChannels")); stack.Pop(); // # // #text stack.Push("text"); // #text - commands stack.Push("commands"); commandsBotAgent = (string)stack.Get("botAgent"); if (commandsBotAgent == null) { throw new FormatException("bot agent cannot be null"); } // #text - commands - permissions stack.Push("permissions"); commandsDefaultPermissions.Clear(); foreach (var defaultPermission in stack.Get("defaultPermissions").Value <string>().Split(' ')) { commandsDefaultPermissions.Add(defaultPermission); } commandsUserPermissions = readUserPermissions((JObject)stack.Get("userPermissions")); commandsRolePermissions = readRolePermissions((JObject)stack.Get("rolePermissions")); stack.Pop(); // #text - commands stack.Pop(); // #text stack.Pop(); // # stack.Pop(); }
private static JToken writeUserPermissions(PermissionDictionary <ulong> userPermissions) { return(writePermissions(userPermissions)); }
public async Task AssignPermissions(string user_or_role, [Remainder] string permissions) { var uncheckedPermissions = permissions.Split(' '); var invalidPermissions = new List <String>(); var validPermissions = new List <String>(); foreach (var permission in permissions.Split(' ')) { if (isValidPermissionEntry(permission)) { validPermissions.Add(permission); } else { invalidPermissions.Add(permission); } } if (invalidPermissions.Count > 0) { await ReplyAsync(Say(BotString.warning_unrecognizedPermissionEntries, string.Join(", ", invalidPermissions.Select(p => "`" + p + "`")))); } if (Context.Guild != null) { // assign role permissions var role = Utils.ParseRole(Context.Guild, user_or_role); if (role != null) { PermissionDictionary <ulong> permissions_; if (Context.Bot.Config.commandsRolePermissions.ContainsKey(Context.Guild.Id)) { permissions_ = Context.Bot.Config.commandsRolePermissions[Context.Guild.Id]; } else { permissions_ = new PermissionDictionary <ulong>(); Context.Bot.Config.commandsRolePermissions.Add(Context.Guild.Id, permissions_); } foreach (var permission in validPermissions) { permissions_.Assign(role.Id, permission); } Context.Bot.UpdateConfig(); return; } } // assign user permissions var user = Utils.ParseUser(Context.Discord, user_or_role); if (user == null) { await ReplyAsync(Say(BotString.warning_badUser)); return; } foreach (var command in validPermissions) { Context.Bot.Config.commandsUserPermissions.Assign(user.Id, command); } Context.Bot.UpdateConfig(); }