public Rank GetMatchingRank(DiscordMember member) { List <string> roleNamesForMember = new List <string>(); foreach (var role in member.Roles) { roleNamesForMember.Add(role.Name); } //var roleNamesForMember = member.Roles.ToList(); var matchingRole = MilRanks.Where(x => roleNamesForMember.Contains(x.RankName)).FirstOrDefault(); if (matchingRole == null) { matchingRole = CommerceRanks.Where(x => roleNamesForMember.Contains(x.RankName)).FirstOrDefault(); } return(matchingRole); }
//Demote a member public async Task <bool> Demote(DiscordMember member) { try { var currentRank = GetMatchingRank(member); var newRank = MilRanks.Where(x => x.Number == currentRank.Number - 1).FirstOrDefault(); if (newRank == null) { newRank = CommerceRanks.Where(x => x.Number == currentRank.Number - 1).FirstOrDefault(); } await member.RevokeRoleAsync(member.Guild.Roles.Where(x => x.Name == currentRank.RankName).FirstOrDefault()); await member.GrantRoleAsync(member.Guild.Roles.Where(x => x.Name == newRank.RankName).FirstOrDefault()); return(true); } catch (Exception) { return(false); } }