Java Code Examples for net.dv8tion.jda.api.entities.Member#getGuild()

The following examples show how to use net.dv8tion.jda.api.entities.Member#getGuild() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MemberListener.java    From Arraybot with Apache License 2.0 6 votes vote down vote up
/**
 * Applies the autorole to the specified member.
 * @param member The member.
 */
private void assignAutoRole(Member member) {
    Guild guild = member.getGuild();
    long guildId = guild.getIdLong();
    GuildEntry entry = (GuildEntry) Category.GUILD.getEntry();
    boolean apply = Boolean.valueOf(entry.fetch(entry.getField(GuildEntry.Fields.AUTOROLE_ENABLED), guildId, null));
    if(!apply) {
        return;
    }
    String roleId = entry.fetch(entry.getField(GuildEntry.Fields.AUTOROLE_ROLE), guildId, null);
    Role role = guild.getRoleById(roleId);
    if(role == null) {
        return;
    }
    try {
        guild.addRoleToMember(member, role).queue();
    } catch(PermissionException exception) {
        logger.warn("Could not apply the autorole in the guild {} due to a permission error.", guildId);
    }
}
 
Example 2
Source File: ContextServiceImpl.java    From JuniperBot with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void initContext(GenericEvent event) {
    Guild guild = null;
    User user = null;

    if (event instanceof GenericGuildMemberEvent) {
        GenericGuildMemberEvent memberEvent = (GenericGuildMemberEvent) event;
        guild = memberEvent.getMember().getGuild();
        user = memberEvent.getMember().getUser();
    }

    if (guild == null && event instanceof GenericGuildEvent) {
        guild = ((GenericGuildEvent) event).getGuild();
    }

    if (guild == null || user == null) {
        Member member = resolverService.getMember(event);
        if (member != null) {
            guild = member.getGuild();
            user = member.getUser();
        }
    }
    if (guild == null) {
        guild = resolverService.getGuild(event);
    }
    if (user == null) {
        user = resolverService.getUser(event);
    }
    if (guild != null) {
        initContext(guild);
    }
    if (user != null) {
        initContext(user);
    }
}
 
Example 3
Source File: ModerationServiceImpl.java    From JuniperBot with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean setColor(Member member, String color) {
    Role role = null;
    Guild guild = member.getGuild();
    Member self = guild.getSelfMember();

    if (StringUtils.isNotEmpty(color)) {
        String roleName = COLOR_ROLE_NAME + color;
        List<Role> roles = member.getGuild().getRolesByName(roleName, false);
        role = roles.stream().filter(self::canInteract).findFirst().orElse(null);
        if (role == null) {
            role = guild
                    .createRole()
                    .setColor(CommonUtils.hex2Rgb(color))
                    .setMentionable(false)
                    .setName(roleName)
                    .complete();

            Role highestRole = DiscordUtils.getHighestRole(self, Permission.MANAGE_ROLES);
            if (highestRole != null) {
                guild.modifyRolePositions()
                        .selectPosition(role)
                        .moveTo(highestRole.getPosition() - 1)
                        .queue();
            }
        }

        if (!self.canInteract(role)) {
            return false;
        }
    }

    if (role == null || !member.getRoles().contains(role)) {
        List<Role> roleList = member.getRoles().stream()
                .filter(e -> e.getName().startsWith(COLOR_ROLE_NAME))
                .filter(self::canInteract)
                .collect(Collectors.toList());
        guild.modifyMemberRoles(member, role != null ? Collections.singleton(role) : null, roleList).queue();
    }
    // remove unused color roles
    Set<Role> userRoles = new LinkedHashSet<>();
    if (role != null) {
        userRoles.add(role);
    }
    guild.getMembers().forEach(m -> userRoles.addAll(m.getRoles()));
    guild.getRoles().stream()
            .filter(e -> e.getName().startsWith(COLOR_ROLE_NAME) && !userRoles.contains(e) && self.canInteract(e))
            .forEach(e -> e.delete().queue());
    return true;
}
 
Example 4
Source File: GenericGuildVoiceEvent.java    From JDA with Apache License 2.0 4 votes vote down vote up
public GenericGuildVoiceEvent(@Nonnull JDA api, long responseNumber, @Nonnull Member member)
{
    super(api, responseNumber, member.getGuild());
    this.member = member;
}
 
Example 5
Source File: GenericGuildMemberEvent.java    From JDA with Apache License 2.0 4 votes vote down vote up
public GenericGuildMemberEvent(@Nonnull JDA api, long responseNumber, @Nonnull Member member)
{
    super(api, responseNumber, member.getGuild());
    this.member = member;
}
 
Example 6
Source File: UserUpdateOnlineStatusEvent.java    From JDA with Apache License 2.0 4 votes vote down vote up
public UserUpdateOnlineStatusEvent(@Nonnull JDA api, long responseNumber, @Nonnull Member member, @Nonnull OnlineStatus oldOnlineStatus)
{
    super(api, responseNumber, member.getUser(), oldOnlineStatus, member.getOnlineStatus(), IDENTIFIER);
    this.guild = member.getGuild();
    this.member = member;
}
 
Example 7
Source File: GuildVoiceStateImpl.java    From JDA with Apache License 2.0 4 votes vote down vote up
public GuildVoiceStateImpl(Member member)
{
    this.api = member.getJDA();
    this.guild = new SnowflakeReference<>(member.getGuild(), api::getGuildById);
    this.member = new SnowflakeReference<>(member, (id) -> guild.resolve().getMemberById(id));
}