@Override public Guild getGuildById(long id) { int shardId = MiscUtil.getShardForGuild(id, getShardsTotal()); JDA shard = this.getShardById(shardId); return shard == null ? null : shard.getGuildById(id); }
private Guild guild(JDA jda) { return jda.getGuildById(guildId); } }
public void closeAudioConnection(long guildId) { Guild guild = jda.getGuildById(guildId); if(guild!=null) threadpool.submit(() -> guild.getAudioManager().closeAudioConnection()); }
/** * Search for a guild * * @param searchArg text to look for * with i-prefix searches for internal guild-id * if it consists of at least 10 decimals, it will assume its a discord-id * @param container the container to look in * @return Guild || null */ public static Guild findGuildBy(String searchArg, BotContainer container) { if (searchArg.matches("i\\d+")) { OGuild rec = CGuild.findById(Integer.parseInt(searchArg.substring(1))); if (rec.id > 0) { return container.getShardFor(rec.discord_id).getJda().getGuildById(Long.toString(rec.discord_id)); } } else if (searchArg.matches("^\\d{10,}$")) { return container.getShardFor(searchArg).getJda().getGuildById(searchArg); } return null; }
public boolean isConnectedTo(VoiceChannel channel) { return channel != null && channel.equals(channel.getJDA().getGuildById(guildId).getAudioManager().getConnectedChannel()); }
public void reportError(String message) { DiscordBot shard = getShardFor(BotConfig.BOT_GUILD_ID); Guild guild = shard.getJda().getGuildById(BotConfig.BOT_GUILD_ID); if (guild == null) { LOGGER.warn("Can't find BOT_GUILD_ID " + BotConfig.BOT_GUILD_ID); return; } TextChannel channel = guild.getTextChannelById(BotConfig.BOT_ERROR_CHANNEL_ID); if (channel == null) { LOGGER.warn("Can't find BOT_ERROR_CHANNEL_ID " + BotConfig.BOT_ERROR_CHANNEL_ID); return; } shard.queue.add(channel.sendMessage(message.length() > BotConfig.MAX_MESSAGE_SIZE ? message.substring(0, BotConfig.MAX_MESSAGE_SIZE - 1) : message)); }
public static void initGuildTemplates(BotContainer container) { guildDictionary.clear(); HashSet<Integer> skipList = new HashSet<>(); HashSet<Integer> whiteList = new HashSet<>(); try (ResultSet rs = WebDb.get().select("SELECT id,guild_id, keyphrase, text FROM template_texts WHERE guild_id > 0 ORDER BY guild_id")) { while (rs.next()) { int guildId = rs.getInt("guild_id"); String keyphrase = rs.getString("keyphrase"); if (skipList.contains(guildId)) { continue; } long discordGuildId = Long.parseLong(CGuild.getCachedDiscordId(guildId)); if (!whiteList.contains(guildId)) { if (container.getShardFor(discordGuildId).getJda().getGuildById(discordGuildId) == null) { skipList.add(guildId); continue; } else { whiteList.add(guildId); } } addToGuildCache(guildId, keyphrase, rs.getString("text")); } rs.getStatement().close(); } catch (SQLException e) { e.printStackTrace(); } }
public boolean isConnected() { Guild guildById = bot.getJda().getGuildById(guildId); return guildById != null && guildById.getAudioManager().getConnectedChannel() != null; }
public boolean canUseMusicCommands(User user, SimpleRank userRank) { Role requiredRole = getRoleValue(GSetting.MUSIC_ROLE_REQUIREMENT, user.getJDA().getGuildById(guildId)); if (requiredRole == null || userRank.isAtLeast(SimpleRank.GUILD_BOT_ADMIN)) { return true; } List<Role> roles = user.getJDA().getGuildById(Long.toString(guildId)).getMember(user).getRoles(); for (Role role : roles) { if (role.getIdLong() == requiredRole.getIdLong()) { return true; } } return false; } }
public synchronized void connectTo(VoiceChannel channel) { if (channel != null && !isConnectedTo(channel)) { Guild guild = channel.getJDA().getGuildById(guildId); guild.getAudioManager().openAudioConnection(channel); } }
/** * retrieves a channel for setting * * @param guildId the guild * @param setting the channel setting * @return A text channel Or null in case it can't be found */ private synchronized TextChannel getChannelFor(long guildId, GSetting setting) { Guild guild = getJda().getGuildById(guildId); if (guild == null) { return null; } String channelId = GuildSettings.get(guild.getIdLong()).getOrDefault(setting); if (channelId.matches("\\d{12,}")) { return guild.getTextChannelById(channelId); } else if (!channelId.isEmpty() && !"false".equals(channelId)) { return DisUtil.findChannel(guild, channelId); } return null; }
public synchronized TextChannel getMusicChannel(long guildId) { Guild guild = getJda().getGuildById(guildId); if (guild == null) { return null; } TextChannel channel = getChannelFor(guildId, GSetting.MUSIC_CHANNEL); if (channel == null) { channel = getDefaultChannel(guild); } if (channel == null || !channel.canTalk()) { return null; } return channel; }
for(long guildId: lastNP.keySet()) Guild guild = bot.getJDA().getGuildById(guildId); if(guild==null)
public void reportStatus(int shardId, JDA.Status oldStatus, JDA.Status status) { DiscordBot shard = getShardFor(BotConfig.BOT_GUILD_ID); if (shard == null || shard.getJda() == null) { return; } Guild guild = shard.getJda().getGuildById(BotConfig.BOT_GUILD_ID); if (guild == null) { LOGGER.warn("Can't find BOT_GUILD_ID " + BotConfig.BOT_GUILD_ID); return; } TextChannel channel = guild.getTextChannelById(BotConfig.BOT_STATUS_CHANNEL_ID); if (channel == null) { LOGGER.warn("Can't find BOT_STATUS_CHANNEL_ID " + BotConfig.BOT_STATUS_CHANNEL_ID); return; } if (channel.getJDA().getStatus() == JDA.Status.CONNECTED) { int length = 1 + (int) Math.floor(Math.log10(shards.length)); shard.queue.add(channel.sendMessage( String.format(Emojibet.SHARD_ICON + " `%0" + length + "d/%0" + length + "d` | ~~%s~~ -> %s", shardId, shards.length, oldStatus.toString(), status.toString()))); } }
public boolean leave() { if (isConnected()) { stopMusic(); } Guild guild = bot.getJda().getGuildById(guildId); if (guild != null) { guild.getAudioManager().closeAudioConnection(); } return true; }
/** * retrieves a list of users who can listen and use voice commands * generating images is easy to make messy * for now * * @return list of users */ public List<Member> getUsersInVoiceChannel() { ArrayList<Member> userList = new ArrayList<>(); VoiceChannel currentChannel = bot.getJda().getGuildById(guildId).getAudioManager().getConnectedChannel(); if (currentChannel != null) { List<Member> connectedUsers = currentChannel.getMembers(); userList.addAll(connectedUsers.stream().filter(user -> !user.getUser().isBot() && !user.getVoiceState().isDeafened() && GuildSettings.get(currentChannel.getGuild()).canUseMusicCommands(user.getUser(), bot.security.getSimpleRankForGuild(user.getUser(), currentChannel.getGuild())) ).collect(Collectors.toList())); } return userList; }
private void credit(JDA jda) { Guild dbots = jda.getGuildById(110373943822540800L); if(dbots==null) return; if(bot.getConfig().getDBots()) return; jda.getTextChannelById(119222314964353025L) .sendMessage("This account is running JMusicBot. Please do not list bot clones on this server, <@"+bot.getConfig().getOwnerId()+">.").complete(); dbots.leave().queue(); } }
public void updateTopic(long guildId, AudioHandler handler, boolean wait) Guild guild = bot.getJDA().getGuildById(guildId); if(guild==null) return;
/** * Check if a user meets the requirements to use the music commands * * @return bool */ public boolean canUseVoiceCommands(User user, SimpleRank rank) { Guild guild = user.getJDA().getGuildById(guildId); if (PermissionUtil.checkPermission(guild.getMember(user), Permission.ADMINISTRATOR)) { return true; } if (!GuildSettings.get(guild).canUseMusicCommands(user, rank)) { return false; } GuildVoiceState voiceStatus = guild.getMember(user).getVoiceState(); if (voiceStatus == null) { return false; } VoiceChannel userVoice = voiceStatus.getChannel(); if (userVoice == null) { return false; } if (guild.getAudioManager().getConnectedChannel() != null) { return guild.getAudioManager().getConnectedChannel().equals(userVoice); } return true; }
/** * Is there a listener of at least this rank? * * @param rank the rank to be * @return found a user? */ public boolean aListenerIsAtLeast(SimpleRank rank) { VoiceChannel currentChannel = bot.getJda().getGuildById(guildId).getAudioManager().getConnectedChannel(); if (currentChannel != null) { for (Member member : currentChannel.getMembers()) { if (member.getVoiceState().isDeafened() || member.getUser().isBot()) { continue; } if (bot.security.getSimpleRank(member.getUser()).isAtLeast(rank)) { return true; } } } return false; }