-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement fetching guilds for current OAuth user
- Loading branch information
1 parent
9b417f5
commit 3a9ff0a
Showing
9 changed files
with
114 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/dev/capybaralabs/shipa/discord/model/HasGuildIcon.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package dev.capybaralabs.shipa.discord.model | ||
|
||
import dev.capybaralabs.shipa.discord.model.ImageFormatting.Format.GIF | ||
import dev.capybaralabs.shipa.discord.model.ImageFormatting.Format.PNG | ||
import java.util.Optional | ||
import kotlin.jvm.optionals.getOrNull | ||
|
||
interface HasGuildIcon { | ||
val id: Long | ||
val icon: Optional<String> | ||
|
||
fun iconUrl(): String? { | ||
return icon.getOrNull()?.let { | ||
val format = if (it.startsWith("a_")) GIF else PNG | ||
ImageFormatting.imageUrl("/icons/$id/$it", format) | ||
} | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
src/main/kotlin/dev/capybaralabs/shipa/discord/model/PartialGuild.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package dev.capybaralabs.shipa.discord.model | ||
|
||
import java.util.Optional | ||
|
||
/** | ||
* [Partial Discord Guild Object](https://discord.com/developers/docs/resources/user#get-current-user-guilds-example-partial-guild) | ||
*/ | ||
data class PartialGuild( | ||
override val id: Long, | ||
val name: String, | ||
override val icon: Optional<String>, | ||
val owner: Boolean?, | ||
val permissions: StringBitfield<Permission>?, | ||
val features: List<String>, // don't use a enum, these change frequently and many are undocumented | ||
val approximateMemberCount: Int?, | ||
val approximatePresenceCount: Int?, | ||
) : HasGuildIcon |
26 changes: 26 additions & 0 deletions
26
src/main/kotlin/dev/capybaralabs/shipa/discord/oauth2/OAuth2Scope.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package dev.capybaralabs.shipa.discord.oauth2 | ||
|
||
|
||
/** | ||
* See [Discord OAuth2 Scopes](https://discord.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes) for the full list. | ||
*/ | ||
enum class OAuth2Scope(val discordName: String) { | ||
|
||
IDENTIFY("identify"), | ||
GUILDS("guilds"), | ||
; | ||
|
||
companion object { | ||
fun parse(input: String): OAuth2Scope? { | ||
for (scope in entries) { | ||
if (scope.discordName.equals(input, ignoreCase = true)) { | ||
return scope | ||
} | ||
if (scope.name.equals(input, ignoreCase = true)) { | ||
return scope | ||
} | ||
} | ||
return null | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/dev/capybaralabs/shipa/discord/oauth2/OAuth2ScopeException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package dev.capybaralabs.shipa.discord.oauth2 | ||
|
||
import dev.capybaralabs.shipa.discord.client.DiscordAuthToken | ||
|
||
/** | ||
* The used token is lacking a scope to access the requested Discord resource. | ||
*/ | ||
class OAuth2ScopeException( | ||
val token: DiscordAuthToken.Oauth2, | ||
val missingScope: OAuth2Scope, | ||
) : RuntimeException( | ||
"Token is missing scope $missingScope, has scopes ${token.scopes}", | ||
) |