<commit_before>package net.perfectdreams.loritta

import com.mrpowergamerbr.loritta.LorittaLauncher
import com.mrpowergamerbr.loritta.utils.Constants
import com.mrpowergamerbr.loritta.utils.extensions.await
import com.mrpowergamerbr.loritta.utils.loritta
import com.mrpowergamerbr.loritta.utils.lorittaShards
import kotlinx.coroutines.*
import mu.KotlinLogging
import net.dv8tion.jda.api.EmbedBuilder
import net.dv8tion.jda.api.entities.Icon
import net.perfectdreams.loritta.utils.Emotes
import net.perfectdreams.loritta.utils.config.FanArtArtist
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
import java.io.File

class ChangeBanner(val m: QuirkyStuff, val config: QuirkyConfig) {
	companion object {
		private val logger = KotlinLogging.logger {}
	}

	var task: Job? = null
	var currentBannerIndex = -1

	fun selectRandomBanner(): Pair<File, FanArtArtist> {
		logger.info { "Selecting new banner for the guild..." }
		val bannersFolder = File(m.dataFolder, "banners/")

		val currentBannerIndexFile = File(m.dataFolder, "current_banner_index")
		if (currentBannerIndex == -1) {
			currentBannerIndex = if (currentBannerIndexFile.exists())
				currentBannerIndexFile.readText().toInt()
			else
				0
		}

		if (currentBannerIndex + 1 > config.changeBanner.banners.size)
			currentBannerIndex = 0

		logger.info { "Current banner index is $currentBannerIndex"}

		val randomBanner = bannersFolder.listFiles().first { it.name == config.changeBanner.banners[currentBannerIndex] }

		val artistId = randomBanner.nameWithoutExtension.split("-").first()

		val fanArtArtist = loritta.fanArtArtists.first { it.id == artistId }
		logger.info { "Using banner $randomBanner by $fanArtArtist"}

		currentBannerIndexFile.writeText(currentBannerIndex.toString())
		currentBannerIndex++

		return Pair(randomBanner, fanArtArtist)
	}

	fun start() {
		logger.info { "Starting Banner Changing Task..." }
		task = GlobalScope.launch(LorittaLauncher.loritta.coroutineDispatcher) {
			while (true) {
				val currentMillisRelativeToTheCurrentHour = System.currentTimeMillis() % config.changeBanner.timeMod
				logger.info { "Banner will be changed in ${currentMillisRelativeToTheCurrentHour}ms!"}
				delay(config.changeBanner.timeMod - currentMillisRelativeToTheCurrentHour) // Vamos esperar até a próxima hora!

				val randomBanner = selectRandomBanner()
				logger.info { "New banner is ${randomBanner.first} by ${randomBanner.second.id}!"}

				val fanArtArtist = randomBanner.second
				val discordId = fanArtArtist.socialNetworks
						?.firstIsInstanceOrNull<FanArtArtist.SocialNetwork.DiscordSocialNetwork>()
						?.id

				val artistUser = lorittaShards.getUserById(discordId)

				for (guildId in config.changeBanner.guilds) {
					val guild = lorittaShards.getGuildById(guildId) ?: continue

					val icon = withContext(Dispatchers.IO) {<commit_msg>Salvar no arquivo após alterar o valor da variável, e não antes<commit_after>package net.perfectdreams.loritta

import com.mrpowergamerbr.loritta.LorittaLauncher
import com.mrpowergamerbr.loritta.utils.Constants
import com.mrpowergamerbr.loritta.utils.extensions.await
import com.mrpowergamerbr.loritta.utils.loritta
import com.mrpowergamerbr.loritta.utils.lorittaShards
import kotlinx.coroutines.*
import mu.KotlinLogging
import net.dv8tion.jda.api.EmbedBuilder
import net.dv8tion.jda.api.entities.Icon
import net.perfectdreams.loritta.utils.Emotes
import net.perfectdreams.loritta.utils.config.FanArtArtist
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
import java.io.File

class ChangeBanner(val m: QuirkyStuff, val config: QuirkyConfig) {
	companion object {
		private val logger = KotlinLogging.logger {}
	}

	var task: Job? = null
	var currentBannerIndex = -1

	fun selectRandomBanner(): Pair<File, FanArtArtist> {
		logger.info { "Selecting new banner for the guild..." }
		val bannersFolder = File(m.dataFolder, "banners/")

		val currentBannerIndexFile = File(m.dataFolder, "current_banner_index")
		if (currentBannerIndex == -1) {
			currentBannerIndex = if (currentBannerIndexFile.exists())
				currentBannerIndexFile.readText().toInt()
			else
				0
		}

		if (currentBannerIndex + 1 > config.changeBanner.banners.size)
			currentBannerIndex = 0

		logger.info { "Current banner index is $currentBannerIndex"}

		val randomBanner = bannersFolder.listFiles().first { it.name == config.changeBanner.banners[currentBannerIndex] }

		val artistId = randomBanner.nameWithoutExtension.split("-").first()

		val fanArtArtist = loritta.fanArtArtists.first { it.id == artistId }
		logger.info { "Using banner $randomBanner by $fanArtArtist"}

		currentBannerIndex++
		currentBannerIndexFile.writeText(currentBannerIndex.toString())

		return Pair(randomBanner, fanArtArtist)
	}

	fun start() {
		logger.info { "Starting Banner Changing Task..." }
		task = GlobalScope.launch(LorittaLauncher.loritta.coroutineDispatcher) {
			while (true) {
				val currentMillisRelativeToTheCurrentHour = System.currentTimeMillis() % config.changeBanner.timeMod
				logger.info { "Banner will be changed in ${currentMillisRelativeToTheCurrentHour}ms!"}
				delay(config.changeBanner.timeMod - currentMillisRelativeToTheCurrentHour) // Vamos esperar até a próxima hora!

				val randomBanner = selectRandomBanner()
				logger.info { "New banner is ${randomBanner.first} by ${randomBanner.second.id}!"}

				val fanArtArtist = randomBanner.second
				val discordId = fanArtArtist.socialNetworks
						?.firstIsInstanceOrNull<FanArtArtist.SocialNetwork.DiscordSocialNetwork>()
						?.id

				val artistUser = lorittaShards.getUserById(discordId)

				for (guildId in config.changeBanner.guilds) {
					val guild = lorittaShards.getGuildById(guildId) ?: continue

					val icon = withContext(Dispatchers.IO) {
{'fraction_non_alphanumeric': 0.09813084112149532, 'fraction_numerical': 0.0024229837313949464, 'mean_word_length': 5.007276507276507, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5111024', 'n_tokens_mistral': 2093, 'n_tokens_neox': 1862, 'n_words': 359}