private async Task UpdateUser(ulong user) { DisablePluginIfPermissionMissing(GuildPermission.ManageRoles, true); DateTime activity = GetLastActivity(user); ulong role = GetRole(_activityRoles.GetValue(), activity); SocketRole roleObj = GuildHandler.FindRole(role); if (roleObj != null) { SocketGuildUser userObj = GuildHandler.GetUser(user); List <SocketRole> toRemove = userObj.Roles.Where(x => _activityRoles.GetValue().Any(y => x.Id == y.Id)).ToList(); toRemove.Remove(roleObj); if (!userObj.Roles.Contains(roleObj)) { Core.Log.User($"Adding activity role {roleObj.Name} to {userObj.GetShownName ()}"); await userObj.AsyncSecureAddRole(roleObj); } foreach (var toRemoveRole in toRemove) { Core.Log.User($"Removing activity role {toRemoveRole.Name} from {userObj.GetShownName()}"); await userObj.AsyncSecureRemoveRole(toRemoveRole); } } }
private async Task UpdateUser(SocketGuildUser user) { DateTime activity = GetLastActivity(user); DateTime now = DateTime.Now; List <ActivityRole> activityStates = _activityRoles.GetValue(); activityStates.Sort(Comparer <ActivityRole> .Create((x, y) => (int)x.threshold - (int)y.threshold)); SocketRole[] roles = activityStates.Select(x => user.Guild.GetRole(x.id)).ToArray(); SocketRole finalRole = roles[0]; DateTime lastDate = now.AddDays(1); for (int i = 0; i < activityStates.Count; i++) { DateTime thisDate = now.AddDays(-activityStates[i].threshold); if (activity < lastDate && activity > thisDate) { finalRole = roles[i]; lastDate = thisDate; } } if (activity < now.AddDays(-activityStates.Last().threshold)) { finalRole = roles.Last(); } List <SocketRole> toRemove = roles.ToList(); toRemove.Remove(finalRole); await user.AsyncSecureAddRole(finalRole); toRemove.ForEach(x => user.AsyncSecureRemoveRole(x)); }
private async Task GiveRandomColourAsync(SocketGuildUser guildUser) { SocketRole[] roles = GetRoles(); SocketRole randomRole = roles[new Random().Next(roles.Length)]; await guildUser.AsyncSecureAddRole(randomRole); }