-
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.
- Loading branch information
Showing
12 changed files
with
129 additions
and
16 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package fr.shikkanime.services | ||
|
||
import com.google.inject.Inject | ||
import fr.shikkanime.dtos.EpisodeDto | ||
import fr.shikkanime.utils.LoggerFactory | ||
import fr.shikkanime.utils.StringUtils | ||
import net.dv8tion.jda.api.EmbedBuilder | ||
import net.dv8tion.jda.api.JDA | ||
import net.dv8tion.jda.api.JDABuilder | ||
import net.dv8tion.jda.api.entities.Activity | ||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel | ||
import java.net.URI | ||
import java.time.ZonedDateTime | ||
import java.util.logging.Level | ||
import javax.imageio.ImageIO | ||
|
||
class DiscordService { | ||
@Inject | ||
private lateinit var configService: ConfigService | ||
|
||
private val logger = LoggerFactory.getLogger(DiscordService::class.java) | ||
private var isInitialized = false | ||
private var jda: JDA? = null | ||
|
||
fun init() { | ||
if (isInitialized) return | ||
|
||
try { | ||
val builder = JDABuilder.createDefault(configService.getValueAsString("discord_token")) | ||
builder.setActivity(Activity.playing("https://www.shikkanime.fr/")) | ||
jda = builder.build() | ||
jda?.awaitReady() | ||
isInitialized = true | ||
} catch (e: Exception) { | ||
logger.log(Level.SEVERE, "Error while initializing DiscordService", e) | ||
} | ||
} | ||
|
||
private fun getTextChannels(): MutableList<TextChannel>? = | ||
jda?.getTextChannelsByName("bot\uD83E\uDD16", true) | ||
|
||
fun sendEpisodeRelease(episodeDto: EpisodeDto) { | ||
init() | ||
if (!isInitialized) return | ||
if (episodeDto.image.isBlank()) return | ||
|
||
try { | ||
val embedMessage = EmbedBuilder() | ||
val image = ImageIO.read(URI(episodeDto.image).toURL()) | ||
embedMessage.setColor(ImageService.getDominantColor(image)) | ||
embedMessage.setAuthor( | ||
episodeDto.platform.platformName, | ||
episodeDto.platform.url, | ||
"https://www.shikkanime.fr/admin/assets/img/platforms/${episodeDto.platform.image}" | ||
) | ||
embedMessage.setTitle(episodeDto.anime.shortName, episodeDto.url) | ||
embedMessage.setDescription("**${episodeDto.title ?: "Untitled"}**\n${StringUtils.toEpisodeString(episodeDto)}") | ||
embedMessage.setImage(episodeDto.image) | ||
embedMessage.setFooter("Shikkanime", "https://www.shikkanime.fr/admin/assets/favicons/favicon-64x64.png") | ||
embedMessage.setTimestamp(ZonedDateTime.parse(episodeDto.releaseDateTime).toInstant()) | ||
val embed = embedMessage.build() | ||
|
||
getTextChannels()?.forEach { it.sendMessageEmbeds(embed).queue() } | ||
} catch (e: Exception) { | ||
logger.log(Level.SEVERE, "Error while sending message to Discord", e) | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package fr.shikkanime.utils | ||
|
||
import java.time.ZonedDateTime | ||
|
||
fun ZonedDateTime.isEqualOrAfter(other: ZonedDateTime): Boolean { | ||
return this.isEqual(other) || this.isAfter(other) | ||
} |
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,24 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<databaseChangeLog | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://www.liquibase.org/xml/ns/dbchangelog" | ||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog | ||
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.25.xsd" | ||
objectQuotingStrategy="QUOTE_ONLY_RESERVED_WORDS"> | ||
<property global="false" name="id" value="1704810005026"/> | ||
<property global="false" name="author" value="Ziedelth"/> | ||
|
||
<changeSet id="${id}-1" author="${author}" dbms="postgresql"> | ||
<preConditions onFail="MARK_RAN"> | ||
<sqlCheck expectedResult="0">SELECT COUNT(*) | ||
FROM config | ||
WHERE property_key = 'discord_token'</sqlCheck> | ||
</preConditions> | ||
|
||
<insert tableName="config"> | ||
<column name="uuid" valueComputed="gen_random_uuid()"/> | ||
<column name="property_key" value="discord_token"/> | ||
<column name="property_value" value=""/> | ||
</insert> | ||
</changeSet> | ||
</databaseChangeLog> |
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