@Override public IGuild getGuild() { return channel.getGuild(); }
@Override public void trackLoaded(AudioTrack track) { sendMessageToChannel(channel, "Adding to queue " + track.getInfo().title); play(channel.getGuild(), musicManager, track); }
public ChannelEvent(IChannel channel) { super(channel.isPrivate() ? null : channel.getGuild()); this.channel = channel; }
/** * The parent guild of the channel the messages were sent in, or null if no messages are present. * * @return The parent guild of the channel the messages were sent in, or null if no messages are present. */ public IGuild getGuild() { final IChannel channel = getChannel(); return (channel == null) ? null : (channel.isPrivate() ? null : channel.getGuild()); }
/** * Throws a {@link MissingPermissionsException} if user1 is not higher in the role hierarchy than user2 in the given channel's guild * or user1 does not have all of the required permissions. * The former is determined by the positions of each of the users' highest roles. * This method takes into account user and roles overrides in the given channel. * * @param channel The channel to check. * @param user1 The user who must be higher in the role hierarchy and have all of the required permissions. * @param user2 The user who must be lower in the role hierarchy. * @param required The permissions user1 must have. */ public static void requireHierarchicalPermissions(IChannel channel, IUser user1, IUser user2, EnumSet<Permissions> required) { requirePermissions(channel, user1, required); requireUserHigher(channel.getGuild(), user1, user2); }
/** * Throws a {@link MissingPermissionsException} if the given user is not higher in the role hierarchy than all of the given roles * or the user does not have all of the required permissions. * The former is determined by the position of the user's highest role and the position of the highest role in roles. * This method takes into account user and roles overrides in the given channel. * * @param channel The channel to check. * @param user The user who must be higher in the role hierarchy and have all of the required permissions. * @param roles The roles which must be lower in the role hierarchy. * @param required The permissions the user must have. */ public static void requireHierarchicalPermissions(IChannel channel, IUser user, List<IRole> roles, EnumSet<Permissions> required) { requirePermissions(channel, user, required); requireUserHigher(channel.getGuild(), user, roles); }
/** * Determines if user1 is higher in the role hierarchy than user2 in the given channel's guild * and user1 has all of the required permissions. * The former is determined by the positions of each of the users' highest roles. * This method takes into account user and roles overrides in the given channel. * * @param channel The channel to check. * @param user1 The user who must be higher in the role hierarchy and have all of the required permissions. * @param user2 The user who must be lower in the role hierarchy. * @param required The permissions user1 must have. * @return True if user1 is higher in the role hierarchy than user2 and user1 has all of the required permissions. */ public static boolean hasHierarchicalPermissions(IChannel channel, IUser user1, IUser user2, EnumSet<Permissions> required) { return hasPermissions(channel, user1, required) && isUserHigher(channel.getGuild(), user1, user2); }
/** * Determines if the given user is higher in the role hierarchy than all of the given roles * and the user has all of the required permissions. * The former is determined by the position of the user's highest role and the position of the highest role in roles. * This method takes into account user and roles overrides in the given channel. * * @param channel The channel to check. * @param user The user who must be higher in the role hierarchy and have all of the required permissions. * @param roles The roles which must be lower in the role hierarchy. * @param required The permissions the user must have. * @return True if the user is higher in the role hierarchy than every role in roles and the user has all of the required permissions. */ public static boolean hasHierarchicalPermissions(IChannel channel, IUser user, List<IRole> roles, EnumSet<Permissions> required) { return hasPermissions(channel, user, required) && isUserHigher(channel.getGuild(), user, roles); }
@Override public IGuild getGuild() { return getChannel().isPrivate() ? null : getChannel().getGuild(); }
@Override public void playlistLoaded(AudioPlaylist playlist) { AudioTrack firstTrack = playlist.getSelectedTrack(); if (firstTrack == null) { firstTrack = playlist.getTracks().get(0); } sendMessageToChannel(channel, "Adding to queue " + firstTrack.getInfo().title + " (first track of playlist " + playlist.getName() + ")"); play(channel.getGuild(), musicManager, firstTrack); }
private void skipTrack(IChannel channel) { GuildMusicManager musicManager = getGuildAudioPlayer(channel.getGuild()); musicManager.scheduler.nextTrack(); sendMessageToChannel(channel, "Skipped to next track."); }
public void throwException(IChannel channel, Language lg, MissingPermissionsException e) { StringBuilder st = new StringBuilder(Translator.getLabel(lg, "exception.missing_permission") .replace("{channel.name}", channel.getName())); for(Permissions p : e.getMissingPermissions()) st.append(Translator.getLabel(lg, PERMISSION_PREFIX + p.name().toLowerCase())).append(", "); st.delete(st.length() - 2, st.length()).append("."); if (channel.getModifiedPermissions(ClientConfig.DISCORD().getOurUser()).contains(Permissions.SEND_MESSAGES)) Message.sendText(channel, st.toString()); else try { Message.sendText(channel.getGuild().getOwner().getOrCreatePMChannel(), st.toString()); } catch(sx.blah.discord.util.DiscordException de){ LOG.warn("throwException", "Impossible de contacter l'administrateur de la guilde [" + channel.getGuild().getName() + "]."); } } }
public synchronized void removeToDatabase() { getChannelLanguages().remove(getChannelId()); Connexion connexion = Connexion.getInstance(); Connection connection = connexion.getConnection(); try { PreparedStatement request = connection.prepareStatement("DELETE FROM Channel_Language WHERE id_chan = ?;"); request.setString(1, String.valueOf(getChannelId())); request.executeUpdate(); } catch (SQLException e) { Reporter.report(e, ClientConfig.DISCORD().getChannelByID(getChannelId()).getGuild(), ClientConfig.DISCORD().getChannelByID(getChannelId())); LOG.error("removeToDatabase", e); } }
public synchronized static Map<String, AlmanaxCalendar> getAlmanaxCalendars(){ if (almanaxCalendar == null){ almanaxCalendar = new HashMap<>(); Connexion connexion = Connexion.getInstance(); Connection connection = connexion.getConnection(); try { PreparedStatement query = connection.prepareStatement("SELECT id_guild, id_chan FROM Almanax_Calendar"); ResultSet resultSet = query.executeQuery(); while (resultSet.next()){ String idChan = resultSet.getString("id_chan"); String idGuild = resultSet.getString("id_guild"); IChannel chan = ClientConfig.DISCORD().getChannelByID(Long.parseLong(idChan)); if (chan != null && ! chan.isDeleted()) almanaxCalendar.put(chan.getStringID(), new AlmanaxCalendar(chan.getGuild().getStringID(), chan.getStringID())); else { new AlmanaxCalendar(idGuild, idChan).removeToDatabase(); LOG.info("Chan deleted : " + idChan); } } } catch (SQLException e) { Reporter.report(e); LOG.error("getAlmanaxCalendars", e); } } return almanaxCalendar; }
public static void sendText(IChannel channel, String content){ Language lg = Translator.getLanguageFrom(channel); RequestBuffer.request(() -> { try { new MessageBuilder(ClientConfig.DISCORD()) .withChannel(channel) .withContent(content) .build(); } catch(RateLimitException e){ LoggerFactory.getLogger(Message.class).warn(e.getMessage(),e ); throw e; } catch (DiscordException e){ Reporter.report(e, channel.isPrivate()? null : channel.getGuild(), channel); LoggerFactory.getLogger(Message.class).error(e.getMessage(), e); } catch(MissingPermissionsException e){ LoggerFactory.getLogger(Message.class).warn(Constants.name + " n'a pas les permissions pour appliquer cette requête."); missingPermission.throwException(channel, lg, e); } catch(Exception e){ Reporter.report(e, channel.isPrivate()? null : channel.getGuild(), channel); LoggerFactory.getLogger(Message.class).error(e.getMessage(),e ); } }); }
public synchronized void setLanguage(Language lang){ this.lang = lang; Connexion connexion = Connexion.getInstance(); Connection connection = connexion.getConnection(); try { PreparedStatement preparedStatement = connection.prepareStatement( "UPDATE Channel_Language SET lang = ? WHERE id_chan = ?;"); preparedStatement.setString(1, getLang().getAbrev()); preparedStatement.setString(2, String.valueOf(getChannelId())); preparedStatement.executeUpdate(); } catch (SQLException e) { Reporter.report(e, ClientConfig.DISCORD().getChannelByID(getChannelId()).getGuild(), ClientConfig.DISCORD().getChannelByID(getChannelId())); LOG.error("setLanguage", e); } }
public synchronized void addToDatabase(){ if (! getChannelLanguages().containsKey(getChannelId())) { getChannelLanguages().put(getChannelId(), this); Connexion connexion = Connexion.getInstance(); Connection connection = connexion.getConnection(); try { PreparedStatement preparedStatement = connection.prepareStatement( "INSERT INTO Channel_Language(id_chan, lang) VALUES(?, ?);"); preparedStatement.setString(1, String.valueOf(getChannelId())); preparedStatement.setString(2, getLang().getAbrev()); preparedStatement.executeUpdate(); } catch (SQLException e) { Reporter.report(e, ClientConfig.DISCORD().getChannelByID(getChannelId()).getGuild(), ClientConfig.DISCORD().getChannelByID(getChannelId())); LOG.error("addToDatabase", e); } } }
/** * Fournit la langue utilisée dans un salon textuel * @param channel Salon textuel * @return Langue de la guilde ou du salon si précisé */ public static Language getLanguageFrom(IChannel channel){ Language result = Constants.defaultLanguage; if (! channel.isPrivate()) { Guild guild = Guild.getGuild(channel.getGuild()); result = guild.getLanguage(); ChannelLanguage channelLanguage = ChannelLanguage.getChannelLanguages().get(channel.getLongID()); if (channelLanguage != null) result = channelLanguage.getLang(); } return result; }
/** * Converts a json {@link WebhookObject} to a {@link IWebhook}. This method first checks the internal webhook cache * and returns that object with updated information if it exists. Otherwise, it constructs a new webhook. * * @param channel The channel the webhook belongs to. * @param json The json object representing the webhook. * @return The converted webhook object. */ public static IWebhook getWebhookFromJSON(IChannel channel, WebhookObject json) { long webhookId = Long.parseUnsignedLong(json.id); if (channel.getWebhookByID(webhookId) != null) { Webhook webhook = (Webhook) channel.getWebhookByID(webhookId); webhook.setName(json.name); webhook.setAvatar(json.avatar); return webhook; } else { long userId = Long.parseUnsignedLong(json.user.id); IUser author = channel.getGuild() .getUsers() .stream() .filter(it -> it.getLongID() == userId) .findAny() .orElseGet(() -> getUserFromJSON(channel.getShard(), json.user)); return new Webhook(channel.getClient(), json.name, Long.parseUnsignedLong(json.id), channel, author, json.avatar, json.token); } }
private void reactionAdd(ReactionEventResponse event) { IChannel channel = shard.getChannelByID(Long.parseUnsignedLong(event.channel_id)); if (channel == null) return; if (!PermissionUtils.hasPermissions(channel, client.ourUser, Permissions.READ_MESSAGES, Permissions.READ_MESSAGE_HISTORY)) return; // Discord sends this event no matter our permissions for some reason. boolean cached = ((Channel) channel).messages.containsKey(Long.parseUnsignedLong(event.message_id)); IMessage message = channel.fetchMessage(Long.parseUnsignedLong(event.message_id)); if (message == null) { Discord4J.LOGGER.debug("Unable to fetch the message specified by a reaction add event\nObject={}", ToStringBuilder.reflectionToString(event)); return; } IReaction reaction = event.emoji.id == null ? message.getReactionByUnicode(event.emoji.name) : message.getReactionByID(Long.parseUnsignedLong(event.emoji.id)); message.getReactions().remove(reaction); if (reaction == null) { // Only happens in the case of a cached message with a new reaction long id = event.emoji.id == null ? 0 : Long.parseUnsignedLong(event.emoji.id); reaction = new Reaction(message, 1, ReactionEmoji.of(event.emoji.name, id)); } else if (cached) { reaction = new Reaction(message, reaction.getCount() + 1, reaction.getEmoji()); } message.getReactions().add(reaction); IUser user; if (channel.isPrivate()) { user = channel.getUsersHere().get(channel.getUsersHere().get(0).getLongID() == Long.parseUnsignedLong(event.user_id) ? 0 : 1); } else { user = channel.getGuild().getUserByID(Long.parseUnsignedLong(event.user_id)); } client.dispatcher.dispatch(new ReactionAddEvent(message, reaction, user)); }