Skip to content

Commit

Permalink
Add support for webhook types
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Nov 26, 2019
1 parent 0a4532b commit a7e59a2
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/main/java/net/dv8tion/jda/api/entities/Webhook.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ public interface Webhook extends ISnowflake, IFakeable
@Nonnull
JDA getJDA();

/**
* The {@link WebhookType} of this webhook.
* <br>Webhooks of type {@link WebhookType#FOLLOWER} don't have a token.
*
* @return The {@link WebhookType}
*/
@Nonnull
WebhookType getType();

/**
* The {@link net.dv8tion.jda.api.entities.Guild Guild} instance
* for this Webhook.
Expand Down
65 changes: 65 additions & 0 deletions src/main/java/net/dv8tion/jda/api/entities/WebhookType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2015-2019 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 javax.annotation.Nonnull;

public enum WebhookType
{
/** Placeholder for unsupported types */
UNKNOWN(-1),
/** Normal webhooks that can be used for sending messages */
INCOMING(1),
/** Webhook responsible for re-posting messages from another channel */
FOLLOWER(2);

private final int key;

WebhookType(int key)
{
this.key = key;
}

/**
* The raw api key for this type
*
* @return The api key, or -1 for {@link #UNKNOWN}
*/
public int getKey()
{
return key;
}

/**
* Resolves the provided raw api key to the corresponding webhook type.
*
* @param key
* The key
*
* @return The WebhookType or {@link #UNKNOWN}
*/
@Nonnull
public static WebhookType fromKey(int key)
{
for (WebhookType type : values())
{
if (type.key == key)
return type;
}
return UNKNOWN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,7 @@ public WebhookImpl createWebhook(DataObject object)
final long guildId = object.getLong("guild_id");
final long channelId = object.getLong("channel_id");
final String token = object.getString("token", null);
final WebhookType type = WebhookType.fromKey(object.getInt("type", -1));

TextChannel channel = getJDA().getTextChannelById(channelId);
if (channel == null)
Expand Down Expand Up @@ -1183,7 +1184,7 @@ public WebhookImpl createWebhook(DataObject object)
}
}

return new WebhookImpl(channel, id)
return new WebhookImpl(channel, id, type)
.setToken(token)
.setOwner(owner == null ? null : channel.getGuild().getMember(owner))
.setUser(defaultUser);
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/net/dv8tion/jda/internal/entities/WebhookImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,24 @@ public class WebhookImpl implements Webhook
private final ReentrantLock mngLock = new ReentrantLock();
private final TextChannel channel;
private final long id;
private final WebhookType type;

private Member owner;
private User user;
private String token;

public WebhookImpl(TextChannel channel, long id)
public WebhookImpl(TextChannel channel, long id, WebhookType type)
{
this.channel = channel;
this.id = id;
this.type = type;
}

@Nonnull
@Override
public WebhookType getType()
{
return type;
}

@Nonnull
Expand Down

0 comments on commit a7e59a2

Please sign in to comment.