private static async Task GuildUser_LevelUp(GuildUser myGuildUser) { ulong guildId = myGuildUser.GuildId; if (RankRoleCollection.ContainsKey(myGuildUser.GuildId)) { SocketGuild guild = client.GetGuild(myGuildUser.GuildId); Dictionary <int, ulong> SpecificRoleCollection = RankRoleCollection[guildId]; //check if user level is a key if (SpecificRoleCollection.ContainsKey((int)myGuildUser.Level)) { //get value of key-specific value ulong roleId = SpecificRoleCollection.GetValueOrDefault((int)myGuildUser.Level); //get user from guild SocketGuildUser guildUser = guild.Users.ToList().Find(u => u.Id == myGuildUser.Id); //remove any other level-specific roles foreach (KeyValuePair <int, ulong> entry in SpecificRoleCollection) { if (guildUser.Roles.ToList().Find(r => r.Id == entry.Value) != null) { await guildUser.RemoveRoleAsync(guild.Roles.ToList().Find(r => r.Id == entry.Value)); } } //add new role to user await guildUser.AddRoleAsync(myGuild.Roles.ToList().Find(r => r.Id == roleId)); } CreateOrAddRole(guild, $"Level {myGuildUser.Level}", myGuildUser.Id, new[] { $"Level " }); } }
private static void fetchAllServer() { if (!File.Exists("meta/rankRoles.json")) { File.Create("meta/rankRoles.json").Dispose(); } RankRoleCollection = JsonConvert.DeserializeObject <Dictionary <ulong, Dictionary <int, ulong> > >(File.ReadAllText("meta/rankRoles.json")); if (RankRoleCollection == null) { RankRoleCollection = new Dictionary <ulong, Dictionary <int, ulong> >(); } List <SocketGuild> guilds = client.Guilds.ToList(); foreach (SocketGuild guild in guilds) { if (!RankRoleCollection.ContainsKey(guild.Id)) { RankRoleCollection.Add(guild.Id, new Dictionary <int, ulong>()); } } SaveRankRoleCollection(); }