Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add guild welcome screens #2264

Merged
merged 32 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
c01b74c
Steal #1989
freya022 Sep 27, 2022
f7084bc
Add Guild object to GuildWelcomeScreen and GuildWelcomeScreen.Channel…
freya022 Sep 27, 2022
11b6215
Use EmojiUnion
freya022 Sep 27, 2022
4841160
Return null channel on null guild
freya022 Sep 27, 2022
2b957e1
Docs, give emoji from guild's cache if available
freya022 Sep 28, 2022
8bb4afd
Add welcome screen manager
freya022 Sep 29, 2022
31c41aa
Some docs
freya022 Sep 30, 2022
f04737d
More docs
freya022 Sep 30, 2022
98dd7e9
Use constants
freya022 Sep 30, 2022
c2e3004
Remove addWelcomeChannel and removeWelcomeChannel as their name do no…
freya022 Sep 30, 2022
fc90ef6
Finish docs
freya022 Sep 30, 2022
e26e40a
Fix docs
freya022 Sep 30, 2022
718c160
Apply suggestions from code review
freya022 Oct 19, 2022
bd03e1f
Merge branch 'master' into feature/welcome-screen
freya022 Oct 19, 2022
8def12e
Update src/main/java/net/dv8tion/jda/api/entities/GuildWelcomeScreen.…
freya022 Oct 19, 2022
19bc4cf
Null checks
freya022 Oct 19, 2022
f798dfb
Check 5 channels max
freya022 Oct 19, 2022
9b5d70b
`Boolean` be gone
freya022 Oct 19, 2022
ba6d8ef
Preallocate array
freya022 Oct 19, 2022
8f547a6
Use createEmoji
freya022 Oct 19, 2022
3abea81
Typo
freya022 Oct 19, 2022
014165d
Consistency
freya022 Oct 19, 2022
bc05057
Nuke GuildWelcomeScreenManager#clearDescription
freya022 Oct 19, 2022
71fafd5
Separate GuildWelcomeScreen impl
freya022 Oct 19, 2022
ab14823
Separate GuildWelcomeScreen.Channel impl
freya022 Oct 19, 2022
65bdcd5
Fix example
freya022 Oct 20, 2022
b8938ea
Add GuildWelcomeScreen#getManager
freya022 Oct 20, 2022
b406c79
Specify null welcome screen if invite came from an event
freya022 Oct 20, 2022
511d69a
Update src/main/java/net/dv8tion/jda/api/managers/GuildWelcomeScreenM…
freya022 Oct 20, 2022
f6b8cd6
Fix compile
freya022 Oct 20, 2022
2523e5e
Merge branch 'master' into feature/welcome-screen
freya022 Oct 23, 2022
6b1ab93
Merge branch 'master' into feature/welcome-screen
freya022 Nov 10, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/main/java/net/dv8tion/jda/api/entities/Guild.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import net.dv8tion.jda.api.managers.AudioManager;
import net.dv8tion.jda.api.managers.GuildManager;
import net.dv8tion.jda.api.managers.GuildStickerManager;
import net.dv8tion.jda.api.managers.GuildWelcomeScreenManager;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.requests.restaction.*;
Expand Down Expand Up @@ -2259,6 +2260,26 @@ default RestAction<RichCustomEmoji> retrieveEmoji(@Nonnull CustomEmoji emoji)
@CheckReturnValue
RestAction<List<Webhook>> retrieveWebhooks();

/**
* Retrieves the {@link GuildWelcomeScreen welcome screen} for this Guild.
* <br>The welcome screen is shown to all members after joining the Guild.
*
* <p>Possible {@link net.dv8tion.jda.api.requests.ErrorResponse ErrorResponses} include:
* <ul>
* <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#UNKNOWN_GUILD_WELCOME_SCREEN Unknown Guild Welcome Screen}
* <br>The guild has no welcome screen</li>
* <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#MISSING_PERMISSIONS Missing Permissions}
* <br>The guild's welcome screen is disabled
* and the currently logged in account doesn't have the {@link net.dv8tion.jda.api.Permission#MANAGE_SERVER MANAGE_SERVER} permission</li>
* </ul>
*
* @return {@link RestAction} - Type: {@link GuildWelcomeScreen}
* <br>The welcome screen for this Guild.
*/
@Nonnull
@CheckReturnValue
RestAction<GuildWelcomeScreen> retrieveWelcomeScreen();

/**
* A list containing the {@link net.dv8tion.jda.api.entities.GuildVoiceState GuildVoiceState} of every {@link net.dv8tion.jda.api.entities.Member Member}
* in this {@link net.dv8tion.jda.api.entities.Guild Guild}.
Expand Down Expand Up @@ -4666,6 +4687,20 @@ default RoleOrderAction modifyRolePositions()
@CheckReturnValue
RoleOrderAction modifyRolePositions(boolean useAscendingOrder);

/**
* The {@link GuildWelcomeScreenManager Manager} for this guild's welcome screen, used to modify
* properties of the welcome screen like if the welcome screen is enabled, the description and welcome channels.
* <br>You modify multiple fields in one request by chaining setters before calling {@link net.dv8tion.jda.api.requests.RestAction#queue() RestAction.queue()}.
*
* @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
* If the currently logged in account does not have {@link net.dv8tion.jda.api.Permission#MANAGE_SERVER Permission.MANAGE_SERVER}
*
* @return The GuildWelcomeScreenManager for this guild's welcome screen
*/
@Nonnull
@CheckReturnValue
GuildWelcomeScreenManager modifyWelcomeScreen();

//////////////////////////

/**
Expand Down
244 changes: 244 additions & 0 deletions src/main/java/net/dv8tion/jda/api/entities/GuildWelcomeScreen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
/*
* 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.entities.channel.middleman.GuildChannel;
import net.dv8tion.jda.api.entities.channel.middleman.StandardGuildChannel;
import net.dv8tion.jda.api.entities.emoji.CustomEmoji;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.entities.emoji.EmojiUnion;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.api.utils.data.SerializableData;
import net.dv8tion.jda.internal.utils.Checks;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;

/**
* The welcome screen of a {@link Guild}.
* This welcome screen will be shown to all members after joining the Guild.
*
* @see Guild#retrieveWelcomeScreen()
* @see Invite.Guild#getWelcomeScreen()
*/
public class GuildWelcomeScreen
{
public static final int MAX_DESCRIPTION_LENGTH = 140;
freya022 marked this conversation as resolved.
Show resolved Hide resolved

private final Guild guild;
private final String description;
private final List<GuildWelcomeScreen.Channel> channels;

public GuildWelcomeScreen(Guild guild, String description, List<GuildWelcomeScreen.Channel> channels)
freya022 marked this conversation as resolved.
Show resolved Hide resolved
{
this.guild = guild;
this.description = description;
this.channels = channels;
}

/**
* The {@link Guild Guild}, or {@code null} if this welcome screen came from an {@link Invite}
*
* @return The Guild, or {@code null}
*/
@Nullable
public Guild getGuild()
{
return guild;
}

/**
* The server description shown in the Welcome screen.
freya022 marked this conversation as resolved.
Show resolved Hide resolved
* <br>This will be {@code null} if the welcome screen has no description.
*
* @return The server description shown in the Welcome screen or {@code null}
*/
@Nullable
public String getDescription()
{
return description;
}

/**
* The channels shown in the Welcome screen.
*
* @return Possibly-empty, unmodifiable list of the channels shown in the Welcome screen
*/
@Nonnull
public List<GuildWelcomeScreen.Channel> getChannels()
{
return channels;
}

/**
* POJO for the recommended channels information provided by a Welcome screen.
* <br>Recommended channels are shown in the Welcome screen after joining a server.
*
* @see GuildWelcomeScreen#getChannels()
*/
public static class Channel implements ISnowflake, SerializableData
{
public static final int MAX_DESCRIPTION_LENGTH = 42;
freya022 marked this conversation as resolved.
Show resolved Hide resolved

private final Guild guild;
private final long id;
private final String description;
freya022 marked this conversation as resolved.
Show resolved Hide resolved
private final EmojiUnion emoji;

public Channel(Guild guild, long id, String description, EmojiUnion emoji)
freya022 marked this conversation as resolved.
Show resolved Hide resolved
{
this.guild = guild;
this.id = id;
this.description = description;
this.emoji = emoji;
}

/**
* Constructs a new welcome channel.
*
* @param channel
* The Discord channel to be presented to the user
* @param description
* The description of the channel, must not be longer than {@value MAX_DESCRIPTION_LENGTH}
freya022 marked this conversation as resolved.
Show resolved Hide resolved
*
* @throws IllegalArgumentException
* <ul>
* <li>If the channel is null</li>
* <li>If the description is null, blank, or longer than {@value MAX_DESCRIPTION_LENGTH}</li>
freya022 marked this conversation as resolved.
Show resolved Hide resolved
* </ul>
*
* @return The new welcome channel
*/
@Nonnull
public static Channel of(@Nonnull StandardGuildChannel channel, @Nonnull String description)
{
return of(channel, description, null);
}

/**
* Constructs a new welcome channel.
*
* @param channel
* The Discord channel to be presented the user
* @param description
* The description of the channel, must not be longer than {@value MAX_DESCRIPTION_LENGTH}
freya022 marked this conversation as resolved.
Show resolved Hide resolved
* @param emoji
* The emoji to show beside the channel
*
* @throws IllegalArgumentException
* <ul>
* <li>If the channel is null</li>
* <li>If the description is null, blank, or longer than {@value MAX_DESCRIPTION_LENGTH}</li>
freya022 marked this conversation as resolved.
Show resolved Hide resolved
* </ul>
*
* @return The new welcome channel
*/
@Nonnull
public static Channel of(@Nonnull StandardGuildChannel channel, @Nonnull String description, @Nullable Emoji emoji)
{
Checks.notBlank(description, "Description");
Checks.notLonger(description, MAX_DESCRIPTION_LENGTH, "Description");

return new Channel(channel.getGuild(), channel.getIdLong(), description, (EmojiUnion) emoji);
freya022 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* The {@link Guild Guild}, or {@code null} if this welcome channel came from an {@link Invite}
*
* @return The Guild, or {@code null}
*/
@Nullable
public Guild getGuild()
{
return guild;
}

/**
* The id of this recommended channel.
*
* @return The id of this recommended channel
*/
@Override
public long getIdLong()
{
return id;
}

/**
* Returns the {@link GuildChannel} that is linked to this recommended channel.
* <br>This will be {@code null} if the linked channel was deleted, or if the welcome screen comes from an {@link Invite.Guild invite guild}.
*
* @return The {@link GuildChannel} that is linked to this recommended channel or {@code null}
*/
@Nullable
public GuildChannel getChannel()
{
if (guild == null)
return null;

return guild.getGuildChannelById(id);
}

/**
* The description of this recommended channel shown in the Welcome screen.
*
* @return The description of this recommended channel
*/
@Nonnull
public String getDescription()
{
return description;
}

/**
* The emoji that is used for this recommended channel.
* <br>This will return {@code null} if no emoji was set
*
* <p>The emoji will always be from this guild, if not a unicode emoji
* <br><b>{@link CustomEmoji#isAnimated()} will always return {@code false} if:</b>
* <ul>
* <li>This welcome screen came from an {@link Invite.Guild invite's guild}</li>
* <li>{@link net.dv8tion.jda.api.utils.cache.CacheFlag#EMOJI CacheFlag.EMOJI} is disabled</li>
* </ul>
*
* @return The emoji that is used for this recommended channel or {@code null}
*/
@Nullable
public EmojiUnion getEmoji()
{
return emoji;
}

@Nonnull
@Override
public DataObject toData()
{
final DataObject data = DataObject.empty();
data.put("channel_id", id);
data.put("description", description);
if (emoji != null)
{
if (emoji.getType() == Emoji.Type.CUSTOM)
data.put("emoji_id", ((CustomEmoji) emoji).getId());
data.put("emoji_name", emoji.getName());
}

return data;
}
}
}
Loading