mirror of
https://github.com/HMCore/Orbot.git
synced 2025-12-12 22:06:23 +00:00
Refactoring, Tests
This commit is contained in:
@@ -1,27 +0,0 @@
|
||||
import net.dv8tion.jda.internal.requests.Method;
|
||||
import net.dv8tion.jda.internal.requests.Route;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
|
||||
public class Inaccessibles {
|
||||
/**
|
||||
* This is private by default
|
||||
*
|
||||
* @param method look
|
||||
* @param route somewhere
|
||||
* @return else
|
||||
*/
|
||||
public static Route getRoute(Method method, String route) {
|
||||
try {
|
||||
Constructor<?> constructor = Route.class.getDeclaredConstructor(Method.class, String.class);
|
||||
constructor.setAccessible(true);
|
||||
return (Route) constructor.newInstance(method, route);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String toUnsignedString(long num) {
|
||||
return Long.toUnsignedString(num);
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,6 @@ package org.hmcore;
|
||||
import com.github.redouane59.twitter.TwitterClient;
|
||||
import com.github.redouane59.twitter.dto.tweet.Tweet;
|
||||
import com.github.redouane59.twitter.signature.TwitterCredentials;
|
||||
import de.wulkanat.Admin;
|
||||
import de.wulkanat.Channels;
|
||||
import net.dv8tion.jda.api.MessageBuilder;
|
||||
import org.quartz.Job;
|
||||
import org.quartz.JobExecutionContext;
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
package de.wulkanat.extensions
|
||||
|
||||
import Inaccessibles
|
||||
import net.dv8tion.jda.api.entities.Message
|
||||
import net.dv8tion.jda.api.entities.MessageChannel
|
||||
import net.dv8tion.jda.api.requests.restaction.MessageAction
|
||||
import net.dv8tion.jda.internal.requests.Method
|
||||
import net.dv8tion.jda.internal.requests.Route
|
||||
import net.dv8tion.jda.internal.requests.restaction.MessageActionImpl
|
||||
import net.dv8tion.jda.internal.utils.Checks
|
||||
|
||||
fun MessageChannel.crosspostById(messageId: String): MessageAction {
|
||||
Checks.isSnowflake(messageId, "Message ID")
|
||||
|
||||
val route = CROSSPOST_MESSAGE.compile(id, messageId)
|
||||
return MessageActionImpl(jda, route, this).append("This is not of your interest.")
|
||||
}
|
||||
|
||||
fun Message.crosspost(): MessageAction {
|
||||
val messageId = Inaccessibles.toUnsignedString(idLong)
|
||||
|
||||
return channel.crosspostById(messageId)
|
||||
}
|
||||
|
||||
val CROSSPOST_MESSAGE: Route = Inaccessibles.getRoute(
|
||||
Method.POST,
|
||||
"channels/{channel_id}/messages/{message_id}/crosspost"
|
||||
)
|
||||
@@ -1,27 +0,0 @@
|
||||
package de.wulkanat.model
|
||||
|
||||
import net.dv8tion.jda.api.EmbedBuilder
|
||||
import net.dv8tion.jda.api.entities.MessageEmbed
|
||||
import de.wulkanat.extensions.hex2Rgb
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class BlogPostPreview(
|
||||
val title: String,
|
||||
val description: String,
|
||||
val date: String,
|
||||
val author: String,
|
||||
val imgUrl: String,
|
||||
val fullPostUrl: String
|
||||
) {
|
||||
fun toMessageEmbed(): MessageEmbed {
|
||||
return EmbedBuilder()
|
||||
.setTitle(this.title, this.fullPostUrl)
|
||||
.setDescription(this.description)
|
||||
.setAuthor(this.author)
|
||||
.setThumbnail(this.imgUrl)
|
||||
.setFooter(this.date, "https://www.hytale.com/static/images/logo-h.png")
|
||||
.setColor(hex2Rgb("#337fb0"))
|
||||
.build()
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
@file:JvmName("Admin")
|
||||
|
||||
package de.wulkanat
|
||||
package org.hmcore
|
||||
|
||||
import de.wulkanat.extensions.embed
|
||||
import org.hmcore.extensions.embed
|
||||
import kotlinx.serialization.decodeFromString
|
||||
import kotlinx.serialization.json.Json
|
||||
import net.dv8tion.jda.api.entities.MessageEmbed
|
||||
@@ -23,7 +23,7 @@ object Admin {
|
||||
fun connectToUser() {
|
||||
Main.jdas.find { jda ->
|
||||
jda.retrieveUserById(userId).complete()?.also { admin = it } != null
|
||||
} ?: return kotlin.io.println("Connection to de.wulkanat.Admin failed!")
|
||||
} ?: return kotlin.io.println("Connection to org.hmcore.Admin failed!")
|
||||
|
||||
kotlin.io.println("Connected to ${admin!!.name}. No further errors will be printed here.")
|
||||
}
|
||||
@@ -1,11 +1,10 @@
|
||||
package de.wulkanat
|
||||
package org.hmcore
|
||||
|
||||
import de.wulkanat.web.fakeUpdateBlogPost
|
||||
import de.wulkanat.web.fakeUpdateJobListings
|
||||
import org.hmcore.web.fakeUpdateBlogPost
|
||||
import org.hmcore.web.fakeUpdateJobListings
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter
|
||||
import net.dv8tion.jda.api.EmbedBuilder
|
||||
import net.dv8tion.jda.api.events.message.priv.PrivateMessageReceivedEvent
|
||||
import org.hmcore.TwitterJob
|
||||
import java.awt.Color
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
@file:JvmName("Channels")
|
||||
|
||||
package de.wulkanat
|
||||
package org.hmcore
|
||||
|
||||
import de.wulkanat.webhook.WebhookCaller
|
||||
import org.hmcore.webhook.WebhookCaller
|
||||
import kotlinx.serialization.decodeFromString
|
||||
import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.json.Json
|
||||
@@ -1,9 +1,7 @@
|
||||
@file:JvmName("DataIO")
|
||||
package de.wulkanat
|
||||
package org.hmcore
|
||||
|
||||
import de.wulkanat.extensions.ensureExists
|
||||
import de.wulkanat.model.BlogPostPreview
|
||||
import de.wulkanat.model.JobListingPreview
|
||||
import org.hmcore.extensions.ensureExists
|
||||
import kotlinx.serialization.Required
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.decodeFromString
|
||||
@@ -1,7 +1,4 @@
|
||||
package de.wulkanat
|
||||
|
||||
import net.dv8tion.jda.api.JDA
|
||||
import net.dv8tion.jda.api.entities.Activity
|
||||
package org.hmcore
|
||||
|
||||
object DiscordRpc {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package de.wulkanat
|
||||
package org.hmcore
|
||||
|
||||
import net.dv8tion.jda.api.events.ExceptionEvent
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter
|
||||
@@ -1,6 +1,6 @@
|
||||
package de.wulkanat
|
||||
package org.hmcore
|
||||
|
||||
import de.wulkanat.web.getNewBlogPosts
|
||||
import org.hmcore.web.getNewBlogPosts
|
||||
import net.dv8tion.jda.api.JDA
|
||||
import net.dv8tion.jda.api.JDABuilder
|
||||
import net.dv8tion.jda.api.MessageBuilder
|
||||
@@ -9,7 +9,6 @@ import net.dv8tion.jda.api.requests.GatewayIntent
|
||||
import net.dv8tion.jda.api.utils.ChunkingFilter
|
||||
import net.dv8tion.jda.api.utils.MemberCachePolicy
|
||||
import net.dv8tion.jda.api.utils.cache.CacheFlag
|
||||
import org.hmcore.TwitterJob
|
||||
import org.quartz.CronScheduleBuilder.cronSchedule
|
||||
import org.quartz.JobBuilder.newJob
|
||||
import org.quartz.JobDetail
|
||||
@@ -1,4 +1,4 @@
|
||||
package de.wulkanat
|
||||
package org.hmcore
|
||||
|
||||
import net.dv8tion.jda.api.EmbedBuilder
|
||||
import net.dv8tion.jda.api.Permission
|
||||
@@ -1,4 +1,4 @@
|
||||
package de.wulkanat.extensions
|
||||
package org.hmcore.extensions
|
||||
|
||||
import java.awt.Color
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package de.wulkanat.extensions
|
||||
package org.hmcore.extensions
|
||||
|
||||
import net.dv8tion.jda.api.EmbedBuilder
|
||||
import java.awt.Color
|
||||
@@ -23,6 +23,12 @@ class EmbedBuilderBuilder {
|
||||
}
|
||||
get() = Color.BLACK
|
||||
|
||||
var thumbnail: String?
|
||||
set(value) {
|
||||
_embed.setThumbnail(value)
|
||||
}
|
||||
get() = null
|
||||
|
||||
fun field(builder: FieldBuilderBuilder.() -> Unit) =
|
||||
FieldBuilderBuilder().apply { builder() }.let {
|
||||
_embed.addField(it.name, it.value, it.inline)
|
||||
@@ -32,6 +38,16 @@ class EmbedBuilderBuilder {
|
||||
AuthorBuilderBuilder().apply { builder() }.let {
|
||||
_embed.setAuthor(it.name, it.url, it.icon)
|
||||
}
|
||||
|
||||
fun title(builder: TitleBuilderBuilder.() -> Unit) =
|
||||
TitleBuilderBuilder().apply { builder() }.let {
|
||||
_embed.setTitle(it.title, it.url)
|
||||
}
|
||||
|
||||
fun footer(builder: FooterBuilderBuilder.() -> Unit) =
|
||||
FooterBuilderBuilder().apply { builder() }.let {
|
||||
_embed.setFooter(it.value, it.iconUrl)
|
||||
}
|
||||
}
|
||||
|
||||
class FieldBuilderBuilder {
|
||||
@@ -40,11 +56,21 @@ class FieldBuilderBuilder {
|
||||
var inline = false
|
||||
}
|
||||
|
||||
class TitleBuilderBuilder {
|
||||
var title: String? = null
|
||||
var url: String? = null
|
||||
}
|
||||
|
||||
class AuthorBuilderBuilder {
|
||||
var name: String? = null
|
||||
var url: String? = null
|
||||
var icon: String? = null
|
||||
}
|
||||
|
||||
class FooterBuilderBuilder {
|
||||
var value: String? = null
|
||||
var iconUrl: String? = null
|
||||
}
|
||||
|
||||
fun embed(builder: EmbedBuilderBuilder.() -> Unit) =
|
||||
EmbedBuilderBuilder().apply { builder() }._embed.build()
|
||||
@@ -1,4 +1,4 @@
|
||||
package de.wulkanat.extensions
|
||||
package org.hmcore.extensions
|
||||
|
||||
import java.io.File
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package de.wulkanat.extensions
|
||||
package org.hmcore.extensions
|
||||
|
||||
import org.jsoup.nodes.Element
|
||||
import org.jsoup.select.Elements
|
||||
33
src/main/kotlin/org/hmcore/model/BlogPostPreview.kt
Normal file
33
src/main/kotlin/org/hmcore/model/BlogPostPreview.kt
Normal file
@@ -0,0 +1,33 @@
|
||||
package org.hmcore.model
|
||||
|
||||
import org.hmcore.extensions.hex2Rgb
|
||||
import kotlinx.serialization.Serializable
|
||||
import org.hmcore.extensions.embed
|
||||
|
||||
@Serializable
|
||||
data class BlogPostPreview(
|
||||
val title: String,
|
||||
val description: String,
|
||||
val date: String,
|
||||
val author: String,
|
||||
val imgUrl: String,
|
||||
val fullPostUrl: String
|
||||
) {
|
||||
fun toMessageEmbed() = embed {
|
||||
title {
|
||||
title = this@BlogPostPreview.title
|
||||
url = fullPostUrl
|
||||
}
|
||||
description = this@BlogPostPreview.description
|
||||
color = hex2Rgb("#337FB0")
|
||||
thumbnail = imgUrl
|
||||
|
||||
author {
|
||||
name = author
|
||||
}
|
||||
footer {
|
||||
value = date
|
||||
iconUrl = "https://www.hytale.com/static/images/logo-h.png"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package de.wulkanat.model
|
||||
package org.hmcore.model
|
||||
|
||||
import de.wulkanat.extensions.hex2Rgb
|
||||
import org.hmcore.extensions.hex2Rgb
|
||||
import kotlinx.serialization.Serializable
|
||||
import net.dv8tion.jda.api.EmbedBuilder
|
||||
import net.dv8tion.jda.api.entities.MessageEmbed
|
||||
@@ -1,11 +1,11 @@
|
||||
package de.wulkanat.web
|
||||
package org.hmcore.web
|
||||
|
||||
import de.wulkanat.extensions.absUrl
|
||||
import de.wulkanat.extensions.get
|
||||
import de.wulkanat.extensions.imgSrc
|
||||
import de.wulkanat.extensions.text
|
||||
import de.wulkanat.model.BlogPostPreview
|
||||
import de.wulkanat.model.JobListingPreview
|
||||
import org.hmcore.extensions.absUrl
|
||||
import org.hmcore.extensions.get
|
||||
import org.hmcore.extensions.imgSrc
|
||||
import org.hmcore.extensions.text
|
||||
import org.hmcore.model.BlogPostPreview
|
||||
import org.hmcore.model.JobListingPreview
|
||||
|
||||
private const val BLOG_POST_STATE_FILE_NAME = "blog_state.json"
|
||||
fun fakeUpdateBlogPost(amount: Int = 1) = removeFromSiteSave<BlogPostPreview>(BLOG_POST_STATE_FILE_NAME, amount)
|
||||
@@ -1,4 +1,4 @@
|
||||
package de.wulkanat.web
|
||||
package org.hmcore.web
|
||||
|
||||
import kotlinx.serialization.decodeFromString
|
||||
import kotlinx.serialization.encodeToString
|
||||
@@ -1,6 +1,6 @@
|
||||
package de.wulkanat.webhook
|
||||
package org.hmcore.webhook
|
||||
|
||||
import de.wulkanat.WEBHOOKS
|
||||
import org.hmcore.WEBHOOKS
|
||||
import net.dv8tion.jda.api.entities.Message
|
||||
import org.hmcore.DiscordWebhook
|
||||
import org.hmcore.DiscordWebhook.EmbedObject
|
||||
16
src/test/kotlin/org/hmcore/extensions/ColorTest.kt
Normal file
16
src/test/kotlin/org/hmcore/extensions/ColorTest.kt
Normal file
@@ -0,0 +1,16 @@
|
||||
package org.hmcore.extensions
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
import java.awt.Color
|
||||
|
||||
class ColorTest {
|
||||
@Test
|
||||
fun `color should parse from hex correctly`() {
|
||||
assertEquals(hex2Rgb("#FFFFFF"), Color.WHITE)
|
||||
assertEquals(hex2Rgb("#000000"), Color.BLACK)
|
||||
assertEquals(hex2Rgb("#FF0000"), Color.RED)
|
||||
assertEquals(hex2Rgb("#00FF00"), Color.GREEN)
|
||||
assertEquals(hex2Rgb("#0000FF"), Color.BLUE)
|
||||
}
|
||||
}
|
||||
90
src/test/kotlin/org/hmcore/extensions/EmbedTest.kt
Normal file
90
src/test/kotlin/org/hmcore/extensions/EmbedTest.kt
Normal file
@@ -0,0 +1,90 @@
|
||||
package org.hmcore.extensions
|
||||
|
||||
import java.awt.Color
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotNull
|
||||
|
||||
class EmbedTest {
|
||||
@Test
|
||||
fun `Embed Title DSL should work`() {
|
||||
val dslEmbed = embed {
|
||||
title {
|
||||
title = "Title"
|
||||
url = "https://a.b.c"
|
||||
}
|
||||
}
|
||||
|
||||
assertEquals("Title", dslEmbed.title)
|
||||
assertEquals("https://a.b.c", dslEmbed.url)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Embed Thumbnail should work`() {
|
||||
val dslEmbed = embed {
|
||||
thumbnail = "https://a.b.c"
|
||||
}
|
||||
|
||||
assertNotNull(dslEmbed.thumbnail)
|
||||
assertEquals("https://a.b.c", dslEmbed.thumbnail!!.url)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Embed Footer DSL should work`() {
|
||||
val dslEmbed = embed {
|
||||
footer {
|
||||
value = "Value"
|
||||
iconUrl = "https://a.b.c"
|
||||
}
|
||||
}
|
||||
|
||||
assertNotNull(dslEmbed.footer)
|
||||
assertEquals("Value", dslEmbed.footer!!.text)
|
||||
assertEquals("https://a.b.c", dslEmbed.footer!!.iconUrl)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Embed DSL should produce same result as native`() {
|
||||
val dslEmbed = embed {
|
||||
title = "A Title"
|
||||
description = "A Description"
|
||||
color = Color.YELLOW
|
||||
|
||||
author {
|
||||
name = "An author"
|
||||
icon = "https://d.e.f"
|
||||
url = "https://a.b.c"
|
||||
}
|
||||
|
||||
field {
|
||||
inline = true
|
||||
name = "Field1"
|
||||
value = "Body1"
|
||||
}
|
||||
|
||||
field {
|
||||
inline = false
|
||||
name = "Field2"
|
||||
value = "Body2"
|
||||
}
|
||||
}
|
||||
|
||||
assertEquals("A Title", dslEmbed.title)
|
||||
assertEquals("A Description", dslEmbed.description)
|
||||
assertEquals(Color.YELLOW, dslEmbed.color)
|
||||
|
||||
assertNotNull(dslEmbed.author)
|
||||
assertEquals("An author", dslEmbed.author!!.name)
|
||||
assertEquals("https://d.e.f", dslEmbed.author!!.iconUrl)
|
||||
assertEquals("https://a.b.c", dslEmbed.author!!.url)
|
||||
|
||||
assertEquals(2, dslEmbed.fields.size)
|
||||
assertEquals("Field1", dslEmbed.fields[0].name)
|
||||
assertEquals("Body1", dslEmbed.fields[0].value)
|
||||
assertEquals(true, dslEmbed.fields[0].isInline)
|
||||
|
||||
assertEquals("Field2", dslEmbed.fields[1].name)
|
||||
assertEquals("Body2", dslEmbed.fields[1].value)
|
||||
assertEquals(false, dslEmbed.fields[1].isInline)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user