All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.entity.Team.kt Maven / Gradle / Ivy

There is a newer version: 0.15.0
Show newest version
package dev.kord.core.entity

import dev.kord.common.entity.Snowflake
import dev.kord.common.entity.TeamMembershipState
import dev.kord.common.exception.RequestException
import dev.kord.core.Kord
import dev.kord.core.cache.data.TeamData
import dev.kord.core.cache.data.TeamMemberData
import dev.kord.core.exception.EntityNotFoundException
import dev.kord.core.supplier.EntitySupplier
import dev.kord.core.supplier.EntitySupplyStrategy

/**
 * A Discord [developer team](https://discord.com/developers/docs/topics/teams) which can own applications.
 */
public class Team(
    public val data: TeamData,
    override val kord: Kord,
    override val supplier: EntitySupplier = kord.defaultSupplier,
) : KordEntity, Strategizable {
    /**
     * The unique ID of this team.
     */
    override val id: Snowflake
        get() = data.id

    /**
     * The hash of this team's icon.
     */
    @Deprecated("Binary compatibility", level = DeprecationLevel.HIDDEN)
    public fun getIcon(): String? = data.icon

    /** The hash of this team's icon. */
    public val iconHash: String? get() = data.icon

    public val icon: Asset? get() = iconHash?.let { Asset.teamIcon(id, it, kord) }

    /**
     * A collection of all members of this team.
     */
    public val members: List
        get() = data.members.map { TeamMember(it, kord) }

    /**
     * The ID of the user that owns the team.
     */
    public val ownerUserId: Snowflake
        get() = data.ownerUserId


    /**
     * Requests to get the team owner through the [supplier].
     *
     * @throws [RequestException] if anything went wrong during the request.
     * @throws [EntityNotFoundException] if the [User] wasn't present.
     */
    public suspend fun getUser(): User = supplier.getUser(ownerUserId)

    /**
     * Requests to get the team owner through the [supplier],
     * returns null if the [User] isn't present.
     *
     * @throws [RequestException] if anything went wrong during the request.
     */
    public suspend fun getUserOrNUll(): User? = supplier.getUserOrNull(ownerUserId)

    override fun withStrategy(strategy: EntitySupplyStrategy<*>): Team = Team(data, kord, strategy.supply(kord))

    override fun toString(): String {
        return "Team(data=$data, kord=$kord, supplier=$supplier)"
    }

}

/**
 * A member of a Discord developer team.
 */
public class TeamMember(public val data: TeamMemberData, public val kord: Kord) {
    /**
     * An enumeration representing the membership state of this user.
     */
    public val membershipState: TeamMembershipState get() = data.membershipState

    /**
     * A collection of permissions granted to this member.
     * At the moment, this collection will only have one element: `*`, meaning the member has all permissions.
     * This is because right now there are no other permissions. Read mode [here](https://discord.com/developers/docs/topics/teams#data-models-team-members-object)
     */
    public val permissions: List get() = data.permissions

    /**
     * The unique ID that this member belongs to.
     */
    public val teamId: Snowflake get() = data.teamId

    /**
     * The ID of the user this member represents.
     */
    public val userId: Snowflake get() = data.userId

    /**
     * Utility method that gets the user from Kord.
     */
    public suspend fun getUser(): User? = kord.getUser(userId)

    override fun toString(): String {
        return "TeamMember(data=$data, kord=$kord)"
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy