use of net.dv8tion.jda.api.events.guild.member.GuildMemberRoleAddEvent in project JDA by DV8FromTheWorld.
the class EntityBuilder method updateMemberRoles.
private void updateMemberRoles(MemberImpl member, List<Role> newRoles, long responseNumber) {
Set<Role> currentRoles = member.getRoleSet();
// Find the roles removed.
List<Role> removedRoles = new LinkedList<>();
each: for (Role role : currentRoles) {
for (Iterator<Role> it = newRoles.iterator(); it.hasNext(); ) {
Role r = it.next();
if (role.equals(r)) {
it.remove();
continue each;
}
}
removedRoles.add(role);
}
if (removedRoles.size() > 0)
currentRoles.removeAll(removedRoles);
if (newRoles.size() > 0)
currentRoles.addAll(newRoles);
if (removedRoles.size() > 0) {
getJDA().handleEvent(new GuildMemberRoleRemoveEvent(getJDA(), responseNumber, member, removedRoles));
}
if (newRoles.size() > 0) {
getJDA().handleEvent(new GuildMemberRoleAddEvent(getJDA(), responseNumber, member, newRoles));
}
}
use of net.dv8tion.jda.api.events.guild.member.GuildMemberRoleAddEvent in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onGuildMemberRoleAdd.
public void onGuildMemberRoleAdd(GuildMemberRoleAddEvent event) {
Guild guild = event.getGuild();
Member member = event.getMember();
User user = event.getUser();
List<Role> roles = event.getRoles();
Role firstRole = roles.get(0);
boolean multiple = roles.size() > 1;
LoggerEvent loggerEvent = LoggerEvent.MEMBER_ROLE_ADD;
LoggerContext loggerContext = new LoggerContext().setRole(multiple ? 0L : firstRole.getIdLong()).setUser(user);
StringBuilder description = new StringBuilder();
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
embed.setColor(this.bot.getConfig().getGreen());
embed.setTimestamp(Instant.now());
embed.setAuthor(new EmbedAuthor(user.getAsTag(), user.getEffectiveAvatarUrl(), null));
if (multiple) {
StringBuilder builder = new StringBuilder();
/* Make sure there is always enough space to write all the components to it */
int maxLength = MessageEmbed.DESCRIPTION_MAX_LENGTH - 32 - /* Max nickname length */
32 - /* Result String overhead */
15 - /* " and x more" overhead */
3;
for (int i = 0; i < roles.size(); i++) {
Role role = roles.get(i);
String entry = (i == roles.size() - 1 ? " and " : i != 0 ? ", " : "") + role.getAsMention();
if (builder.length() + entry.length() < maxLength) {
builder.append(entry);
} else {
builder.append(String.format(" and **%s** more", roles.size() - i));
break;
}
}
description.append(String.format("The roles %s have been added to `%s`", builder, member.getEffectiveName()));
} else {
description.append(String.format("The role %s has been added to `%s`", firstRole.getAsMention(), member.getEffectiveName()));
embed.setFooter(new EmbedFooter(String.format("Role ID: %s", firstRole.getId()), null));
}
this.bot.getMongo().aggregateLoggers(this.getPipeline(guild.getIdLong())).whenComplete((documents, exception) -> {
if (ExceptionUtility.sendErrorMessage(exception)) {
return;
}
if (documents.isEmpty()) {
return;
}
Document data = documents.get(0);
List<Document> loggers = LoggerUtility.getValidLoggers(data.getList("loggers", Document.class), loggerEvent, loggerContext);
if (loggers.isEmpty()) {
return;
}
if (!firstRole.isManaged() && guild.getSelfMember().hasPermission(Permission.VIEW_AUDIT_LOGS)) {
this.retrieveAuditLogsDelayed(guild, ActionType.MEMBER_ROLE_UPDATE).whenComplete((logs, auditException) -> {
User moderator = logs == null ? null : logs.stream().filter(e -> Duration.between(e.getTimeCreated(), ZonedDateTime.now(ZoneOffset.UTC)).toSeconds() <= 5).filter(e -> e.getTargetIdLong() == member.getUser().getIdLong()).filter(e -> {
AuditLogChange change = e.getChangeByKey(AuditLogKey.MEMBER_ROLES_ADD);
if (change == null) {
return false;
}
List<Map<String, String>> roleEntries = change.getNewValue();
List<String> roleIds = roleEntries.stream().map(roleEntry -> roleEntry.get("id")).collect(Collectors.toList());
for (Role role : roles) {
if (!roleIds.contains(role.getId())) {
return false;
}
}
return true;
}).map(AuditLogEntry::getUser).findFirst().orElse(null);
if (moderator != null) {
loggerContext.setModerator(moderator);
description.append(String.format(" by **%s**", moderator.getAsTag()));
}
embed.setDescription(description.toString());
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
});
} else {
embed.setDescription(description.toString());
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
}
});
}
use of net.dv8tion.jda.api.events.guild.member.GuildMemberRoleAddEvent in project c10ver by Gartham.
the class CloverGuildMemberAddRoleConsumer method consume.
@Override
public boolean consume(GuildMemberRoleAddEvent event, InputProcessor<? extends GuildMemberRoleAddEvent> processor, InputConsumer<GuildMemberRoleAddEvent> consumer) {
var e = (GuildMemberRoleAddEvent) event;
Server s = clover.getEconomy().getServer(e.getGuild().getId());
var role = s.getVoteRole();
if (role != null)
for (Role r : e.getRoles()) if (r.getId().equals(role)) {
try {
e.getGuild().removeRoleFromMember(e.getMember(), r).queue();
} catch (Exception er) {
System.err.println("An error occurred while attempting to remove the vote role from a user!");
er.printStackTrace();
}
clover.getVoteManager().handleVoteRoleAdded(e.getMember());
break;
}
return false;
}
Aggregations