Skip to content

Commit 9149723

Browse files
Support for viewing ban reasons (discord-jda#585)
Added Guild#getBanList to replace Guild#getBans
1 parent 7755b79 commit 9149723

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

src/main/java/net/dv8tion/jda/core/entities/Guild.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import net.dv8tion.jda.core.utils.cache.SnowflakeCacheView;
2929

3030
import javax.annotation.CheckReturnValue;
31+
import javax.annotation.Nonnull;
3132
import javax.annotation.Nullable;
3233
import java.util.Collection;
3334
import java.util.List;
@@ -785,10 +786,41 @@ default List<Emote> getEmotesByName(String name, boolean ignoreCase)
785786
*
786787
* @return {@link net.dv8tion.jda.core.requests.RestAction RestAction} - Type: {@literal List<}{@link net.dv8tion.jda.core.entities.User User}{@literal >}
787788
* <br>An unmodifiable list of all users currently banned from this Guild
789+
*
790+
* @deprecated
791+
* Use {@link #getBanList()} instead
788792
*/
793+
@Deprecated
789794
@CheckReturnValue
790795
RestAction<List<User>> getBans();
791796

797+
/**
798+
* Gets an unmodifiable list of the currently banned {@link net.dv8tion.jda.core.entities.User Users}.
799+
* <br>If you wish to ban or unban a user, please {@link GuildController#ban(User, int) GuildController.ban(User, int)} or
800+
* {@link GuildController#unban(User) GuildController.ban(User)}.
801+
*
802+
* <p>Possible {@link net.dv8tion.jda.core.requests.ErrorResponse ErrorResponses} caused by
803+
* the returned {@link net.dv8tion.jda.core.requests.RestAction RestAction} include the following:
804+
* <ul>
805+
* <li>{@link net.dv8tion.jda.core.requests.ErrorResponse#MISSING_PERMISSIONS MISSING_PERMISSIONS}
806+
* <br>The ban list cannot be fetched due to a permission discrepancy</li>
807+
*
808+
* <li>{@link net.dv8tion.jda.core.requests.ErrorResponse#MISSING_ACCESS MISSING_ACCESS}
809+
* <br>We were removed from the Guild before finishing the task</li>
810+
* </ul>
811+
*
812+
* @throws net.dv8tion.jda.core.exceptions.InsufficientPermissionException
813+
* If the logged in account does not have the {@link net.dv8tion.jda.core.Permission#BAN_MEMBERS} permission.
814+
* @throws net.dv8tion.jda.core.exceptions.GuildUnavailableException
815+
* If the guild is temporarily not {@link #isAvailable() available}
816+
*
817+
* @return {@link net.dv8tion.jda.core.requests.RestAction RestAction} - Type: {@literal List<}{@link net.dv8tion.jda.core.entities.Guild.Ban Ban}{@literal >}
818+
* <br>An unmodifiable list of all users currently banned from this Guild
819+
*/
820+
@Nonnull
821+
@CheckReturnValue
822+
RestAction<List<Ban>> getBanList();
823+
792824
/**
793825
* The method calculates the amount of Members that would be pruned if {@link GuildController#prune(int)} was executed.
794826
* Prunability is determined by a Member being offline for at least <i>days</i> days.
@@ -1373,4 +1405,50 @@ public static ExplicitContentLevel fromKey(int key)
13731405
return UNKNOWN;
13741406
}
13751407
}
1408+
1409+
/**
1410+
* Represents a Ban object.
1411+
*
1412+
* @see #getBanList()
1413+
* @see <a href="https://discordapp.com/developers/docs/resources/guild#ban-object" target="_blank">Discord Docs: Ban Object</a>
1414+
*/
1415+
class Ban
1416+
{
1417+
protected final User user;
1418+
protected final String reason;
1419+
1420+
public Ban(User user, String reason)
1421+
{
1422+
this.user = user;
1423+
this.reason = reason;
1424+
}
1425+
1426+
/**
1427+
* The {@link net.dv8tion.jda.core.entities.User User} that was banned
1428+
*
1429+
* @return The banned User
1430+
*/
1431+
@Nonnull
1432+
public User getUser()
1433+
{
1434+
return user;
1435+
}
1436+
1437+
/**
1438+
* The reason why this user was banned
1439+
*
1440+
* @return The reason for this ban, or {@code null}
1441+
*/
1442+
@Nullable
1443+
public String getReason()
1444+
{
1445+
return reason;
1446+
}
1447+
1448+
@Override
1449+
public String toString()
1450+
{
1451+
return "GuildBan:" + user + (reason == null ? "" : '(' + reason + ')');
1452+
}
1453+
}
13761454
}

src/main/java/net/dv8tion/jda/core/entities/impl/GuildImpl.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.json.JSONException;
4747
import org.json.JSONObject;
4848

49+
import javax.annotation.Nonnull;
4950
import javax.annotation.Nullable;
5051
import java.time.OffsetDateTime;
5152
import java.time.temporal.ChronoUnit;
@@ -316,6 +317,42 @@ protected void handleResponse(Response response, Request<List<User>> request)
316317
};
317318
}
318319

320+
@Nonnull
321+
@Override
322+
public RestAction<List<Ban>> getBanList()
323+
{
324+
if (!isAvailable())
325+
throw new GuildUnavailableException();
326+
if (!getSelfMember().hasPermission(Permission.BAN_MEMBERS))
327+
throw new InsufficientPermissionException(Permission.BAN_MEMBERS);
328+
329+
Route.CompiledRoute route = Route.Guilds.GET_BANS.compile(getId());
330+
return new RestAction<List<Ban>>(getJDA(), route)
331+
{
332+
@Override
333+
protected void handleResponse(Response response, Request<List<Ban>> request)
334+
{
335+
if (!response.isOk())
336+
{
337+
request.onFailure(response);
338+
return;
339+
}
340+
341+
EntityBuilder builder = api.getEntityBuilder();
342+
List<Ban> bans = new LinkedList<>();
343+
JSONArray bannedArr = response.getArray();
344+
345+
for (int i = 0; i < bannedArr.length(); i++)
346+
{
347+
final JSONObject object = bannedArr.getJSONObject(i);
348+
JSONObject user = object.getJSONObject("user");
349+
bans.add(new Ban(builder.createFakeUser(user, false), object.optString("reason", null)));
350+
}
351+
request.onSuccess(Collections.unmodifiableList(bans));
352+
}
353+
};
354+
}
355+
319356
@Override
320357
public RestAction<Integer> getPrunableMemberCount(int days)
321358
{

0 commit comments

Comments
 (0)