Skip to content

Commit

Permalink
Add better error messages and use prerelease branch
Browse files Browse the repository at this point in the history
  • Loading branch information
nea89o committed Feb 15, 2024
1 parent 43344a2 commit 45315d0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/main/kotlin/moe/nea/gaybot/Gaybot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import dev.kord.core.behavior.interaction.response.respond
import dev.kord.core.event.interaction.GuildChatInputCommandInteractionCreateEvent
import dev.kord.core.on
import dev.kord.rest.builder.interaction.string
import dev.kord.rest.builder.message.allowedMentions
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.request.*
Expand All @@ -19,9 +20,10 @@ import kotlinx.coroutines.flow.onEach

suspend fun main() {
GitService.ensureInitialized()
GitService.checkoutBranch("master")
val masterBranch = "prerelease"
GitService.checkoutBranch(masterBranch)
GitService.fetchRepo()
GitService.setHeadTo("origin/master")
GitService.setHeadTo("origin/$masterBranch")

val kord = Kord(System.getenv("TOKEN"))
val moulberryBushId = Snowflake(516977525906341928)
Expand All @@ -30,6 +32,7 @@ suspend fun main() {
val client = HttpClient(CIO)
BoosterNamesService.load()

kord.getGlobalApplicationCommands().onEach { it.delete() }
val moulberryBush = kord.getGuild(moulberryBushId)
moulberryBush.getApplicationCommands().onEach {
it.delete()
Expand All @@ -55,8 +58,11 @@ suspend fun main() {
}
}
}
RainbowManager.setRainbowNamesInMisc(legacy)
return GitService.useLock {
GitService.checkoutBranch(masterBranch)
GitService.fetchRepo()
GitService.setHeadTo("origin/$masterBranch")
RainbowManager.setRainbowNamesInMisc(legacy)
GitService.commitFiles("Updated rainbownames", RainbowManager.miscFile)
GitService.pushHeadTo("origin", "bot/rainbownames")
GitService.parseCommit("HEAD")
Expand All @@ -82,24 +88,38 @@ suspend fun main() {
}
kord.on<GuildChatInputCommandInteractionCreateEvent> {
if (interaction.command.rootName != "rainbowlink") return@on
if (boostRoleId !in interaction.user.roleIds) {
if (boostRoleId !in interaction.user.roleIds && maintainerRoleId !in interaction.user.roleIds) {
interaction.respondEphemeral {
content =
"You are not currently boosting this server. Please boost the server in order to link your account and get a rainbow name in /pv. Check out `/pv Eisengolem` to see how it looks!"
}
return@on
}
val name = interaction.command.strings["mcname"]!!
require(name.matches("^[a-z0-9A-Z]{3,}$".toRegex()))
val response = interaction.deferPublicResponse()
if (!name.matches("^[_a-z0-9A-Z]{3,16}$".toRegex())) {
response.respond {
content = "$name does not seeem to be a valid minecraft user name"
allowedMentions()
}
return@on
}
val mojangText = client.get {
url("https://api.ashcon.app/mojang/v2/user/$name")
}.bodyAsText()
val json = gson.fromJson(mojangText, JsonObject::class.java)
if (json["error"] != null) {
response.respond {
allowedMentions()
content = "Could not find uuid for $name: `${json["error"]}`"
}
return@on
}
val uuid = json.getAsJsonPrimitive("uuid").asString.replace("-", "")
BoosterNamesService.setUuid(interaction.user.id, uuid)
BoosterNamesService.save()
response.respond {
allowedMentions()
content =
"Changed your linked username to $name (`$uuid`). Please be aware that it may take about a day until this change is applied. If it takes longer than a day for your name to become rainbowed in /pv, please update your repository."
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/moe/nea/gaybot/GitService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ object GitService {
fun checkoutBranch(branchname: String) {
if (callGit("branch", "--", branchname).exitValue() == 0) {
logger.info("Created branch $branchname")
} else {
logger.info("Did not create branch $branchname")
}
logger.info("Switching to branch $branchname")
callGit("switch", "-f", "--", branchname)
Expand Down

0 comments on commit 45315d0

Please sign in to comment.