|
| 1 | +/* |
| 2 | + * Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package net.dv8tion.jda.api.events.guild.member.update; |
| 18 | + |
| 19 | +import net.dv8tion.jda.api.JDA; |
| 20 | +import net.dv8tion.jda.api.entities.Member; |
| 21 | +import net.dv8tion.jda.api.utils.DiscordAssets; |
| 22 | +import net.dv8tion.jda.api.utils.ImageFormat; |
| 23 | +import net.dv8tion.jda.api.utils.ImageProxy; |
| 24 | + |
| 25 | +import javax.annotation.Nonnull; |
| 26 | +import javax.annotation.Nullable; |
| 27 | + |
| 28 | +/** |
| 29 | + * Indicates that a {@link net.dv8tion.jda.api.entities.Member Member} updated their {@link net.dv8tion.jda.api.entities.Guild Guild} banner. |
| 30 | + * |
| 31 | + * <p>Can be used to retrieve members who change their per guild banner, the triggering guild, the old banner id and the new banner id. |
| 32 | + * |
| 33 | + * <p>Identifier: {@code banner} |
| 34 | + * |
| 35 | + * <p><b>Requirements</b><br> |
| 36 | + * |
| 37 | + * <p>This event requires the {@link net.dv8tion.jda.api.requests.GatewayIntent#GUILD_MEMBERS GUILD_MEMBERS} intent to be enabled. |
| 38 | + * <br>{@link net.dv8tion.jda.api.JDABuilder#createDefault(String) createDefault(String)} and |
| 39 | + * {@link net.dv8tion.jda.api.JDABuilder#createLight(String) createLight(String)} disable this by default! |
| 40 | + * |
| 41 | + * <p>Additionally, this event requires the {@link net.dv8tion.jda.api.utils.MemberCachePolicy MemberCachePolicy} |
| 42 | + * to cache the updated members. Discord does not specifically tell us about the updates, but merely tells us the |
| 43 | + * member was updated and gives us the updated member object. In order to fire a specific event like this we |
| 44 | + * need to have the old member cached to compare against. |
| 45 | + */ |
| 46 | +public class GuildMemberUpdateBannerEvent extends GenericGuildMemberUpdateEvent<String> { |
| 47 | + public static final String IDENTIFIER = "banner"; |
| 48 | + |
| 49 | + public GuildMemberUpdateBannerEvent( |
| 50 | + @Nonnull JDA api, long responseNumber, @Nonnull Member member, @Nullable String oldBannerId) { |
| 51 | + super(api, responseNumber, member, oldBannerId, member.getBannerId(), IDENTIFIER); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * The old banner id |
| 56 | + * |
| 57 | + * @return The old banner id |
| 58 | + */ |
| 59 | + @Nullable |
| 60 | + public String getOldBannerId() { |
| 61 | + return getOldValue(); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * The previous banner url |
| 66 | + * |
| 67 | + * @return The previous banner url |
| 68 | + */ |
| 69 | + @Nullable |
| 70 | + public String getOldBannerUrl() { |
| 71 | + return previous == null |
| 72 | + ? null |
| 73 | + : getOldBannerUrl(previous.startsWith("a_") ? ImageFormat.ANIMATED_WEBP : ImageFormat.PNG); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * The previous banner url |
| 78 | + * |
| 79 | + * @param format |
| 80 | + * The format in which the image should be |
| 81 | + * |
| 82 | + * @throws IllegalArgumentException |
| 83 | + * If the format is {@code null} |
| 84 | + * |
| 85 | + * @return The previous banner url |
| 86 | + * |
| 87 | + * @see DiscordAssets#memberBanner(ImageFormat, String, String, String) |
| 88 | + */ |
| 89 | + @Nullable |
| 90 | + public String getOldBannerUrl(@Nonnull ImageFormat format) { |
| 91 | + ImageProxy proxy = getOldBanner(format); |
| 92 | + return proxy == null ? null : proxy.getUrl(); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Returns an {@link ImageProxy} for this member's old banner. |
| 97 | + * <p> |
| 98 | + * <b>Note:</b> the old banner may not always be downloadable as it might have been removed from Discord. |
| 99 | + * |
| 100 | + * @return Possibly-null {@link ImageProxy} of this member's old banner |
| 101 | + * |
| 102 | + * @see #getOldBannerUrl() |
| 103 | + */ |
| 104 | + @Nullable |
| 105 | + public ImageProxy getOldBanner() { |
| 106 | + String oldBannerUrl = getOldBannerUrl(); |
| 107 | + return oldBannerUrl == null ? null : new ImageProxy(oldBannerUrl); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Returns an {@link ImageProxy} for this member's old banner. |
| 112 | + * <p> |
| 113 | + * <b>Note:</b> the old banner may not always be downloadable as it might have been removed from Discord. |
| 114 | + * |
| 115 | + * @param format |
| 116 | + * The format in which the image should be |
| 117 | + * |
| 118 | + * @throws IllegalArgumentException |
| 119 | + * If the format is {@code null} |
| 120 | + * |
| 121 | + * @return Possibly-null {@link ImageProxy} of this member's old banner |
| 122 | + * |
| 123 | + * @see #getOldBannerUrl(ImageFormat) |
| 124 | + * @see DiscordAssets#memberBanner(ImageFormat, String, String, String) |
| 125 | + */ |
| 126 | + @Nullable |
| 127 | + public ImageProxy getOldBanner(@Nonnull ImageFormat format) { |
| 128 | + return DiscordAssets.memberBanner(format, getGuild().getId(), getUser().getId(), previous); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * The new banner id |
| 133 | + * |
| 134 | + * @return The new banner id |
| 135 | + */ |
| 136 | + @Nullable |
| 137 | + public String getNewBannerId() { |
| 138 | + return getNewValue(); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * The url of the new banner |
| 143 | + * |
| 144 | + * @return The url of the new banner |
| 145 | + */ |
| 146 | + @Nullable |
| 147 | + public String getNewBannerUrl() { |
| 148 | + return next == null |
| 149 | + ? null |
| 150 | + : getNewBannerUrl(next.startsWith("a_") ? ImageFormat.ANIMATED_WEBP : ImageFormat.PNG); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * The url of the new banner |
| 155 | + * |
| 156 | + * @param format |
| 157 | + * The format in which the image should be |
| 158 | + * |
| 159 | + * @throws IllegalArgumentException |
| 160 | + * If the format is {@code null} |
| 161 | + * |
| 162 | + * @return The url of the new banner |
| 163 | + * |
| 164 | + * @see DiscordAssets#memberBanner(ImageFormat, String, String, String) |
| 165 | + */ |
| 166 | + @Nullable |
| 167 | + public String getNewBannerUrl(@Nonnull ImageFormat format) { |
| 168 | + ImageProxy proxy = getNewBanner(format); |
| 169 | + return proxy == null ? null : proxy.getUrl(); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Returns an {@link ImageProxy} for this member's new banner. |
| 174 | + * |
| 175 | + * @return Possibly-null {@link ImageProxy} of this member's new banner |
| 176 | + * |
| 177 | + * @see #getNewBannerUrl() |
| 178 | + */ |
| 179 | + @Nullable |
| 180 | + public ImageProxy getNewBanner() { |
| 181 | + String newBannerUrl = getNewBannerUrl(); |
| 182 | + return newBannerUrl == null ? null : new ImageProxy(newBannerUrl); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Returns an {@link ImageProxy} for this member's new banner. |
| 187 | + * |
| 188 | + * @param format |
| 189 | + * The format in which the image should be |
| 190 | + * |
| 191 | + * @throws IllegalArgumentException |
| 192 | + * If the format is {@code null} |
| 193 | + * |
| 194 | + * @return Possibly-null {@link ImageProxy} of this member's new banner |
| 195 | + * |
| 196 | + * @see #getNewBannerUrl(ImageFormat) |
| 197 | + * @see DiscordAssets#memberBanner(ImageFormat, String, String, String) |
| 198 | + */ |
| 199 | + @Nullable |
| 200 | + public ImageProxy getNewBanner(@Nonnull ImageFormat format) { |
| 201 | + return DiscordAssets.memberBanner(format, getGuild().getId(), getUser().getId(), next); |
| 202 | + } |
| 203 | +} |
0 commit comments