List of usage examples for java.lang Long parseUnsignedLong
public static long parseUnsignedLong(String s) throws NumberFormatException
From source file:sx.blah.discord.api.internal.DispatchHandler.java
private void messageUpdate(MessageObject json) { Channel channel = (Channel) client.getChannelByID(Long.parseUnsignedLong(json.channel_id)); if (channel == null) return;//from ww w.j av a 2 s . c o m IMessage toUpdate = channel.messages.get(json.id); if (toUpdate == null) { // Cannot resolve update type. MessageObject is incomplete, so we'll have to request for the full message. if (channel.isPrivate() || PermissionUtils.hasHierarchicalPermissions(channel, client.ourUser, channel.getGuild().getRolesForUser(client.ourUser), Permissions.READ_MESSAGE_HISTORY)) client.dispatcher.dispatch( new MessageUpdateEvent(null, channel.fetchMessage(Long.parseUnsignedLong(json.id)))); // else //FIXME: unable to fire message update events when the user doesn't have the read message history permission } else { IMessage oldMessage = toUpdate.copy(); IMessage updatedMessage = DiscordUtils.getUpdatedMessageFromJSON(client, toUpdate, json); if (json.pinned != null && oldMessage.isPinned() && !json.pinned) { client.dispatcher.dispatch(new MessageUnpinEvent(oldMessage, updatedMessage)); } else if (json.pinned != null && !oldMessage.isPinned() && json.pinned) { client.dispatcher.dispatch(new MessagePinEvent(oldMessage, updatedMessage)); } else if (oldMessage.getEmbeds().size() < updatedMessage.getEmbeds().size()) { client.dispatcher .dispatch(new MessageEmbedEvent(oldMessage, updatedMessage, oldMessage.getEmbeds())); } else if (json.content != null && !oldMessage.getContent().equals(json.content)) { client.dispatcher.dispatch(new MessageEditEvent(oldMessage, updatedMessage)); } else { client.dispatcher.dispatch(new MessageUpdateEvent(oldMessage, updatedMessage)); } } }
From source file:sx.blah.discord.api.internal.DispatchHandler.java
private void messageDelete(MessageDeleteEventResponse event) { long id = Long.parseUnsignedLong(event.id); Channel channel = (Channel) client.getChannelByID(Long.parseUnsignedLong(event.channel_id)); IMessage message = null;/* ww w .j a v a2 s. co m*/ if (channel != null) { message = channel.messages.get(id); } if (message == null) { // we dont have the message cached. The only thing we know about the message is its ID and its channel's ID. client.dispatcher.dispatch(new MessageDeleteEvent(channel, id)); } else { channel.messages.remove(id); client.dispatcher.dispatch(new MessageDeleteEvent(message)); } }
From source file:sx.blah.discord.api.internal.DispatchHandler.java
private void channelCreate(ChannelObject json) { if (json.type == ChannelObject.Type.PRIVATE) { if (!shard.privateChannels.containsKey(json.id)) { shard.privateChannels.put((IPrivateChannel) DiscordUtils.getChannelFromJSON(shard, null, json)); }// www . j av a 2s .co m } else { Guild guild = (Guild) shard.getGuildByID(Long.parseUnsignedLong(json.guild_id)); if (guild != null) { IChannel channel = DiscordUtils.getChannelFromJSON(shard, guild, json); if (json.type == ChannelObject.Type.GUILD_TEXT) { guild.channels.put(channel); client.dispatcher.dispatch(new ChannelCreateEvent(channel)); } else if (json.type == ChannelObject.Type.GUILD_VOICE) { guild.voiceChannels.put((IVoiceChannel) channel); client.dispatcher.dispatch(new VoiceChannelCreateEvent((IVoiceChannel) channel)); } else if (json.type == ChannelObject.Type.GUILD_CATEGORY) { ICategory category = DiscordUtils.getCategoryFromJSON(shard, guild, json); guild.categories.put(category); client.dispatcher.dispatch(new CategoryCreateEvent(category)); } } } }
From source file:sx.blah.discord.api.internal.DispatchHandler.java
private void channelDelete(ChannelObject json) { if (json.type == ChannelObject.Type.GUILD_TEXT) { Channel channel = (Channel) client.getChannelByID(Long.parseUnsignedLong(json.id)); if (channel != null) { if (!channel.isPrivate()) ((Guild) channel.getGuild()).channels.remove(channel); else/*from w w w . j a v a2s. c o m*/ shard.privateChannels.remove(channel); client.dispatcher.dispatch(new ChannelDeleteEvent(channel)); } } else if (json.type == ChannelObject.Type.GUILD_VOICE) { VoiceChannel channel = (VoiceChannel) client.getVoiceChannelByID(Long.parseUnsignedLong(json.id)); if (channel != null) { ((Guild) channel.getGuild()).voiceChannels.remove(channel); client.dispatcher.dispatch(new VoiceChannelDeleteEvent(channel)); } } else if (json.type == ChannelObject.Type.GUILD_CATEGORY) { ICategory category = client.getCategoryByID(Long.parseUnsignedLong(json.id)); if (category != null) { ((Guild) category.getGuild()).categories.remove(category); client.dispatcher.dispatch(new CategoryDeleteEvent(category)); } } }
From source file:sx.blah.discord.api.internal.DispatchHandler.java
private void channelUpdate(ChannelObject json) { if (json.type == ChannelObject.Type.GUILD_TEXT) { Channel toUpdate = (Channel) shard.getChannelByID(Long.parseUnsignedLong(json.id)); if (toUpdate != null) { IChannel oldChannel = toUpdate.copy(); toUpdate = (Channel) DiscordUtils.getChannelFromJSON(shard, toUpdate.getGuild(), json); toUpdate.loadWebhooks();// ww w .j a va 2 s .c om if (!Objects.equals(oldChannel.getCategory(), toUpdate.getCategory())) { client.dispatcher.dispatch(new ChannelCategoryUpdateEvent(oldChannel, toUpdate, oldChannel.getCategory(), toUpdate.getCategory())); } else { client.dispatcher.dispatch(new ChannelUpdateEvent(oldChannel, toUpdate)); } } } else if (json.type == ChannelObject.Type.GUILD_VOICE) { IVoiceChannel toUpdate = shard.getVoiceChannelByID(Long.parseUnsignedLong(json.id)); if (toUpdate != null) { IVoiceChannel oldChannel = toUpdate.copy(); toUpdate = (IVoiceChannel) DiscordUtils.getChannelFromJSON(shard, toUpdate.getGuild(), json); if (!Objects.equals(oldChannel.getCategory(), toUpdate.getCategory())) { client.dispatcher.dispatch(new ChannelCategoryUpdateEvent(oldChannel, toUpdate, oldChannel.getCategory(), toUpdate.getCategory())); } else { client.dispatcher.dispatch(new VoiceChannelUpdateEvent(oldChannel, toUpdate)); } } } else if (json.type == ChannelObject.Type.GUILD_CATEGORY) { ICategory toUpdate = shard.getCategoryByID(Long.parseUnsignedLong(json.id)); if (toUpdate != null) { ICategory oldCategory = toUpdate.copy(); toUpdate = DiscordUtils.getCategoryFromJSON(shard, toUpdate.getGuild(), json); client.dispatcher.dispatch(new CategoryUpdateEvent(oldCategory, toUpdate)); } } }
From source file:sx.blah.discord.api.internal.DispatchHandler.java
private void guildBanAdd(GuildBanEventResponse event) { Guild guild = (Guild) client.getGuildByID(Long.parseUnsignedLong(event.guild_id)); if (guild != null) { IUser user = DiscordUtils.getUserFromJSON(shard, event.user); if (guild.getUserByID(user.getLongID()) != null) { guild.users.remove(user);//from w ww .j a v a2s.com guild.joinTimes.remove(user); } client.dispatcher.dispatch(new UserBanEvent(guild, user)); } }
From source file:sx.blah.discord.api.internal.DispatchHandler.java
private void guildEmojisUpdate(GuildEmojiUpdateResponse event) { Guild guild = (Guild) shard.getGuildByID(Long.parseUnsignedLong(event.guild_id)); if (guild != null) { List<IEmoji> oldEmoji = guild.getEmojis(); List<IEmoji> newEmoji = Arrays.stream(event.emojis).map(e -> DiscordUtils.getEmojiFromJSON(guild, e)) .collect(Collectors.toList()); guild.emojis.clear();// www .j av a 2 s.co m guild.emojis.putAll(newEmoji); client.dispatcher.dispatch(new GuildEmojisUpdateEvent(guild, oldEmoji, newEmoji)); } }
From source file:sx.blah.discord.api.internal.DispatchHandler.java
private void reactionAdd(ReactionEventResponse event) { IChannel channel = shard.getChannelByID(Long.parseUnsignedLong(event.channel_id)); if (channel == null) return;//from w ww .j ava 2 s . c o m 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)); }
From source file:sx.blah.discord.api.internal.DispatchHandler.java
private void reactionRemove(ReactionEventResponse event) { IChannel channel = shard.getChannelByID(Long.parseUnsignedLong(event.channel_id)); if (channel == null) return;/*from w ww. j a v a 2s . c o m*/ 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 remove 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) { // the last reaction of the emoji was removed long id = event.emoji.id == null ? 0 : Long.parseUnsignedLong(event.emoji.id); reaction = new Reaction(message, 0, ReactionEmoji.of(event.emoji.name, id)); } else { reaction = new Reaction(message, !cached ? reaction.getCount() : reaction.getCount() - 1, reaction.getEmoji()); } if (reaction.getCount() > 0) { 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 ReactionRemoveEvent(message, reaction, user)); }