/
MessageSticker.java
222 lines (203 loc) · 6.04 KB
/
MessageSticker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.api.entities;
import net.dv8tion.jda.api.utils.ImageProxy;
import net.dv8tion.jda.internal.utils.Helpers;
import javax.annotation.Nonnull;
import java.util.Set;
/**
* An object representing a sticker in a Discord message.
*
* @see Message#getStickers()
*/
public class MessageSticker implements ISnowflake
{
private final long id;
private final String name;
private final String description;
private final long packId;
private final StickerFormat formatType;
private final Set<String> tags;
/** Template for {@link #getIconUrl()} */
public static final String ICON_URL = "https://cdn.discordapp.com/stickers/%s.%s";
public MessageSticker(final long id, final String name, final String description, final long packId, final StickerFormat formatType, final Set<String> tags)
{
this.id = id;
this.name = name;
this.description = description;
this.packId = packId;
this.formatType = formatType;
this.tags = tags;
}
@Override
public long getIdLong()
{
return id;
}
/**
* The name of the sticker.
*
* @return the name of the sticker
*/
@Nonnull
public String getName()
{
return name;
}
/**
* The description of the sticker or empty String if the sticker doesn't have one.
*
* @return Possibly-empty String containing the description of the sticker
*/
@Nonnull
public String getDescription()
{
return description;
}
/**
* The ID of the pack the sticker is from.
*
* <p>If this sticker is from a guild, this will be the guild id instead.
*
* @return the ID of the pack the sticker is from
*/
@Nonnull
public String getPackId()
{
return Long.toUnsignedString(getPackIdLong());
}
/**
* The ID of the pack the sticker is from.
*
* <p>If this sticker is from a guild, this will be the guild id instead.
*
* @return the ID of the pack the sticker is from
*/
public long getPackIdLong()
{
return packId;
}
/**
* The url of the sticker image.
*
* @throws java.lang.IllegalStateException
* If the {@link StickerFormat StickerFormat} of this sticker is {@link StickerFormat#UNKNOWN UNKNOWN}
*
* @return The image url of the sticker
*/
@Nonnull
public String getIconUrl()
{
return Helpers.format(ICON_URL, getId(), formatType.getExtension());
}
/**
* Returns an {@link ImageProxy} for this sticker's image.
*
* @throws java.lang.IllegalStateException
* If the {@link StickerFormat} of this sticker is {@link StickerFormat#UNKNOWN UNKNOWN}
*
* @return Never-null {@link ImageProxy} of this sticker's image
*
* @see #getIconUrl()
*/
@Nonnull
public ImageProxy getIcon()
{
return new ImageProxy(getIconUrl());
}
/**
* The {@link StickerFormat Format} of the sticker.
*
* @return the format of the sticker
*/
@Nonnull
public StickerFormat getFormatType()
{
return formatType;
}
/**
* Set of tags of the sticker. Tags can be used instead of the name of the sticker as aliases.
*
* @return Possibly-empty unmodifiable Set of tags of the sticker
*/
@Nonnull
public Set<String> getTags()
{
return tags;
}
public enum StickerFormat
{
/**
* The PNG format.
*/
PNG(1, "png"),
/**
* The APNG format.
*/
APNG(2, "apng"),
/**
* The LOTTIE format.
* <br>Lottie isn't a standard renderable image. It is a JSON with data that can be rendered using the lottie library.
*
* @see <a href="https://airbnb.io/lottie/">Lottie website</a>
*/
LOTTIE(3, "json"),
/**
* Represents any unknown or unsupported {@link net.dv8tion.jda.api.entities.MessageSticker MessageSticker} format types.
*/
UNKNOWN(-1, null);
private final int id;
private final String extension;
StickerFormat(final int id, final String extension)
{
this.id = id;
this.extension = extension;
}
/**
* The file extension used for the sticker asset.
*
* @throws java.lang.IllegalStateException
* If the {@link StickerFormat StickerFormat} is {@link StickerFormat#UNKNOWN UNKNOWN}
*
* @return The file extension for this format
*/
@Nonnull
public String getExtension()
{
if (this == UNKNOWN)
throw new IllegalStateException("Can only get extension of a known format");
return extension;
}
/**
* Resolves the specified format identifier to the StickerFormat enum constant.
*
* @param id
* The id for this format
*
* @return The representative StickerFormat or UNKNOWN if it can't be resolved
*/
@Nonnull
public static MessageSticker.StickerFormat fromId(int id)
{
for (MessageSticker.StickerFormat stickerFormat : values())
{
if (stickerFormat.id == id)
return stickerFormat;
}
return UNKNOWN;
}
}
}