Skip to content

Commit

Permalink
Added the option to get and set a guilds system channel (#431)
Browse files Browse the repository at this point in the history
  • Loading branch information
Almighty-Alpaca authored and MinnDevelopment committed Sep 2, 2017
1 parent 6c7e405 commit c53c4c7
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/main/java/net/dv8tion/jda/core/audit/ActionType.java
Expand Up @@ -37,6 +37,7 @@ public enum ActionType
* <li>{@link net.dv8tion.jda.core.audit.AuditLogKey#GUILD_OWNER GUILD_OWNER}</li>
* <li>{@link net.dv8tion.jda.core.audit.AuditLogKey#GUILD_REGION GUILD_REGION}</li>
* <li>{@link net.dv8tion.jda.core.audit.AuditLogKey#GUILD_SPLASH GUILD_SPLASH}</li>
* <li>{@link net.dv8tion.jda.core.audit.AuditLogKey#GUILD_SYSTEM_CHANNEL GUILD_SYSTEM_CHANNEL}</li>
* </ul>
*/
GUILD_UPDATE(1, TargetType.GUILD),
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/net/dv8tion/jda/core/audit/AuditLogKey.java
Expand Up @@ -68,13 +68,20 @@ public enum AuditLogKey

/**
* Change of the {@link net.dv8tion.jda.core.entities.Guild#getAfkChannel() Guild.getAfkChannel()} value represented by a VoiceChannel ID.
* <br>Use with {@link net.dv8tion.jda.core.entities.Guild#getVoiceChannelById(String)
* Guild.getVoiceChannelById(String)}
* <br>Use with {@link net.dv8tion.jda.core.entities.Guild#getVoiceChannelById(String) Guild.getVoiceChannelById(String)}
*
* <p>Expected type: <b>String</b>
*/
GUILD_AFK_CHANNEL("afk_channel_id"),

/**
* Change of the {@link net.dv8tion.jda.core.entities.Guild#getSystemChannel() Guild.getSystemChannel()} value represented by a TextChannel ID.
* <br>Use with {@link net.dv8tion.jda.core.entities.Guild#getTextChannelById(String) Guild.getTextChannelById(String)}
*
* <p>Expected type: <b>String</b>
*/
GUILD_SYSTEM_CHANNEL("system_channel_id"),

/**
* Change of the {@link net.dv8tion.jda.core.entities.Guild#getExplicitContentLevel() Guild.getExplicitContentLevel()} of a Guild.
* <br>Use with {@link net.dv8tion.jda.core.entities.Guild.ExplicitContentLevel#fromKey(int) Guild.ExplicitContentLevel.fromKey(int)}
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/net/dv8tion/jda/core/entities/EntityBuilder.java
Expand Up @@ -215,16 +215,19 @@ public void createGuildFirstPass(JSONObject guild, Consumer<Guild> secondPassCal
}
else if (type == ChannelType.VOICE)
{
VoiceChannel newChannel = createVoiceChannel(channel, guildObj.getIdLong(), false);
if (!guild.isNull("afk_channel_id")
&& newChannel.getId().equals(guild.getString("afk_channel_id")))
guildObj.setAfkChannel(newChannel);
createVoiceChannel(channel, guildObj.getIdLong(), false);
}
else
WebSocketClient.LOG.fatal("Received a channel for a guild that isn't a text or voice channel. JSON: " + channel);
}
}

if (!guild.isNull("system_channel_id"))
guildObj.setSystemChannel(guildObj.getTextChannelsMap().get(guild.getLong("system_channel_id")));

if (!guild.isNull("afk_channel_id"))
guildObj.setAfkChannel(guildObj.getVoiceChannelMap().get(guild.getLong("afk_channel_id")));

//If the members that we were provided with (and loaded above) were not all of the
// the members in this guild, then we need to request more users from Discord using
// op 9 (GUILD_MEMBERS_CHUNK). To do so, we will cache the guild's JSON so we can properly
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/net/dv8tion/jda/core/entities/Guild.java
Expand Up @@ -106,6 +106,18 @@ public interface Guild extends ISnowflake
*/
VoiceChannel getAfkChannel();

/**
* Provides the {@link net.dv8tion.jda.core.entities.TextChannel TextChannel} that has been set as the channel
* which newly joined {@link net.dv8tion.jda.core.entities.Member Members} will be announced in.
* <br>If no channel has been set as the system channel, this returns {@code null}.
* <p>
* This value can be modified using {@link net.dv8tion.jda.core.managers.GuildManager#setSystemChannel(TextChannel)}
* or {@link net.dv8tion.jda.core.managers.GuildManagerUpdatable#getSystemChannelField()}.
*
* @return Possibly-null {@link net.dv8tion.jda.core.entities.TextChannel TextChannel} that is the system Channel.
*/
TextChannel getSystemChannel();

/**
* The {@link net.dv8tion.jda.core.entities.Member Member} object of the owner of this {@link net.dv8tion.jda.core.entities.Guild Guild}.
* <p>
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/net/dv8tion/jda/core/entities/impl/GuildImpl.java
Expand Up @@ -72,6 +72,7 @@ public class GuildImpl implements Guild
private Region region;
private TextChannel publicChannel;
private VoiceChannel afkChannel;
private TextChannel systemChannel;
private Role publicRole;
private VerificationLevel verificationLevel;
private NotificationLevel defaultNotificationLevel;
Expand Down Expand Up @@ -123,6 +124,12 @@ public VoiceChannel getAfkChannel()
return afkChannel;
}

@Override
public TextChannel getSystemChannel()
{
return systemChannel;
}

@Override
public RestAction<List<Webhook>> getWebhooks()
{
Expand Down Expand Up @@ -739,6 +746,12 @@ public GuildImpl setAfkChannel(VoiceChannel afkChannel)
return this;
}

public GuildImpl setSystemChannel(TextChannel systemChannel)
{
this.systemChannel = systemChannel;
return this;
}

public GuildImpl setPublicRole(Role publicRole)
{
this.publicRole = publicRole;
Expand Down
@@ -0,0 +1,48 @@
/*
* Copyright 2015-2017 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.guild.update;

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

/**
* <b><u>GuildUpdateSystemChannelEvent</u></b><br>
* Fired if the {@link net.dv8tion.jda.core.entities.Guild#getSystemChannel() system channel} of a {@link net.dv8tion.jda.core.entities.Guild Guild} has been changed.<br>
* <br>
* Use: Retrieve the {@link #getOldSystemChannel() previous system channel} after a change.<p>
*/
public class GuildUpdateSystemChannelEvent extends GenericGuildUpdateEvent
{
private final TextChannel oldSystemChannel;

public GuildUpdateSystemChannelEvent(JDA api, long responseNumber, Guild guild, TextChannel oldSystemChannel)
{
super(api, responseNumber, guild);
this.oldSystemChannel = oldSystemChannel;
}

/**
* Returns the previous system channel.
*
* @return The previous system channel
*/
public TextChannel getOldSystemChannel()
{
return oldSystemChannel;
}
}
13 changes: 13 additions & 0 deletions src/main/java/net/dv8tion/jda/core/handle/GuildUpdateHandler.java
Expand Up @@ -18,6 +18,7 @@
import net.dv8tion.jda.core.Region;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.TextChannel;
import net.dv8tion.jda.core.entities.VoiceChannel;
import net.dv8tion.jda.core.entities.impl.GuildImpl;
import net.dv8tion.jda.core.entities.impl.JDAImpl;
Expand Down Expand Up @@ -55,6 +56,9 @@ protected Long handleInternally(JSONObject content)
VoiceChannel afkChannel = !content.isNull("afk_channel_id")
? guild.getVoiceChannelMap().get(content.getLong("afk_channel_id"))
: null;
TextChannel systemChannel = !content.isNull("system_channel_id")
? guild.getTextChannelsMap().get(content.getLong("system_channel_id"))
: null;

if (!Objects.equals(owner, guild.getOwner()))
{
Expand Down Expand Up @@ -155,6 +159,15 @@ protected Long handleInternally(JSONObject content)
api, responseNumber,
guild, oldAfkChannel));
}
if (!Objects.equals(systemChannel, guild.getSystemChannel()))
{
TextChannel oldSystemChannel = guild.getSystemChannel();
guild.setSystemChannel(systemChannel);
api.getEventManager().handle(
new GuildUpdateSystemChannelEvent(
api, responseNumber,
guild, oldSystemChannel));
}
return null;
}
}
3 changes: 3 additions & 0 deletions src/main/java/net/dv8tion/jda/core/hooks/ListenerAdapter.java
Expand Up @@ -193,6 +193,7 @@ public void onGuildUnban(GuildUnbanEvent event) {}

//Guild Update Events
public void onGuildUpdateAfkChannel(GuildUpdateAfkChannelEvent event) {}
public void onGuildUpdateSystemChannel(GuildUpdateSystemChannelEvent event) {}
public void onGuildUpdateAfkTimeout(GuildUpdateAfkTimeoutEvent event) {}
public void onGuildUpdateIcon(GuildUpdateIconEvent event) {}
public void onGuildUpdateMFALevel(GuildUpdateMFALevelEvent event) {}
Expand Down Expand Up @@ -483,6 +484,8 @@ else if (event instanceof GuildUnbanEvent)
//Guild Update Events
else if (event instanceof GuildUpdateAfkChannelEvent)
onGuildUpdateAfkChannel((GuildUpdateAfkChannelEvent) event);
else if (event instanceof GuildUpdateSystemChannelEvent)
onGuildUpdateSystemChannel((GuildUpdateSystemChannelEvent) event);
else if (event instanceof GuildUpdateAfkTimeoutEvent)
onGuildUpdateAfkTimeout((GuildUpdateAfkTimeoutEvent) event);
else if (event instanceof GuildUpdateIconEvent)
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/net/dv8tion/jda/core/managers/GuildManager.java
Expand Up @@ -20,6 +20,7 @@
import net.dv8tion.jda.core.Region;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.Icon;
import net.dv8tion.jda.core.entities.TextChannel;
import net.dv8tion.jda.core.entities.VoiceChannel;
import net.dv8tion.jda.core.requests.restaction.AuditableRestAction;

Expand Down Expand Up @@ -171,6 +172,28 @@ public AuditableRestAction<Void> setAfkChannel(VoiceChannel afkChannel)
return updatable.getAfkChannelField().setValue(afkChannel).update();
}

/**
* Sets the system {@link net.dv8tion.jda.core.entities.TextChannel TextChannel} of this {@link net.dv8tion.jda.core.entities.Guild Guild}.
* More information can be found {@link GuildManagerUpdatable#getSystemChannelField() here}!
*
* <p>For information on possible {@link net.dv8tion.jda.core.requests.ErrorResponse ErrorResponses}
* by the returned {@link net.dv8tion.jda.core.requests.RestAction RestAction} see {@link GuildManagerUpdatable#update() #update()}
*
* @param systemChannel
* The new system channel for this {@link net.dv8tion.jda.core.entities.Guild Guild}
*
* @return {@link net.dv8tion.jda.core.requests.restaction.AuditableRestAction AuditableRestAction}
* <br>Update RestAction from {@link GuildManagerUpdatable#update() #update()}
*
* @see net.dv8tion.jda.core.managers.GuildManagerUpdatable#getSystemChannelField()
* @see net.dv8tion.jda.core.managers.GuildManagerUpdatable#update()
*/
@CheckReturnValue
public AuditableRestAction<Void> setSystemChannel(TextChannel systemChannel)
{
return updatable.getSystemChannelField().setValue(systemChannel).update();
}

/**
* Sets the afk {@link net.dv8tion.jda.core.entities.Guild.Timeout Timeout} of this {@link net.dv8tion.jda.core.entities.Guild Guild}.
* More information can be found {@link GuildManagerUpdatable#getAfkTimeoutField() here}!
Expand Down
Expand Up @@ -21,6 +21,7 @@
import net.dv8tion.jda.core.Region;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.Icon;
import net.dv8tion.jda.core.entities.TextChannel;
import net.dv8tion.jda.core.entities.VoiceChannel;
import net.dv8tion.jda.core.exceptions.GuildUnavailableException;
import net.dv8tion.jda.core.exceptions.InsufficientPermissionException;
Expand Down Expand Up @@ -59,6 +60,7 @@ public class GuildManagerUpdatable
protected GuildField<Icon> splash;
protected GuildField<Region> region;
protected GuildField<VoiceChannel> afkChannel;
protected GuildField<TextChannel> systemChannel;
protected GuildField<Guild.VerificationLevel> verificationLevel;
protected GuildField<Guild.NotificationLevel> defaultNotificationLevel;
protected GuildField<Guild.MFALevel> mfaLevel;
Expand Down Expand Up @@ -208,6 +210,30 @@ public GuildField<VoiceChannel> getAfkChannelField()
return afkChannel;
}

/**
* An {@link net.dv8tion.jda.core.managers.fields.GuildField GuildField}
* for the <b><u>system {@link net.dv8tion.jda.core.entities.TextChannel TextChannel}</u></b> of the selected {@link net.dv8tion.jda.core.entities.Guild Guild}.
* <br>To reset the channel of a Guild provide {@code null} to {@link net.dv8tion.jda.core.managers.fields.Field#setValue(Object) setValue(VoiceChannel)}.
*
* <p>To set the value use {@link net.dv8tion.jda.core.managers.fields.Field#setValue(Object) setValue(TextChannel)}
* on the returned {@link net.dv8tion.jda.core.managers.fields.GuildField GuildField} instance.
*
* <p>A guild system channel <b>must</b> be from this Guild!
* <br>Otherwise {@link net.dv8tion.jda.core.managers.fields.Field#setValue(Object) Field.setValue(...)} will
* throw an {@link IllegalArgumentException IllegalArgumentException}.
*
* @throws net.dv8tion.jda.core.exceptions.GuildUnavailableException
* If the Guild is temporarily not {@link net.dv8tion.jda.core.entities.Guild#isAvailable() available}
*
* @return {@link net.dv8tion.jda.core.managers.fields.GuildField GuildField} - Type: {@link net.dv8tion.jda.core.entities.TextChannel TextChannel}
*/
public GuildField<TextChannel> getSystemChannelField()
{
checkAvailable();

return systemChannel;
}

/**
* An {@link net.dv8tion.jda.core.managers.fields.GuildField GuildField}
* for the <b><u>AFK {@link net.dv8tion.jda.core.entities.Guild.Timeout Timeout}</u></b> of the selected {@link net.dv8tion.jda.core.entities.Guild Guild}.
Expand Down Expand Up @@ -338,6 +364,7 @@ public void reset()
this.icon.reset();
this.splash.reset();
this.afkChannel.reset();
this.systemChannel.reset();
this.verificationLevel.reset();
this.defaultNotificationLevel.reset();
this.mfaLevel.reset();
Expand Down Expand Up @@ -393,6 +420,8 @@ public AuditableRestAction<Void> update()
body.put("splash", splash.getValue() == null ? JSONObject.NULL : splash.getValue().getEncoding());
if (afkChannel.shouldUpdate())
body.put("afk_channel_id", afkChannel.getValue() == null ? JSONObject.NULL : afkChannel.getValue().getId());
if (systemChannel.shouldUpdate())
body.put("system_channel_id", systemChannel.getValue() == null ? JSONObject.NULL : systemChannel.getValue().getId());
if (verificationLevel.shouldUpdate())
body.put("verification_level", verificationLevel.getValue().getKey());
if (defaultNotificationLevel.shouldUpdate())
Expand Down Expand Up @@ -425,6 +454,7 @@ protected boolean needToUpdate()
|| icon.shouldUpdate()
|| splash.shouldUpdate()
|| afkChannel.shouldUpdate()
|| systemChannel.shouldUpdate()
|| verificationLevel.shouldUpdate()
|| defaultNotificationLevel.shouldUpdate()
|| mfaLevel.shouldUpdate()
Expand Down Expand Up @@ -522,6 +552,16 @@ public void checkValue(VoiceChannel value)
}
};

this.systemChannel = new GuildField<TextChannel>(this, guild::getSystemChannel)
{
@Override
public void checkValue(TextChannel value)
{
if (value != null && !guild.equals(value.getGuild()))
throw new IllegalArgumentException("Provided system channel is not from this Guild!");
}
};

this.verificationLevel = new GuildField<Guild.VerificationLevel>(this, guild::getVerificationLevel)
{
@Override
Expand Down

0 comments on commit c53c4c7

Please sign in to comment.