Skip to content

Commit

Permalink
+ Counting Ready to go!
Browse files Browse the repository at this point in the history
  • Loading branch information
MLGPenguin committed Dec 26, 2023
1 parent 5b33524 commit 95c7ef2
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ PROJECTS_CHANNEL_ID=
STARBOARD_CHANNEL_ID=
SHOWCASE_CHANNEL_ID=
NEWS_CHANNEL_ID=
COUNTING_CHANNEL_ID=

STUDENT_ROLE_ID=
SUPPORT_ROLE_ID=
Expand All @@ -26,9 +27,8 @@ MANAGEMENT_ROLE_ID=
VERIFIER_ROLE_ID=

RIGHT_ARROW_EMOJI_ID=

UPVOTE_EMOJI_ID=
DOWNVOTE_EMOJI_ID=

NOSTARBOARD_EMOJI_ID=

STARBOARD_AMOUNT=
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies {
implementation("gg.flyte:neptune:2.4")
implementation("org.mongodb:mongodb-driver-sync:4.9.0")
implementation("io.github.cdimascio:dotenv-kotlin:6.4.1")
implementation(files("libs/Math-Evaluator-1.2.0.jar"))
}

application {
Expand Down
Binary file added libs/Math-Evaluator-1.2.0.jar
Binary file not shown.
1 change: 1 addition & 0 deletions src/main/kotlin/com/learnspigot/bot/Server.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ object Server {
val starboardChannel = guild.getTextChannelById(Environment.get("STARBOARD_CHANNEL_ID"))!!
val helpChannel = guild.getForumChannelById(Environment.get("HELP_CHANNEL_ID"))!!
val knowledgebaseChannel = guild.getForumChannelById(Environment.get("KNOWLEDGEBASE_CHANNEL_ID"))!!
val countingChannel = guild.getTextChannelById(Environment.get("COUNTING_CHANNEL_ID"))!!

val upvoteEmoji = Emoji.fromCustom("upvote", Environment.get("UPVOTE_EMOJI_ID").toLong(), false)
val downvoteEmoji = Emoji.fromCustom("downvote", Environment.get("DOWNVOTE_EMOJI_ID").toLong(), false)
Expand Down
106 changes: 106 additions & 0 deletions src/main/kotlin/com/learnspigot/bot/counting/CountingListener.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.learnspigot.bot.counting

import com.learnspigot.bot.Environment
import com.learnspigot.bot.Server
import com.learnspigot.bot.profile.ProfileRegistry
import com.learnspigot.bot.util.Mongo
import com.mongodb.client.model.Filters
import gg.flyte.neptune.annotation.Inject
import me.superpenguin.mathevaluator.Evaluator
import net.dv8tion.jda.api.entities.Message
import net.dv8tion.jda.api.entities.User
import net.dv8tion.jda.api.entities.channel.Channel
import net.dv8tion.jda.api.events.message.MessageDeleteEvent
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
import net.dv8tion.jda.api.events.message.MessageUpdateEvent
import net.dv8tion.jda.api.hooks.ListenerAdapter
import org.bson.Document

class CountingListener: ListenerAdapter() {

@Inject private lateinit var profileRegistry: ProfileRegistry

private val mongoCollection = Mongo.countingCollection

var topServerCount: Int = 0
var serverTotalCounts: Int = 0
var currentCount = 0

var lastCount: Message? = null

init {
val document = mongoCollection.find().first()
if (document == null) {
val newDoc = Document()
newDoc["highestCount"] = 0
newDoc["currentCount"] = 0
newDoc["serverTotalCounts"] = 0
mongoCollection.insertOne(newDoc)
} else {
topServerCount = document.getInteger("highestCount", 0)
currentCount = document.getInteger("currentCount", 0)
serverTotalCounts = document.getInteger("serverTotalCounts", 0)
}
}

fun incrementCount(user: User) {
currentCount++
serverTotalCounts++
profileRegistry.findByUser(user).incrementCount(currentCount)
if (currentCount > topServerCount) topServerCount = currentCount
val newDoc = mongoCollection.find().first()!!
newDoc["highestCount"] = topServerCount
newDoc["currentCount"] = currentCount
newDoc["serverTotalCounts"] = serverTotalCounts
mongoCollection.replaceOne(Filters.eq("_id", newDoc.getObjectId("_id")), newDoc)
}

fun fuckedUp(user: User) {
currentCount = 0
lastCount = null
profileRegistry.findByUser(user).fuckedUpCounting()
}

private fun Channel.isCounting() = id == Environment.get("COUNTING_CHANNEL_ID")

override fun onMessageReceived(event: MessageReceivedEvent) {
if (event.author.isBot || !event.isFromGuild || !event.channel.isCounting() || event.guild.id != Server.guildId) return
if (event.message.embeds.isNotEmpty()) return

val msg = event.message.contentRaw
val userId = event.author.id
if (Evaluator.isValidSyntax(msg)) {
val evaluated = Evaluator.eval(msg).intValue()
if (evaluated == currentCount + 1) {
if (userId.equals(lastCount?.author?.id, true)) return run {
event.message.addReaction(Server.downvoteEmoji)
event.message.reply("You can't count twice in a row, let someone else join in!").queue()
fuckedUp(event.author)
}
lastCount = event.message
event.message.addReaction(Server.upvoteEmoji).queue()
incrementCount(event.author)
} else {
val next = currentCount + 1
fuckedUp(event.author)
event.message.addReaction(Server.downvoteEmoji).queue()
event.message.reply("The next number was $next").queue()
}
}
}

override fun onMessageDelete(event: MessageDeleteEvent) {
if (!event.channel.isCounting()) return
if (event.messageId == lastCount?.id) {
Server.countingChannel.sendMessage("${lastCount?.author?.asMention} deleted their count of $currentCount").queue()
}
}

override fun onMessageUpdate(event: MessageUpdateEvent) {
if (!event.channel.isCounting()) return
if (event.messageId == lastCount?.id) {
Server.countingChannel.sendMessage("${event.author.asMention} edited their count of $currentCount").queue()
}
}

}
27 changes: 26 additions & 1 deletion src/main/kotlin/com/learnspigot/bot/profile/Profile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ data class Profile(
var udemyProfileUrl: String?,
val reputation: NavigableMap<Int, Reputation>,
val notifyOnRep: Boolean,
var intellijKeyGiven: Boolean
var intellijKeyGiven: Boolean,
var highestCount: Int,
var totalCounts: Int,
var countingFuckUps: Int
) {

fun addReputation(user: User, fromUserId: String, fromPostId: String, amount: Int) {
Expand Down Expand Up @@ -58,7 +61,29 @@ data class Profile(
document["reputation"] = reputationDocument
document["notifyOnRep"] = notifyOnRep
document["intellijKeyGiven"] = intellijKeyGiven
document["highestCount"] = highestCount
document["totalCounts"] = totalCounts
document["countingFuckUps"] = countingFuckUps
Mongo.userCollection.replaceOne(Filters.eq("_id", id), document, ReplaceOptions().upsert(true))
}

fun incrementCount(currentCount: Int) {
totalCounts++
if (currentCount > highestCount) highestCount = currentCount
saveCounting()
}

fun fuckedUpCounting() {
countingFuckUps++
saveCounting()
}

private fun saveCounting() {
val doc = Mongo.userCollection.find(Filters.eq("_id", id)).first()!!
doc["highestCount"] = highestCount
doc["totalCounts"] = totalCounts
doc["countingFuckUps"] = countingFuckUps
Mongo.userCollection.replaceOne(Filters.eq("_id", id), doc, ReplaceOptions().upsert(true))
}

}
15 changes: 11 additions & 4 deletions src/main/kotlin/com/learnspigot/bot/profile/ProfileRegistry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.learnspigot.bot.util.Mongo
import net.dv8tion.jda.api.entities.Message
import net.dv8tion.jda.api.entities.User
import org.bson.Document
import java.net.URL
import java.util.*

class ProfileRegistry {
Expand All @@ -32,8 +31,12 @@ class ProfileRegistry {
document.getString("tag"),
document.getString("udemyProfileUrl"),
reputation,
document.getBoolean("notifyOnRep") ?: true,
document.getBoolean("intellijKeyGiven") ?: false).let {
document.getBoolean("notifyOnRep", true),
document.getBoolean("intellijKeyGiven", false),
document.getInteger("highestCount", 0),
document.getInteger("totalCounts", 0),
document.getInteger("countingFuckUps", 0)
).let {
profileCache[it.id] = it
if (it.udemyProfileUrl != null)
urlProfiles[it.udemyProfileUrl!!] = it
Expand Down Expand Up @@ -64,7 +67,11 @@ class ProfileRegistry {
null,
TreeMap(),
true,
false).apply {
false,
0,
0,
0,
).apply {
profileCache[user.id] = this
save()
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/com/learnspigot/bot/util/Mongo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ object Mongo {
val userCollection: MongoCollection<Document> = database.getCollection("users")
val starboardCollection: MongoCollection<Document> = database.getCollection("starboard")
val docsCollection: MongoCollection<Document> = database.getCollection("spigot-docs")
val countingCollection: MongoCollection<Document> = database.getCollection("counting")


}

0 comments on commit 95c7ef2

Please sign in to comment.