-
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.
Merge pull request #475 from Shikkanime/dev
Dev
- Loading branch information
Showing
7 changed files
with
126 additions
and
74 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
71 changes: 71 additions & 0 deletions
71
src/main/kotlin/fr/shikkanime/socialnetworks/listeners/SlashCommandInteractionListener.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,71 @@ | ||
package fr.shikkanime.socialnetworks.listeners | ||
|
||
import fr.shikkanime.socialnetworks.DiscordSocialNetwork.Channel | ||
import fr.shikkanime.utils.Constant | ||
import fr.shikkanime.utils.ObjectParser | ||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent | ||
import net.dv8tion.jda.api.hooks.ListenerAdapter | ||
import java.io.File | ||
|
||
class SlashCommandInteractionListener : ListenerAdapter() { | ||
private fun getFile() = File(Constant.configFolder, "discord_channels.json") | ||
|
||
private fun getChannels(file: File): MutableList<Channel> { | ||
return if (file.exists()) { | ||
ObjectParser.fromJson(file.readText(), Array<Channel>::class.java).toMutableList() | ||
} else { | ||
mutableListOf() | ||
} | ||
} | ||
|
||
override fun onSlashCommandInteraction(event: SlashCommandInteractionEvent) { | ||
if (!event.name.equals("add-release-channel", true)) return | ||
|
||
val channel = event.getOption("channel")?.asChannel ?: run { | ||
event.reply("Channel is required").setEphemeral(true).queue() | ||
return | ||
} | ||
|
||
// If we can send messages to the channel | ||
if (!channel.type.isMessage) { | ||
event.reply("I can't send messages to this channel").setEphemeral(true).queue() | ||
return | ||
} | ||
|
||
val animeName = event.getOption("anime")?.asString | ||
val releaseType = if (animeName.isNullOrBlank()) "ALL" else "CUSTOM" | ||
|
||
// Save the channel to the database | ||
event.reply("Channel added to receive release notifications").queue() | ||
|
||
val file = getFile() | ||
val channels = getChannels(file) | ||
|
||
// If channel id already exists, update the release type and animes | ||
val existingChannel = channels.firstOrNull { it.id == channel.idLong } | ||
|
||
if (existingChannel != null) { | ||
val oldReleaseType = existingChannel.releaseType | ||
existingChannel.releaseType = releaseType | ||
|
||
if (oldReleaseType != releaseType && oldReleaseType == "CUSTOM") { | ||
existingChannel.animes.clear() | ||
} | ||
|
||
if (releaseType == "CUSTOM" && !animeName.isNullOrBlank() && !existingChannel.animes.contains( | ||
animeName | ||
) | ||
) { | ||
existingChannel.animes.add(animeName) | ||
} | ||
} else { | ||
channels.add( | ||
Channel( | ||
channel.idLong, | ||
releaseType, | ||
animeName?.let { mutableListOf(it) } ?: mutableListOf())) | ||
} | ||
|
||
file.writeText(ObjectParser.toJson(channels)) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/test/kotlin/fr/shikkanime/jobs/FetchOldEpisodesJobTest.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,29 @@ | ||
package fr.shikkanime.jobs | ||
|
||
import com.google.inject.Inject | ||
import fr.shikkanime.caches.CountryCodeIdKeyCache | ||
import fr.shikkanime.entities.enums.CountryCode | ||
import fr.shikkanime.utils.Constant | ||
import org.junit.jupiter.api.Assertions.assertNotNull | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
|
||
class FetchOldEpisodesJobTest { | ||
@Inject | ||
private lateinit var fetchOldEpisodesJob: FetchOldEpisodesJob | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
Constant.injector.injectMembers(this) | ||
} | ||
|
||
@Test | ||
fun `check if Atelier Riza is correctly fetch`() { | ||
val episodes = fetchOldEpisodesJob.crunchyrollEpisodesCache[CountryCodeIdKeyCache(CountryCode.FR, "GEXH3W2Z5")] | ||
|
||
// If episodes contains the episode 1, it means that the job has worked | ||
assertNotNull(episodes) | ||
assertTrue(episodes!!.any { it.seasonNumber == 1 && it.number == 1 }) | ||
} | ||
} |