Skip to content

Commit

Permalink
Slowmode (#773)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shengaero authored and MinnDevelopment committed Oct 3, 2018
1 parent caf8aae commit e59c048
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/main/java/net/dv8tion/jda/core/audit/ActionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public enum ActionType
* <li>{@link net.dv8tion.jda.core.audit.AuditLogKey#CHANNEL_BITRATE CHANNEL_BITRATE} (VoiceChannel only)</li>
* <li>{@link net.dv8tion.jda.core.audit.AuditLogKey#CHANNEL_USER_LIMIT CHANNEL_USER_LIMIT} (VoiceChannel only)</li>
* <li>{@link net.dv8tion.jda.core.audit.AuditLogKey#CHANNEL_TOPIC CHANNEL_TOPIC} (TextChannel only)</li>
* <li>{@link net.dv8tion.jda.core.audit.AuditLogKey#CHANNEL_SLOWMODE CHANNEL_SLOWMODE} (TextChannel only)</li>
* <li>{@link net.dv8tion.jda.core.audit.AuditLogKey#CHANNEL_NSFW CHANNEL_NSFW} (TextChannel only)</li>
* <li>{@link net.dv8tion.jda.core.audit.AuditLogKey#CHANNEL_OVERRIDES CHANNEL_OVERRIDES}</li>
* <li>{@link net.dv8tion.jda.core.audit.AuditLogKey#CHANNEL_NAME CHANNEL_NAME}</li>
Expand All @@ -68,6 +69,7 @@ public enum ActionType
* <li>{@link net.dv8tion.jda.core.audit.AuditLogKey#CHANNEL_BITRATE CHANNEL_BITRATE} (VoiceChannel only)</li>
* <li>{@link net.dv8tion.jda.core.audit.AuditLogKey#CHANNEL_USER_LIMIT CHANNEL_USER_LIMIT} (VoiceChannel only)</li>
* <li>{@link net.dv8tion.jda.core.audit.AuditLogKey#CHANNEL_TOPIC CHANNEL_TOPIC} (TextChannel only)</li>
* <li>{@link net.dv8tion.jda.core.audit.AuditLogKey#CHANNEL_SLOWMODE CHANNEL_SLOWMODE} (TextChannel only)</li>
* <li>{@link net.dv8tion.jda.core.audit.AuditLogKey#CHANNEL_NSFW CHANNEL_NSFW} (TextChannel only)</li>
* <li>{@link net.dv8tion.jda.core.audit.AuditLogKey#CHANNEL_NAME CHANNEL_NAME}</li>
* <li>{@link net.dv8tion.jda.core.audit.AuditLogKey#CHANNEL_TYPE CHANNEL_TYPE}</li>
Expand All @@ -83,6 +85,7 @@ public enum ActionType
* <li>{@link net.dv8tion.jda.core.audit.AuditLogKey#CHANNEL_BITRATE CHANNEL_BITRATE} (VoiceChannel only)</li>
* <li>{@link net.dv8tion.jda.core.audit.AuditLogKey#CHANNEL_USER_LIMIT CHANNEL_USER_LIMIT} (VoiceChannel only)</li>
* <li>{@link net.dv8tion.jda.core.audit.AuditLogKey#CHANNEL_TOPIC CHANNEL_TOPIC} (TextChannel only)</li>
* <li>{@link net.dv8tion.jda.core.audit.AuditLogKey#CHANNEL_SLOWMODE CHANNEL_SLOWMODE} (TextChannel only)</li>
* <li>{@link net.dv8tion.jda.core.audit.AuditLogKey#CHANNEL_NSFW CHANNEL_NSFW} (TextChannel only)</li>
* <li>{@link net.dv8tion.jda.core.audit.AuditLogKey#CHANNEL_OVERRIDES CHANNEL_OVERRIDES}</li>
* <li>{@link net.dv8tion.jda.core.audit.AuditLogKey#CHANNEL_NAME CHANNEL_NAME}</li>
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/net/dv8tion/jda/core/audit/AuditLogKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ public enum AuditLogKey
*/
CHANNEL_TOPIC("topic"),

/**
* Change of the {@link net.dv8tion.jda.core.entities.TextChannel#getSlowmode() TextChannel.getSlowmode()} value.
* <br>Only for {@link net.dv8tion.jda.core.entities.ChannelType#TEXT ChannelType.TEXT}
*
* <p>Expected type: <b>Integer</b>
*/
CHANNEL_SLOWMODE("rate_limit_per_user"),

/**
* Change of the {@link net.dv8tion.jda.core.entities.VoiceChannel#getBitrate() VoiceChannel.getBitrate()} value.
* <br>Only for {@link net.dv8tion.jda.core.entities.ChannelType#VOICE ChannelType.VOICE}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,9 +609,10 @@ public TextChannel createTextChannel(GuildImpl guildObj, JSONObject json, long g
.setParent(Helpers.optLong(json, "parent_id", 0))
.setLastMessageId(Helpers.optLong(json, "last_message_id", 0))
.setName(json.getString("name"))
.setTopic(json.optString("topic"))
.setTopic(json.optString("topic", null))
.setPosition(json.getInt("position"))
.setNSFW(Helpers.optBoolean(json, "nsfw"));
.setNSFW(Helpers.optBoolean(json, "nsfw"))
.setSlowmode(Helpers.optInt(json, "rate_limit_per_user", 0));
if (playbackCache)
getJDA().getEventCache().playbackCache(EventCache.Type.CHANNEL, id);
return channel;
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/net/dv8tion/jda/core/entities/TextChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ public interface TextChannel extends Channel, MessageChannel, Comparable<TextCha
*/
boolean isNSFW();

/**
* The slowmode set for this TextChannel.
* <br>If slowmode is set this returns an {@code int} between 1 and 120. If not set this returns {@code 0}.
*
* <p>Note that only {@link net.dv8tion.jda.core.AccountType#CLIENT CLIENT} type accounts are
* affected by slowmode, and that {@link net.dv8tion.jda.core.AccountType#BOT BOT} accounts
* are immune to the restrictions.
* <br>Having {@link net.dv8tion.jda.core.Permission#MESSAGE_MANAGE MESSAGE_MANAGE} or
* {@link net.dv8tion.jda.core.Permission#MANAGE_CHANNEL MANAGE_CHANNEL} permission also
* grants immunity to slowmode.
*
* @return The slowmode for this TextChannel, between 1 and 120, or {@code 0} if no slowmode is set.
*/
int getSlowmode();

/**
* Retrieves the {@link net.dv8tion.jda.core.entities.Webhook Webhooks} attached to this TextChannel.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class TextChannelImpl extends AbstractChannelImpl<TextChannelImpl> implem
private String topic;
private long lastMessageId;
private boolean nsfw;
private int slowmode;

public TextChannelImpl(long id, GuildImpl guild)
{
Expand Down Expand Up @@ -263,6 +264,12 @@ public boolean isNSFW() {
return nsfw || name.equals("nsfw") || name.startsWith("nsfw-");
}

@Override
public int getSlowmode()
{
return slowmode;
}

@Override
public List<Member> getMembers()
{
Expand Down Expand Up @@ -537,6 +544,12 @@ public TextChannelImpl setNSFW(boolean nsfw)
return this;
}

public TextChannelImpl setSlowmode(int slowmode)
{
this.slowmode = slowmode;
return this;
}

// -- internal --

private void checkVerification()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2015-2018 Austin Keener & Michael Ritter & Florian Spieß
*
* 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.core.events.channel.text.update;

import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.entities.TextChannel;

/**
* Indicates that a {@link net.dv8tion.jda.core.entities.TextChannel TextChannel}'s slowmode changed.
*
* <p>Can be used to detect when a TextChannel slowmode changes and get its previous value.
*
* <p>Identifier: {@code slowmode}
*/
public class TextChannelUpdateSlowmodeEvent extends GenericTextChannelUpdateEvent<Integer>
{
public static final String IDENTIFIER = "slowmode";

public TextChannelUpdateSlowmodeEvent(JDA api, long responseNumber, TextChannel channel, int oldSlowmode)
{
super(api, responseNumber, channel, oldSlowmode, channel.getSlowmode(), IDENTIFIER);
}

/**
* The old slowmode.
*
* @return The old slowmode.
*/
public int getOldSlowmode()
{
return getOldValue();
}

/**
* The new slowmode.
*
* @return The new slowmode.
*/
public int getNewSlowmode()
{
return getNewValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ protected Long handleInternally(JSONObject content)
final int position = content.getInt("position");
final String name = content.getString("name");
final boolean nsfw = Helpers.optBoolean(content, "nsfw");
final int slowmode = Helpers.optInt(content, "rate_limit_per_user", 0);
JSONArray permOverwrites = content.getJSONArray("permission_overwrites");
switch (type)
{
Expand All @@ -85,6 +86,7 @@ protected Long handleInternally(JSONObject content)
final String oldTopic = textChannel.getTopic();
final int oldPosition = textChannel.getPositionRaw();
final boolean oldNsfw = textChannel.isNSFW();
final int oldSlowmode = textChannel.getSlowmode();
if (!Objects.equals(oldName, name))
{
textChannel.setName(name);
Expand Down Expand Up @@ -124,7 +126,16 @@ protected Long handleInternally(JSONObject content)
getJDA().getEventManager().handle(
new TextChannelUpdateNSFWEvent(
getJDA(), responseNumber,
textChannel, nsfw));
textChannel, oldNsfw));
}

if (oldSlowmode != slowmode)
{
textChannel.setSlowmode(slowmode);
getJDA().getEventManager().handle(
new TextChannelUpdateSlowmodeEvent(
getJDA(), responseNumber,
textChannel, oldSlowmode));
}

applyPermissions(textChannel, content, permOverwrites, contained, changed);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/dv8tion/jda/core/hooks/ListenerAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ public void onTextChannelUpdatePosition(TextChannelUpdatePositionEvent event) {}
public void onTextChannelUpdatePermissions(TextChannelUpdatePermissionsEvent event) {}
public void onTextChannelUpdateNSFW(TextChannelUpdateNSFWEvent event) {}
public void onTextChannelUpdateParent(TextChannelUpdateParentEvent event) {}
public void onTextChannelUpdateSlowmode(TextChannelUpdateSlowmodeEvent event) {}
public void onTextChannelCreate(TextChannelCreateEvent event) {}

//VoiceChannel Events
Expand Down Expand Up @@ -464,6 +465,8 @@ else if (event instanceof TextChannelUpdateNSFWEvent)
onTextChannelUpdateNSFW((TextChannelUpdateNSFWEvent) event);
else if (event instanceof TextChannelUpdateParentEvent)
onTextChannelUpdateParent((TextChannelUpdateParentEvent) event);
else if (event instanceof TextChannelUpdateSlowmodeEvent)
onTextChannelUpdateSlowmode((TextChannelUpdateSlowmodeEvent) event);
else if (event instanceof TextChannelDeleteEvent)
onTextChannelDelete((TextChannelDeleteEvent) event);

Expand Down
55 changes: 48 additions & 7 deletions src/main/java/net/dv8tion/jda/core/managers/ChannelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,23 @@
public class ChannelManager extends ManagerBase
{
/** Used to reset the name field */
public static final long NAME = 0x1;
public static final long NAME = 0x1;
/** Used to reset the parent field */
public static final long PARENT = 0x2;
public static final long PARENT = 0x2;
/** Used to reset the topic field */
public static final long TOPIC = 0x4;
public static final long TOPIC = 0x4;
/** Used to reset the position field */
public static final long POSITION = 0x8;
public static final long POSITION = 0x8;
/** Used to reset the nsfw field */
public static final long NSFW = 0x10;
public static final long NSFW = 0x10;
/** Used to reset the userlimit field */
public static final long USERLIMIT = 0x20;
public static final long USERLIMIT = 0x20;
/** Used to reset the bitrate field */
public static final long BITRATE = 0x40;
public static final long BITRATE = 0x40;
/** Used to reset the permission field */
public static final long PERMISSION = 0x80;
/** Used to reset the rate-limit per user field */
public static final long SLOWMODE = 0x100;

protected final UpstreamReference<Channel> channel;

Expand All @@ -78,6 +80,7 @@ public class ChannelManager extends ManagerBase
protected String topic;
protected int position;
protected boolean nsfw;
protected int slowmode;
protected int userlimit;
protected int bitrate;

Expand Down Expand Up @@ -148,6 +151,7 @@ public Guild getGuild()
* <li>{@link #TOPIC}</li>
* <li>{@link #POSITION}</li>
* <li>{@link #NSFW}</li>
* <li>{@link #SLOWMODE}</li>
* <li>{@link #USERLIMIT}</li>
* <li>{@link #BITRATE}</li>
* <li>{@link #PERMISSION}</li>
Expand Down Expand Up @@ -587,6 +591,41 @@ public ChannelManager setNSFW(boolean nsfw)
return this;
}

/**
* Sets the <b><u>slowmode</u></b> of the selected {@link net.dv8tion.jda.core.entities.TextChannel TextChannel}.
* <br>Provide {@code 0} to reset the slowmode of the {@link net.dv8tion.jda.core.entities.TextChannel TextChannel}
*
* <p>A channel slowmode <b>must not</b> be negative nor greater than {@code 120}!
* <br><b>This is only available to {@link net.dv8tion.jda.core.entities.TextChannel TextChannels}</b>
*
* <p>Note that only {@link net.dv8tion.jda.core.AccountType#CLIENT CLIENT} type accounts are
* affected by slowmode, and that {@link net.dv8tion.jda.core.AccountType#BOT BOT} accounts
* are immune to the restrictions.
* <br>Having {@link net.dv8tion.jda.core.Permission#MESSAGE_MANAGE MESSAGE_MANAGE} or
* {@link net.dv8tion.jda.core.Permission#MANAGE_CHANNEL MANAGE_CHANNEL} permission also
* grants immunity to slowmode.
*
* @param slowmode
* The new slowmode for the selected {@link net.dv8tion.jda.core.entities.TextChannel TextChannel}
*
* @throws IllegalStateException
* If the selected {@link net.dv8tion.jda.core.entities.Channel Channel}'s type is not {@link net.dv8tion.jda.core.entities.ChannelType#TEXT TEXT}
* @throws IllegalArgumentException
* If the provided slowmode is negative or greater than {@code 120}
*
* @return ChannelManager for chaining convenience
*/
@CheckReturnValue
public ChannelManager setSlowmode(int slowmode)
{
if (getType() != ChannelType.TEXT)
throw new IllegalStateException("Can only set slowmode on text channels");
Checks.check(slowmode <= 120 && slowmode >= 0, "Slowmode per user must be between 0 and 120 (seconds)!");
this.slowmode = slowmode;
set |= SLOWMODE;
return this;
}

/**
* Sets the <b><u>user-limit</u></b> of the selected {@link net.dv8tion.jda.core.entities.VoiceChannel VoiceChannel}.
* <br>Provide {@code 0} to reset the user-limit of the {@link net.dv8tion.jda.core.entities.VoiceChannel VoiceChannel}
Expand Down Expand Up @@ -661,6 +700,8 @@ protected RequestBody finalizeData()
frame.put("topic", opt(topic));
if (shouldUpdate(NSFW))
frame.put("nsfw", nsfw);
if (shouldUpdate(SLOWMODE))
frame.put("rate_limit_per_user", slowmode);
if (shouldUpdate(USERLIMIT))
frame.put("user_limit", userlimit);
if (shouldUpdate(BITRATE))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class ChannelAction extends AuditableRestAction<Channel>
// --text only--
protected String topic = null;
protected Boolean nsfw = null;
protected Integer slowmode = null;

// --voice only--
protected Integer bitrate = null;
Expand Down Expand Up @@ -170,6 +171,33 @@ public ChannelAction setNSFW(boolean nsfw)
return this;
}

/**
* Sets the slowmode value, which limits the amount of time that individual users must wait
* between sending messages in the new TextChannel. This is measured in seconds.
*
* <p>Note that only {@link net.dv8tion.jda.core.AccountType#CLIENT CLIENT} type accounts are
* affected by slowmode, and that {@link net.dv8tion.jda.core.AccountType#BOT BOT} accounts
* are immune to the restrictions.
* <br>Having {@link net.dv8tion.jda.core.Permission#MESSAGE_MANAGE MESSAGE_MANAGE} or
* {@link net.dv8tion.jda.core.Permission#MANAGE_CHANNEL MANAGE_CHANNEL} permission also
* grants immunity to slowmode.
*
* @param slowmode
* The number of seconds required to wait between sending messages in the channel.
*
* @throws IllegalArgumentException
* If the {@code slowmode} is greater than 120, or less than 0
*
* @return The current ChannelAction, for chaining convenience
*/
@CheckReturnValue
public ChannelAction setSlowmode(int slowmode)
{
Checks.check(slowmode <= 120 && slowmode >= 0, "Slowmode must be between 0 and 120 (seconds)!");
this.slowmode = slowmode;
return this;
}

/**
* Adds a new Role or Member {@link net.dv8tion.jda.core.entities.PermissionOverride PermissionOverride}
* for the new Channel.
Expand Down Expand Up @@ -343,6 +371,8 @@ protected RequestBody finalizeData()
object.put("topic", topic);
if (nsfw != null)
object.put("nsfw", nsfw);
if (slowmode != null)
object.put("rate_limit_per_user", slowmode);
}
if (type != ChannelType.CATEGORY && parent != null)
object.put("parent_id", parent.getId());
Expand Down

0 comments on commit e59c048

Please sign in to comment.