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

[Feature] Add modern message styles and link button and inline link support #61

Merged
merged 16 commits into from
Nov 13, 2024
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
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,4 @@ fun replaceVersion(path: String) {
}

replaceVersion("README.md")
replaceVersion("docs/pages/getting_started.mdx")
replaceVersion("docs/pages/getting_started.mdx")
Awakened-Redstone marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion docs/pages/platforms/curseforge.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ publishMods {
changelogType = "markdown"
}
}
```
```
72 changes: 71 additions & 1 deletion docs/pages/platforms/discord.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,74 @@ publishMods {
announcementTitle = "Download from CurseForge"
}
}
```
```

The webhook message can be customized to different styles
The look can be set to `CLASSIC` or `MODERN`

<details>
<summary>Look examples</summary>
## Classic look
The classic look is used by default by the plugin
![Classic look](/images/discord_example.png)

## Modern look
![Modern look](/images/discord_modern.png)

```groovy
publishMods {
// ...
discord {
// ...
style {
// ...
look = MODERN
}
}
}
```

The style tag also has the options `thumbnailUrl` and `color`, this options allow you to customize more the look of your embeds

The `color` option can be either an integer or a string, the string must be a full `RRGGBB` hex optionally prefixed with a `#`.
The string can also be `modrinth`, `github` or `curseforge`, with those working as alias for the colors used for the embed links for those platforms

![Example with thumbnail](/images/discord_modern_button_thumbnail.png)
```groovy
publishMods {
// ...
discord {
// ...
style {
// ...
look = MODERN
thumbnailUrl = https://example.com
color = "github"
}
}
}
```
</details>

You can also set your links to be posted as `EMBED` or `BUTTON`
<details>
<summary>Link examples</summary>
## Embed link
The embed links are used by default by the plugin
![Embed links](/images/discord_example.png)
## Button links
![Button links](/images/discord_button.png)

```groovy
publishMods {
// ...
discord {
// ...
style {
// ...
link = BUTTON
}
}
}
```
</details>
2 changes: 1 addition & 1 deletion docs/pages/platforms/modrinth.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ publishMods {
embeds("project-slug")
}
}
```
```
Binary file added docs/public/images/discord_button.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/public/images/discord_modern.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 55 additions & 12 deletions src/main/kotlin/me/modmuss50/mpp/platforms/discord/DiscordAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import okhttp3.RequestBody.Companion.toRequestBody

object DiscordAPI {
@OptIn(ExperimentalSerializationApi::class)
val json = Json { explicitNulls = false }
val json = Json { explicitNulls = false; classDiscriminator = "class"; encodeDefaults = true }
Awakened-Redstone marked this conversation as resolved.
Show resolved Hide resolved
private val httpUtils = HttpUtils()
private val headers: Map<String, String> = mapOf("Content-Type" to "application/json")

Expand All @@ -29,17 +29,43 @@ object DiscordAPI {
val avatarUrl: String? = null,
val tts: Boolean? = null,
val embeds: List<Embed>? = null,
// allowedMentions -- Skip these as we dont need them
// components
// files
// allowedMentions -- Skip this as we don't need it
val components: List<Component>? = null,
// files -- Skip these as we don't need them
// payload_json
// attachments
val flags: Int? = null,
@SerialName("thread_name")
val threadName: String? = null,
)

// https://discord.com/developers/docs/resources/channel#embed-object
@Serializable
sealed class Component {
protected abstract val type: Int
}

@Serializable
data class ActionRow(
val components: List<Component>? = null,
) : Component() {
override val type: Int = 1
}

@Serializable
data class ButtonComponent(
val label: String? = null,
// emoji
// @SerialName("custom_id")
// val customId: String?,
// sku_id
val url: String? = null,
// disabled
) : Component() {
override val type: Int = 2
val style: Int = 5 // Shouldn't be touched as we only work with links
}

// https://discord.com/developers/docs/resources/message#embed-object
@Serializable
data class Embed(
val title: String? = null,
Expand All @@ -57,7 +83,7 @@ object DiscordAPI {
val fields: List<EmbedField>? = null,
)

// https://discord.com/developers/docs/resources/channel#embed-object-embed-footer-structure
// https://discord.com/developers/docs/resources/message#embed-object-embed-footer-structure
@Serializable
data class EmbedFooter(
val text: String,
Expand All @@ -67,7 +93,7 @@ object DiscordAPI {
val proxyIconUrl: String? = null,
)

// https://discord.com/developers/docs/resources/channel#embed-object-embed-image-structure
// https://discord.com/developers/docs/resources/message#embed-object-embed-image-structure
@Serializable
data class EmbedImage(
val url: String,
Expand All @@ -77,7 +103,7 @@ object DiscordAPI {
val width: Int? = null,
)

// https://discord.com/developers/docs/resources/channel#embed-object-embed-thumbnail-structure
// https://discord.com/developers/docs/resources/message#embed-object-embed-thumbnail-structure
@Serializable
data class EmbedThumbnail(
val url: String,
Expand All @@ -87,7 +113,7 @@ object DiscordAPI {
val width: Int? = null,
)

// https://discord.com/developers/docs/resources/channel#embed-object-embed-video-structure
// https://discord.com/developers/docs/resources/message#embed-object-embed-video-structure
@Serializable
data class EmbedVideo(
val url: String? = null,
Expand All @@ -97,14 +123,14 @@ object DiscordAPI {
val width: Int? = null,
)

// https://discord.com/developers/docs/resources/channel#embed-object-embed-provider-structure
// https://discord.com/developers/docs/resources/message#embed-object-embed-provider-structure
@Serializable
data class EmbedProvider(
val name: String? = null,
val url: String? = null,
)

// https://discord.com/developers/docs/resources/channel#embed-object-embed-author-structure
// https://discord.com/developers/docs/resources/message#embed-object-embed-author-structure
@Serializable
data class EmbedAuthor(
val name: String,
Expand All @@ -115,11 +141,28 @@ object DiscordAPI {
val proxyIconUrl: String? = null,
)

// https://discord.com/developers/docs/resources/channel#embed-object-embed-field-structure
// https://discord.com/developers/docs/resources/message#embed-object-embed-field-structure
@Serializable
data class EmbedField(
val name: String,
val value: String,
val inline: Boolean? = null,
)

// https://discord.com/developers/docs/resources/webhook#get-webhook
fun getWebhook(url: String): WebhookData {
val response = httpUtils.get<WebhookData>(url, headers)
return response
}

/**
* The response from getting the webhook data
*/
@Serializable
data class WebhookData(
// Only get the application id
// as this is the only thing that matters
@SerialName("application_id")
val applicationId: String?,
)
}
Loading