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

feat(channels): media channels #195

Merged
merged 1 commit into from
Aug 2, 2023
Merged
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
39 changes: 39 additions & 0 deletions src/main/java/com/seailz/discordjar/DiscordJar.java
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,25 @@ public ForumChannel getForumChannelById(String id) {
return ForumChannel.decompile(raw, this);
}

/**
* Returns info about a {@link MediaChannel}
*
* @param id The id of the channel
* @return A {@link MediaChannel} object
*/
@Nullable
public MediaChannel getMediaChannelById(String id) {
Checker.isSnowflake(id, "Given id is not a snowflake");
JSONObject raw = null;
try {
raw = getChannelCache().getById(id).raw();
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
if (e.getHttpCode() == 404) return null;
throw new DiscordRequest.DiscordAPIErrorException(e);
}
return MediaChannel.decompile(raw, this);
}

/**
* Returns info about a {@link Category}
*
Expand All @@ -607,6 +626,26 @@ public Category getCategoryById(String id) {
return Category.decompile(raw, this);
}

/**
* Returns info about a {@link VoiceChannel}
*
* @param id The id of the channel
* @return A {@link VoiceChannel} object
*/
@Nullable
public VoiceChannel getVoiceChannelById(String id) {
Checker.isSnowflake(id, "Given id is not a snowflake");
JSONObject raw = null;
try {
raw = getChannelCache().getById(id).raw();
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
if (e.getHttpCode() == 404) return null;
throw new DiscordRequest.DiscordAPIErrorException(e);
}
return VoiceChannel.decompile(raw, this);
}


/**
* Returns info about a {@link Guild}
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.seailz.discordjar.model.channel;

import com.seailz.discordjar.DiscordJar;
import com.seailz.discordjar.model.channel.forum.DefaultSortOrder;
import com.seailz.discordjar.model.channel.forum.ForumTag;
import com.seailz.discordjar.model.channel.internal.ForumChannelImpl;
import com.seailz.discordjar.model.channel.internal.MediaChannelImpl;
import com.seailz.discordjar.model.channel.utils.ChannelType;
import com.seailz.discordjar.model.guild.Guild;
import com.seailz.discordjar.model.permission.PermissionOverwrite;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
* Represents a media channel.
* These are very similar to {@link ForumChannel}s, with very few differences.
*
* @see ForumChannel
*/
public interface MediaChannel extends ForumChannel {
static MediaChannel decompile(JSONObject obj, DiscordJar discordJar) {
String id = obj.getString("id");
String name = obj.getString("name");
int position = obj.getInt("position");

List<PermissionOverwrite> permissionOverwrites = new ArrayList<>();
for (Object overwrite : obj.getJSONArray("permission_overwrites")) {
permissionOverwrites.add(PermissionOverwrite.decompile((JSONObject) overwrite));
}

boolean nsfw = obj.getBoolean("nsfw");
String postGuidelines = obj.has("topic") && !obj.isNull("topic") ? obj.getString("topic") : null;
String lastThreadId = obj.has("last_thread_id") && !obj.isNull("last_thread_id") ? obj.getString("last_thread_id") : null;
DefaultSortOrder defaultSortOrder = obj.has("default_sort_order") && !obj.isNull("default_sort_order") ? DefaultSortOrder.fromCode(obj.getInt("default_sort_order")) : DefaultSortOrder.UNKNOWN;

List<ForumTag> tags = new ArrayList<>();
if (obj.has("available_tags") && !obj.isNull("available_tags")) {
for (Object tag : obj.getJSONArray("available_tags")) {
tags.add(ForumTag.decompile((JSONObject) tag));
}
}

Guild guild = obj.has("guild_id") && !obj.isNull("guild_id") ? discordJar.getGuildById(obj.getString("guild_id")) : null;
DefaultForumLayout dfl = obj.has("default_forum_layout") && !obj.isNull("default_forum_layout") ? DefaultForumLayout.fromCode(obj.getInt("default_forum_layout")) : DefaultForumLayout.UNKNOWN;
return new MediaChannelImpl(id, ChannelType.GUILD_FORUM, name, guild, position, permissionOverwrites, nsfw, postGuidelines, tags, defaultSortOrder, lastThreadId, obj, discordJar, dfl);
}

@Override
default @NotNull ChannelType type() {
return ChannelType.GUILD_MEDIA;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.seailz.discordjar.model.channel.internal;

import com.seailz.discordjar.DiscordJar;
import com.seailz.discordjar.model.channel.MediaChannel;
import com.seailz.discordjar.model.channel.forum.DefaultSortOrder;
import com.seailz.discordjar.model.channel.forum.ForumTag;
import com.seailz.discordjar.model.channel.utils.ChannelType;
import com.seailz.discordjar.model.guild.Guild;
import com.seailz.discordjar.model.permission.PermissionOverwrite;
import org.json.JSONObject;

import java.util.List;

public class MediaChannelImpl extends ForumChannelImpl implements MediaChannel {
public MediaChannelImpl(String id, ChannelType type, String name, Guild guild, int position, List<PermissionOverwrite> permissionOverwrites, boolean nsfw, String postGuidelines, List<ForumTag> tags, DefaultSortOrder defaultSortOrder, String lastThreadId, JSONObject raw, DiscordJar discordJar, DefaultForumLayout defaultForumLayout) {
super(id, type, name, guild, position, permissionOverwrites, nsfw, postGuidelines, tags, defaultSortOrder, lastThreadId, raw, discordJar, defaultForumLayout);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public enum ChannelType {
PRIVATE_THREAD(12, "Private Thread", true, true),
GUILD_STAGE_VOICE(13, "Stage Channel", false, true),
GUILD_DIRECTORY(14, "Student Hub Directory", false, true),
GUILD_FORUM(15, " Forum", false, true),
GUILD_FORUM(15, "Forum", false, true),
GUILD_MEDIA(16, "Media Channel", false, true),
;
private final int code;
private final String name;
Expand Down