Skip to content
This repository was archived by the owner on Jun 9, 2024. It is now read-only.

feature: Guild scheduled events (closes #633) #640

Open
wants to merge 1 commit into
base: mistress
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
201 changes: 201 additions & 0 deletions src/main/java/com/mewna/catnip/entity/guild/ScheduledEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/*
* Copyright (c) 2021 amy, All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.mewna.catnip.entity.guild;

import com.mewna.catnip.entity.partials.GuildEntity;
import com.mewna.catnip.entity.partials.Snowflake;
import com.mewna.catnip.entity.partials.Timestamped;
import com.mewna.catnip.entity.user.User;
import lombok.Getter;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.time.OffsetDateTime;
import java.util.List;

/**
* @author amy
* @since 11/25/21.
*/
public interface ScheduledEvent extends Snowflake, GuildEntity, Timestamped {
@Nullable
@CheckReturnValue
default String channelId() {
return Long.toUnsignedString(channelIdAsLong());
}

@CheckReturnValue
long channelIdAsLong();

@Nullable
@CheckReturnValue
default String creatorId() {
return Long.toUnsignedString(creatorIdAsLong());
}

@CheckReturnValue
long creatorIdAsLong();

@CheckReturnValue
String name();

@Nullable
@CheckReturnValue
String description();

@Nonnull
@CheckReturnValue
default OffsetDateTime scheduledStartTime() {
return parseTimestamp(scheduledStartTimeRaw());
}

@Nonnull
@CheckReturnValue
String scheduledStartTimeRaw();

@Nullable
@CheckReturnValue
default OffsetDateTime scheduledEndTime() {
return parseTimestamp(scheduledEndTimeRaw());
}

@Nullable
@CheckReturnValue
String scheduledEndTimeRaw();

@Nonnull
@CheckReturnValue
PrivacyLevel privacyLevel();

@Nonnull
@CheckReturnValue
EventStatus status();

@Nonnull
@CheckReturnValue
EventEntityType entityType();

@Nullable
@CheckReturnValue
String entityId();

@Nullable
@CheckReturnValue
EntityMetadata entityMetadata();

@Nullable
@CheckReturnValue
User creator();

@CheckReturnValue
int userCount();

enum PrivacyLevel {
PUBLIC(1),
GUILD_ONLY(2),
;

@Getter
private final int key;

PrivacyLevel(final int key) {
this.key = key;
}

@Nonnull
public static PrivacyLevel byKey(final int key) {
for(final PrivacyLevel level : values()) {
if(level.key == key) {
return level;
}
}
throw new IllegalArgumentException("No privacy level for key " + key);
}
}

enum EventEntityType {
NONE(0),
STAGE_INSTANCE(1),
VOICE(2),
EXTERNAL(3),
;

@Getter
private final int key;

EventEntityType(final int key) {
this.key = key;
}

@Nonnull
public static EventEntityType byKey(final int key) {
for(final EventEntityType level : values()) {
if(level.key == key) {
return level;
}
}
throw new IllegalArgumentException("No event entity type for key " + key);
}
}

enum EventStatus {
SCHEDULED(1),
ACTIVE(2),
COMPLETED(3),
CANCELED(4),
;

@Getter
private final int key;

EventStatus(final int key) {
this.key = key;
}

@Nonnull
public static EventStatus byKey(final int key) {
for(final EventStatus level : values()) {
if(level.key == key) {
return level;
}
}
throw new IllegalArgumentException("No event status for key " + key);
}
}

interface EntityMetadata {
@Nullable
@CheckReturnValue
List<String> speakerIds();

@Nullable
@CheckReturnValue
String location();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,20 @@ public enum ActionType {
MESSAGE_UNPIN(75),
INTEGRATION_CREATE(80),
INTEGRATION_UPDATE(81),
INTEGRATION_DELETE(82);
INTEGRATION_DELETE(82),
STAGE_INSTANCE_CREATE(83),
STAGE_INSTANCE_UPDATE(84),
STAGE_INSTANCE_DELETE(85),
STICKER_CREATE(90),
STICKER_UPDATE(91),
STICKER_DELETE(92),
GUILD_SCHEDULED_EVENT_CREATE(100),
GUILD_SCHEDULED_EVENT_UPDATE(101),
GUILD_SCHEDULED_EVENT_DELETE(102),
THREAD_CREATE(110),
THREAD_UPDATE(111),
THREAD_DELETE(112),
;

@Getter
private final int value;
Expand Down
38 changes: 37 additions & 1 deletion src/main/java/com/mewna/catnip/entity/impl/EntityBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
import com.mewna.catnip.entity.guild.Invite.InviteGuild;
import com.mewna.catnip.entity.guild.Invite.Inviter;
import com.mewna.catnip.entity.guild.PermissionOverride.OverrideType;
import com.mewna.catnip.entity.guild.ScheduledEvent.EntityMetadata;
import com.mewna.catnip.entity.guild.ScheduledEvent.EventEntityType;
import com.mewna.catnip.entity.guild.ScheduledEvent.EventStatus;
import com.mewna.catnip.entity.guild.ScheduledEvent.PrivacyLevel;
import com.mewna.catnip.entity.guild.audit.*;
import com.mewna.catnip.entity.impl.channel.*;
import com.mewna.catnip.entity.impl.channel.ThreadChannelImpl.ThreadMemberImpl;
Expand All @@ -48,6 +52,7 @@
import com.mewna.catnip.entity.impl.guild.InviteImpl.InviteChannelImpl;
import com.mewna.catnip.entity.impl.guild.InviteImpl.InviteGuildImpl;
import com.mewna.catnip.entity.impl.guild.InviteImpl.InviterImpl;
import com.mewna.catnip.entity.impl.guild.ScheduledEventImpl.EntityMetadataImpl;
import com.mewna.catnip.entity.impl.guild.audit.*;
import com.mewna.catnip.entity.impl.interaction.CustomIdInteractionDataImpl;
import com.mewna.catnip.entity.impl.interaction.InteractionMemberImpl;
Expand Down Expand Up @@ -102,7 +107,7 @@
* @author natanbc
* @since 9/2/18.
*/
@SuppressWarnings({"WeakerAccess", "OverlyCoupledClass", "DuplicatedCode"})
@SuppressWarnings({"WeakerAccess", "OverlyCoupledClass", "DuplicatedCode", "ClassCanBeRecord"})
public final class EntityBuilder {
private final Catnip catnip;

Expand Down Expand Up @@ -1795,4 +1800,35 @@ public ThreadMembersUpdate createThreadMembersUpdate(@Nonnull final JsonObject d
.removedMembers(toStringList(data.getArray("removed_members", new JsonArray())))
.build());
}

@Nonnull
@CheckReturnValue
public ScheduledEvent createScheduledEvent(@Nonnull final JsonObject data) {
return delegate(ScheduledEvent.class, ScheduledEventImpl.builder()
.idAsLong(Long.parseUnsignedLong(data.getString("id")))
.guildIdAsLong(Long.parseUnsignedLong(data.getString("guild_id")))
.channelIdAsLong(Long.parseUnsignedLong(data.getString("channel_id", "0")))
.creatorIdAsLong(Long.parseUnsignedLong(data.getString("creator_id", "0")))
.name(data.getString("name"))
.description(data.getString("description"))
.scheduledStartTimeRaw(data.getString("scheduled_start_time"))
.scheduledEndTimeRaw(data.getString("scheduled_end_time"))
.privacyLevel(PrivacyLevel.byKey(data.getInt("privacy_level")))
.status(EventStatus.byKey(data.getInt("status")))
.entityType(EventEntityType.byKey(data.getInt("entity_type")))
.entityId(data.getString("entity_id"))
.entityMetadata(createScheduledEventEntityMetadata(data.getObject("entity_metadata")))
.creator(data.has("creator") ? createUser(data.getObject("creator")) : null)
.userCount(data.getInt("user_count", 0))
.build());
}

@Nonnull
@CheckReturnValue
public EntityMetadata createScheduledEventEntityMetadata(@Nonnull final JsonObject data) {
return delegate(EntityMetadata.class, EntityMetadataImpl.builder()
.speakerIds(data.has("speaker_ids") ? toStringList(data.getArray("speaker_ids")) : List.of())
.location(data.getString("location"))
.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
package com.mewna.catnip.entity.impl.guild;

import com.mewna.catnip.Catnip;
import com.mewna.catnip.entity.RequiresCatnip;
import com.mewna.catnip.entity.partials.Timestamped;
import com.mewna.catnip.entity.guild.Guild;
import com.mewna.catnip.entity.guild.GuildFeature;
import com.mewna.catnip.entity.util.ImageOptions;
Expand All @@ -55,7 +53,7 @@
@Accessors(fluent = true)
@NoArgsConstructor
@AllArgsConstructor
public class GuildImpl implements Guild, RequiresCatnip, Timestamped {
public class GuildImpl implements Guild {
private transient Catnip catnip;

private long idAsLong;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2021 amy, All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.mewna.catnip.entity.impl.guild;

import com.mewna.catnip.Catnip;
import com.mewna.catnip.entity.RequiresCatnip;
import com.mewna.catnip.entity.guild.ScheduledEvent;
import com.mewna.catnip.entity.user.User;
import lombok.*;
import lombok.experimental.Accessors;

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

/**
* @author amy
* @since 11/25/21.
*/
@Getter
@Setter
@Builder
@Accessors(fluent = true)
@NoArgsConstructor
@AllArgsConstructor
public class ScheduledEventImpl implements ScheduledEvent, RequiresCatnip {
private transient Catnip catnip;

private long idAsLong;
private long guildIdAsLong;
private long channelIdAsLong;
private long creatorIdAsLong;
private String name;
private String description;
private String scheduledStartTimeRaw;
private String scheduledEndTimeRaw;
private PrivacyLevel privacyLevel;
private EventStatus status;
private EventEntityType entityType;
private String entityId;
private EntityMetadata entityMetadata;
private User creator;
private int userCount;

@Override
public void catnip(@Nonnull final Catnip catnip) {
this.catnip = catnip;
}

@Getter
@Setter
@Builder
@Accessors(fluent = true)
@NoArgsConstructor
@AllArgsConstructor
public static class EntityMetadataImpl implements EntityMetadata {
private List<String> speakerIds;
private String location;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/mewna/catnip/entity/util/Permission.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public enum Permission {
MANAGE_EMOJIS_AND_STICKERS(1L << 30, false, "Manage Emojis & Stickers"),
USE_APPLICATION_COMMANDS(1L << 31, true, "Use Application Commands"),
REQUEST_TO_SPEAK(1L << 32L, true, "Request To Speak"),
MANAGE_EVENTS(1L << 33, false, "Manage Guild Events"), // This permission is still an experiment, but will come soon
MANAGE_EVENTS(1L << 33, false, "Manage Guild Events"),
MANAGE_THREADS(1L << 34, true, "Manage Threads"),
/**
* Replaced by {@link #CREATE_PUBLIC_THREADS}. See https://github.com/discord/discord-api-docs/pull/3672
Expand Down
Loading