mirror of
https://gitlab.com/2009scape/2009scape.git
synced 2025-12-09 16:45:44 -07:00
Discord GE offer notifications via webhook
This is disabled by default unless server config server.discord_webhook is set to a discord webhook URL
This commit is contained in:
parent
ce14aa0e80
commit
6d12982b76
6 changed files with 145 additions and 3 deletions
|
|
@ -2,8 +2,8 @@
|
|||
<configuration default="false" name="[Linux] Run Server" type="ShConfigurationType">
|
||||
<option name="SCRIPT_TEXT" value="" />
|
||||
<option name="INDEPENDENT_SCRIPT_PATH" value="true" />
|
||||
<option name="SCRIPT_PATH" value="$PROJECT_DIR$/run-server.sh" />
|
||||
<option name="SCRIPT_OPTIONS" value="" />
|
||||
<option name="SCRIPT_PATH" value="$PROJECT_DIR$/run" />
|
||||
<option name="SCRIPT_OPTIONS" value="g" />
|
||||
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true" />
|
||||
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$" />
|
||||
<option name="INDEPENDENT_INTERPRETER_PATH" value="true" />
|
||||
|
|
|
|||
128
Server/src/main/kotlin/discord/Discord.kt
Normal file
128
Server/src/main/kotlin/discord/Discord.kt
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
package discord
|
||||
|
||||
import api.getItemName
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import org.json.simple.JSONArray
|
||||
import org.json.simple.JSONObject
|
||||
import rs09.ServerConstants
|
||||
import java.io.BufferedReader
|
||||
import java.io.DataOutputStream
|
||||
import java.io.InputStreamReader
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.URL
|
||||
|
||||
|
||||
object Discord {
|
||||
private const val COLOR_NEW_BUY_OFFER = 47789
|
||||
private const val COLOR_NEW_SALE_OFFER = 5752709
|
||||
private const val COLOR_OFFER_UPDATE = 15588691
|
||||
|
||||
|
||||
fun postNewOffer(isSale: Boolean, itemId: Int, value: Int, qty: Int, user: String) {
|
||||
if (ServerConstants.DISCORD_GE_WEBHOOK.isEmpty()) return
|
||||
GlobalScope.launch {
|
||||
val offer = encodeOfferJson(isSale, itemId, value, qty, user)
|
||||
try {
|
||||
sendJsonPost(offer)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun postOfferUpdate(isSale: Boolean, itemId: Int, value: Int, amtLeft: Int) {
|
||||
if (ServerConstants.DISCORD_GE_WEBHOOK.isEmpty()) return
|
||||
GlobalScope.launch {
|
||||
val offer = encodeUpdateJson(isSale, itemId, value, amtLeft)
|
||||
try {
|
||||
sendJsonPost(offer)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun encodeUpdateJson(sale: Boolean, itemId: Int, value: Int, amtLeft: Int): String {
|
||||
val obj = JSONObject()
|
||||
val embeds = JSONArray()
|
||||
val embed = JSONObject()
|
||||
|
||||
val fields = arrayOf(
|
||||
EmbedField("Item", getItemName(itemId), false),
|
||||
EmbedField("Amount Remaining", "%,d".format(amtLeft), true),
|
||||
EmbedField("Price", "%,d".format(value) + "gp", true),
|
||||
)
|
||||
|
||||
embed["title"] = if (sale) "Sell Offer Updated" else "Buy Offer Updated"
|
||||
embed["color"] = COLOR_OFFER_UPDATE
|
||||
embed["thumbnail"] = getItemImage(itemId)
|
||||
embed["fields"] = getFields(fields)
|
||||
|
||||
embeds.add(embed)
|
||||
obj["embeds"] = embeds
|
||||
|
||||
return obj.toJSONString()
|
||||
}
|
||||
|
||||
private fun encodeOfferJson(isSale: Boolean, itemId: Int, value: Int, qty: Int, user: String): String {
|
||||
val obj = JSONObject()
|
||||
val embeds = JSONArray()
|
||||
val embed = JSONObject()
|
||||
|
||||
val fields = arrayOf(
|
||||
EmbedField("Player", user, false),
|
||||
EmbedField("Item", getItemName(itemId), false),
|
||||
EmbedField("Amount", "%,d".format(qty), true),
|
||||
EmbedField("Price", "%,d".format(value) + "gp", true),
|
||||
)
|
||||
|
||||
embed["title"] = if (isSale) "New Sell Offer" else "New Buy Offer"
|
||||
embed["color"] = if (isSale) COLOR_NEW_SALE_OFFER else COLOR_NEW_BUY_OFFER
|
||||
embed["thumbnail"] = getItemImage(itemId)
|
||||
embed["fields"] = getFields(fields)
|
||||
|
||||
embeds.add(embed)
|
||||
obj["embeds"] = embeds
|
||||
|
||||
return obj.toJSONString()
|
||||
}
|
||||
|
||||
private fun getFields(fields: Array<EmbedField>): JSONArray {
|
||||
val arr = JSONArray()
|
||||
|
||||
for (field in fields) {
|
||||
val o = JSONObject()
|
||||
o["name"] = field.name
|
||||
o["value"] = field.value
|
||||
if (field.inline) o["inline"] = true
|
||||
arr.add(o)
|
||||
}
|
||||
|
||||
return arr
|
||||
}
|
||||
|
||||
data class EmbedField(val name: String, val value: String, val inline: Boolean)
|
||||
|
||||
fun getItemImage(id: Int) : JSONObject {
|
||||
val obj = JSONObject()
|
||||
obj["url"] = "https://github.com/2009scape/2009scape.github.io/raw/master/services/m%3Ddata/img/items/$id.png"
|
||||
return obj
|
||||
}
|
||||
|
||||
private fun sendJsonPost(data: String) {
|
||||
val conn = URL(ServerConstants.DISCORD_GE_WEBHOOK).openConnection() as HttpURLConnection
|
||||
conn.doOutput = true
|
||||
conn.requestMethod = "POST"
|
||||
conn.setRequestProperty("Content-Type", "application/json")
|
||||
conn.useCaches = false
|
||||
|
||||
DataOutputStream(conn.outputStream).use { it.writeBytes(data) }
|
||||
BufferedReader(InputStreamReader(conn.inputStream)).use { br ->
|
||||
var line: String?
|
||||
while (br.readLine().also { line = it } != null) {
|
||||
println(line)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -211,5 +211,8 @@ class ServerConstants {
|
|||
|
||||
@JvmField
|
||||
var DAILY_RESTART = false
|
||||
|
||||
@JvmField
|
||||
var DISCORD_GE_WEBHOOK = ""
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import core.game.ge.OfferState
|
|||
import core.game.node.entity.player.Player
|
||||
import core.game.node.entity.player.info.PlayerDetails
|
||||
import core.game.node.entity.player.link.audio.Audio
|
||||
import discord.Discord
|
||||
import rs09.ServerConstants
|
||||
import rs09.game.system.SystemLogger
|
||||
import rs09.game.system.command.Privilege
|
||||
|
|
@ -298,6 +299,14 @@ class GrandExchange : StartupListener, Commands {
|
|||
if(canUpdatePriceIndex(seller, buyer))
|
||||
PriceIndex.addTrade(offer.itemID, amount, (totalCoinXC / amount))
|
||||
|
||||
if (seller.amountLeft > 0) {
|
||||
Discord.postOfferUpdate(true, seller.itemID, seller.offeredValue, seller.amountLeft)
|
||||
}
|
||||
|
||||
if (buyer.amountLeft > 0) {
|
||||
Discord.postOfferUpdate(false, buyer.itemID, buyer.offeredValue, buyer.amountLeft)
|
||||
}
|
||||
|
||||
seller.update()
|
||||
val sellerPlayer = Repository.uid_map[seller.playerUID]
|
||||
GrandExchangeRecords.getInstance(sellerPlayer).visualizeRecords()
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ import core.net.packet.context.ContainerContext
|
|||
import core.net.packet.context.GrandExchangeContext
|
||||
import core.net.packet.out.ContainerPacket
|
||||
import core.net.packet.out.GrandExchangePacket
|
||||
import rs09.game.system.SystemLogger
|
||||
import discord.Discord
|
||||
import rs09.game.world.repository.Repository
|
||||
import java.sql.ResultSet
|
||||
|
||||
|
|
@ -144,6 +144,7 @@ class GrandExchangeOffer() {
|
|||
uid = nowuid.getLong(1)
|
||||
visualize(player)
|
||||
stmt.close()
|
||||
Discord.postNewOffer(sell, itemID, offeredValue, amount, player?.username ?: "Unknown")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -116,6 +116,7 @@ object ServerConfigParser {
|
|||
ServerConstants.RULES_AND_INFO_ENABLED = data.getBoolean("world.show_rules", true)
|
||||
ServerConstants.BOTS_INFLUENCE_PRICE_INDEX = data.getBoolean("world.bots_influence_ge_price", true)
|
||||
ServerConstants.REVENANT_POPULATION = data.getLong("world.revenant_population", 30L).toInt()
|
||||
ServerConstants.DISCORD_GE_WEBHOOK = data.getString("server.discord_webhook", "")
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue