跳至正文
版本:latest - a1.171.5 ⚖️

🪙 Tebex

在游戏内出售你 Tebex 商店中的包,并将它们发放给玩家

🗿Static ClassServer Only🧑‍💻API Source

💡 关于 Tebex​

Tebex 是一个变现平台,让玩家可以从你的商店中购买游戏内的包(等级、物品、货币等)。 nanos world 原生支持它:玩家在游戏内菜单中浏览你的商店,在 Tebex 的结账页付款,然后你的服务器发放玩家购买的内容。

本页涵盖技术方面的内容。关于为什么使用 Tebex、费用、可出售内容的规则以及你作为卖家的责任,请参阅变现。

获取密钥​

首先在你的 Tebex 创作者面板中点击 Create Project,选择 I operate a game server,然后从游戏列表中选择 nanos world。然后打开游戏服务器页面,点击 Edit(或 Instructions)以显示你的游戏服务器密钥。

请妥善保密此密钥:任何持有它的人都可以管理你的商店。

更多信息请参阅 Tebex 快速开始指南:

Tebex 快速开始指南https://docs.tebex.io/developers/getting-started

初始化​

在你的 Config.toml 的 tebex_secret 设置中设置你的密钥,或使用 --tebex_secret \<your_secret_key> 启动服务器。无需其他设置。

启动时,服务器会连接到 Tebex,记录 Tebex initialized successfully! 并开始发放购买项。玩家也会在游戏内菜单中获得你的商店。

发放购买项​

当付款被确认后,Tebex 会将你在包中配置的命令加入队列。服务器会按照 Tebex 要求的间隔(大约每分钟)拉取它们:

  • 在线命令仅在玩家在服务器上时运行。如果他们离线,命令会等待他们下次访问。
  • 离线命令在被拉取后立即运行,无论玩家是否在线。
  • 带有延迟的命令会在被拉取后的指定秒数后运行。如果服务器在它运行前重启,它会被重新拉取。

命令运行前,这些占位符会被替换:{id} 和 {steamid} 替换为玩家的 Steam ID,{username} 和 {name} 替换为玩家的名称。

默认情况下,每条命令作为你的包中的服务器控制台命令运行:即通过 Console.RegisterCommand() 注册的命令,或在 PlayerSubmit (Console) 事件中处理的命令(在那里返回 false 以标记为已处理)。内置服务器命令(例如 kick、stop)不能被 Tebex 命令调用。要改为从你的脚本中发放购买项,请订阅 OnlineCommand (Tebex) 或 OfflineCommand (Tebex) 并返回 false。

命令一旦被处理,就会从 Tebex 队列中移除,因此不会再次发放。没有任何包处理的命令(没有匹配的已注册命令,也没有事件返回 false)会留在队列中,并在下次拉取时再次尝试,同时在服务器日志中记录错误。这样,在你的包未加载时进行的购买,或者命令中有拼写错误的购买,都会在你修复后立即发放。

注意

使用 {steamid} 识别玩家,绝不要使用 {name} 或 {username}。玩家可以自己选择名称,带空格的名称会成为你命令的额外参数。

提示

使用事件发放时,避免在脚本中硬编码包 ID。改为读取 command 参数(例如 vip 30d 或 coins 1000),这样新包只需在 Tebex 面板中更改即可。

打开结账页​

玩家可以在游戏内菜单中从你的商店购买。你也可以通过 Tebex.OpenCheckoutOverlay() 从你的脚本中(NPC 商店、聊天命令、你自己的 UI)启动特定包的购买:玩家的游戏内菜单会打开该包的结账页,与商店显示的相同,并附有在浏览器中完成付款的链接。由玩家决定是否付款。

🎒 示例​

使用控制台命令​

在你的包中注册一个控制台命令,然后在 Tebex 面板中将其用作包命令,例如 giveitem {steamid} vip_rank:

Server/Index.lua
-- 在线命令仅在玩家连接时运行,因此可以找到玩家
Console.RegisterCommand("giveitem", function(steam_id, item)
local player = Player.GetBySteamID(steam_id)

if (player) then
GivePlayerItem(player, item)
end
end, "给予玩家一个物品", { "steam_id", "item" })

实现你自己的发放逻辑​

不使用任何控制台命令,订阅事件并返回 false:

Server/Index.lua
-- 在线命令:玩家已连接
Tebex.Subscribe("OnlineCommand", function(command_id, package_id, payment_id, command, player)
-- 在此处理一个特殊包
if (package_id == 123456) then
player:SetValue("vip", true, true)
Chat.BroadcastMessage(player:GetName() .. " 现在是 VIP,谢谢!")

-- 阻止该命令在控制台中运行
return false
end
end)

-- 离线命令:玩家可能未连接
Tebex.Subscribe("OfflineCommand", function(command_id, package_id, payment_id, command, tebex_player_id, player_name, player_steam_id)
UpdatePlayerDatabase(player_steam_id, package_id)

-- 阻止该命令在控制台中运行
return false
end)

通过聊天命令销售​

Server/Index.lua
-- 当玩家输入 /vip 时打开包 123456 的结账页
Chat.Subscribe("PlayerSubmit", function(message, player)
if (message == "/vip") then
Tebex.OpenCheckoutOverlay(player, 123456)
return false
end
end)

🗿 静态函数​

OpenCheckoutOverlay在游戏内菜单之上为玩家打开某个包的 Tebex 结账页
Subscribe: function订阅事件
Unsubscribe退订此特定类和此特定包中所有已订阅的事件,可选择传递函数以仅退订该回调

OpenCheckoutOverlayOptimal​

在游戏内菜单之上为玩家打开某个包的 Tebex 结账页

The server creates a basket with the package for the player's Steam ID, so the purchase is linked to that player without any login. Once Tebex answers, the player's in-game menu opens with the checkout popup, the same one the in-game store shows, and the player decides whether to pay

Tebex.OpenCheckoutOverlay(player, package_id)
Parameters
NameTypeDescription
playerPlayer将要购买该包的玩家
package_idinteger包在你 Tebex 商店中的 ID

SubscribeOptimal​

订阅事件

local ret = Tebex.Subscribe(event_name, callback)
Parameters
NameTypeDescription
event_namestring要订阅的事件
callbackfunction事件发生时运行的回调函数
Returns
TypeDescription
function函数回调

UnsubscribeOptimal​

退订此特定类和此特定包中所有已订阅的事件,可选择传递函数以仅退订该回调

Tebex.Unsubscribe(event_name, callback?)
Parameters
NameTypeDescription
event_namestring要退订的事件
callback?function用于专门退订的可选回调 Default nil

🚀 事件​

OfflineCommand当某笔购买的离线命令即将运行时调用
OnlineCommand当某笔购买的在线命令即将为一名已连接的玩家运行时调用

OfflineCommand​

当某笔购买的离线命令即将运行时调用

Offline commands run whether or not their player is on the server, so the player is given by their Steam ID and name. The {id}, {steamid}, {username} and {name} placeholders are already replaced in command.

Return false to handle the purchase in your script instead of running command in the server console. A handled command is removed from the Tebex queue. If you don't return false and no Package handles command in the console, it stays in the queue and this event is called again on the next fetch

Tebex.Subscribe("OfflineCommand", function(command_id, package_id, payment_id, command, tebex_player_id, player_name, player_steam_id)
-- OfflineCommand was called
return true
end)
Arguments
NameTypeDescription
command_idinteger该命令在 Tebex 队列中的 ID
package_idinteger所购买包的 ID
payment_idinteger付款的 ID
commandstringTebex 包中配置的命令,其中的占位符已被替换
tebex_player_idstring玩家在 Tebex 中的内部 ID
player_namestring玩家的名称
player_steam_idstring玩家的 Steam ID
Returns
TypeDescription
boolean返回 false 以阻止该命令在服务器控制台中运行

OnlineCommand​

当某笔购买的在线命令即将为一名已连接的玩家运行时调用

Online commands only run while their player is on the server. The {id}, {steamid}, {username} and {name} placeholders are already replaced in command.

Return false to handle the purchase in your script instead of running command in the server console. A handled command is removed from the Tebex queue. If you don't return false and no Package handles command in the console, it stays in the queue and this event is called again on the next fetch

Tebex.Subscribe("OnlineCommand", function(command_id, package_id, payment_id, command, player)
-- OnlineCommand was called
return true
end)
Arguments
NameTypeDescription
command_idinteger该命令在 Tebex 队列中的 ID
package_idinteger所购买包的 ID
payment_idinteger付款的 ID
commandstringTebex 包中配置的命令,其中的占位符已被替换
playerPlayer接收该笔购买的玩家
Returns
TypeDescription
boolean返回 false 以阻止该命令在服务器控制台中运行