Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Commit

Permalink
added get guilds/request guilds and request users
Browse files Browse the repository at this point in the history
  • Loading branch information
RealYusufIsmail committed Mar 14, 2023
1 parent 47c4405 commit 9fa255f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 28 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
kotlin.code.style=official
version= 1.0.5-SNAPSHOT
version= 1.0.5

jvmVersion = 1.8.10
pluginAllOpenVersion = 1.8.10
Expand Down
67 changes: 44 additions & 23 deletions src/main/kotlin/io/github/ydwk/yde/YDE.kt
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ interface YDE {
*/
fun requestUser(id: String): CompletableFuture<User> = requestUser(id.toLong())

/**
* Requests all the users the bot can see.
*
* @return The [CompletableFuture] object.
*/
fun requestUsers(): CompletableFuture<List<User>>

/**
* Requests a guild using its id.
*
Expand All @@ -167,6 +174,13 @@ interface YDE {
*/
fun requestGuild(guildId: String): CompletableFuture<Guild> = requestGuild(guildId.toLong())

/**
* Requests all the guilds the bot is in.
*
* @return The [CompletableFuture] object.
*/
fun requestGuilds(): CompletableFuture<List<Guild>>

/**
* Gets a guild by its id.
*
Expand All @@ -183,6 +197,13 @@ interface YDE {
*/
fun getGuildById(id: String): Guild?

/**
* Gets all the guilds the bot is in.
*
* @return A list of [Guild] objects.
*/
fun getGuilds(): List<Guild>

/**
* Gets a channel by its id.
*
Expand Down Expand Up @@ -301,29 +322,6 @@ interface YDE {
fun requestGuildChannels(guildId: String): CompletableFuture<List<GuildChannel>> =
requestGuildChannels(guildId.toLong())

/**
* The entity builder.
*
* @return The [EntityBuilder] object.
*/
@get:Incubating val entityBuilder: EntityBuilder

/** Adds or removes slash commands */
val slashBuilder: ISlashCommandBuilder

/** Adds or removes user commands. */
val userCommandBuilder: IUserCommandBuilder

/** Adds or removes message commands. */
val messageCommandBuilder: IMessageCommandBuilder

/**
* Used to build embeds.
*
* @return The [EmbedBuilder] object.
*/
val embedBuilder: EmbedBuilder

/** Sets the guild ids for guild commands */
fun setGuildIds(vararg guildIds: String)

Expand Down Expand Up @@ -385,6 +383,29 @@ interface YDE {
*/
val bot: Bot?

/**
* The entity builder.
*
* @return The [EntityBuilder] object.
*/
@get:Incubating val entityBuilder: EntityBuilder

/** Adds or removes slash commands */
val slashBuilder: ISlashCommandBuilder

/** Adds or removes user commands. */
val userCommandBuilder: IUserCommandBuilder

/** Adds or removes message commands. */
val messageCommandBuilder: IMessageCommandBuilder

/**
* Used to build embeds.
*
* @return The [EmbedBuilder] object.
*/
val embedBuilder: EmbedBuilder

/**
* Overrides the custom to string method.
*
Expand Down
26 changes: 23 additions & 3 deletions src/main/kotlin/io/github/ydwk/yde/impl/YDEImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ open class YDEImpl(
open var applicationId: String? = null,
protected open val client: OkHttpClient,
protected open var guildIdList: MutableList<String> = mutableListOf(),
open override val githubRepositoryUrl: String,
open override val wrapperVersion: String,
override val githubRepositoryUrl: String,
override val wrapperVersion: String,
) : YDE {
val logger: Logger = LoggerFactory.getLogger(YDEImpl::class.java)

protected val allowedCache: MutableSet<CacheIds> = mutableSetOf()
private val allowedCache: MutableSet<CacheIds> = mutableSetOf()
val cache: Cache = PerpetualCache(allowedCache)
val memberCache: MemberCache = MemberCacheImpl(allowedCache)

Expand Down Expand Up @@ -128,6 +128,14 @@ open class YDEImpl(
}
}

override fun requestUsers(): CompletableFuture<List<User>> {
return this.restApiManager.get(EndPoint.UserEndpoint.GET_USERS).execute { it ->
val jsonBody = it.jsonBody
jsonBody?.map { UserImpl(it, it["id"].asLong(), this) }
?: throw IllegalStateException("json body is null")
}
}

override fun requestGuild(guildId: Long): CompletableFuture<Guild> {
return this.restApiManager
.get(EndPoint.GuildEndpoint.GET_GUILD, guildId.toString())
Expand All @@ -141,10 +149,22 @@ open class YDEImpl(
}
}

override fun requestGuilds(): CompletableFuture<List<Guild>> {
return this.restApiManager.get(EndPoint.GuildEndpoint.GET_GUILDS).execute { it ->
val jsonBody = it.jsonBody
jsonBody?.map { GuildImpl(this, it, it["id"].asLong()) }
?: throw IllegalStateException("json body is null")
}
}

override fun getGuildById(id: String): Guild? {
return cache[id, CacheIds.GUILD] as Guild?
}

override fun getGuilds(): List<Guild> {
return cache.values(CacheIds.GUILD).map { it as Guild }
}

override fun getChannelById(id: Long): Channel? {
return cache[id.toString(), CacheIds.CHANNEL] as Channel?
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/io/github/ydwk/yde/rest/EndPoint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ open class EndPoint {
GET_AUDIT_LOGS("/guilds/%s/audit-logs"),
GET_MEMBERS("/guilds/%s/members"),
GET_GUILD("/guilds/%s"),
GET_GUILDS("/guilds"),
GET_GUILD_CHANNELS("/guilds/%s/channels"),
GET_MEMBER("/guilds/%s/members/%s"),
CREATE_GUILD("/guilds"),
Expand All @@ -89,7 +90,8 @@ open class EndPoint {

enum class UserEndpoint(private val endPoint: String) : IEnumEndpoint {
CREATE_DM("/users/@me/channels"),
GET_USER("/users/%s");
GET_USER("/users/%s"),
GET_USERS("/users");

override fun getEndpoint(): String {
return endPoint
Expand Down

0 comments on commit 9fa255f

Please sign in to comment.