Bots: one click on Trade accepts a trade with a bot (SIN's fix)

SIN's Bot Trading fix for 0.1.36, as he wrote it. The add-on puts its
own button over the stock Trade button; when a bot is the trader it
asks the server for a view of the trade, and a click sends that view
back, so the server accepts only the trade the player saw. Solo
players send the request as a hidden whisper to themselves; grouped
players use the add-on channel.

Code by SIN (1amcoff1n). Applied unchanged; only the add-on version
is set to 0.6.22.
This commit is contained in:
Karrz1
2026-11-16 20:48:01 +00:00
parent 3e12e201a3
commit 1aed7f509d
4 changed files with 166 additions and 4 deletions
@@ -2,7 +2,7 @@
## Title: WoW Forever Bots
## Notes: One-click PlayerbotV2 companions: add a bot of any class and role, call, dismiss and command your bots.
## Author: Karrz
## Version: 0.6.21
## Version: 0.6.22
## IconTexture: Interface\AddOns\ForeverBots\Textures\mono_fb64.tga
## SavedVariables: ForeverBotsDB
@@ -0,0 +1,105 @@
local _, ns = ...
local net = ns.Net
net.ANSWERS.BOT_TRADE_RESP = true
local events = CreateFrame("Frame")
local version, open, button, token, accepted = 0, false, nil, nil, false
local function stockButton()
return TradeFrameTradeButton or (TradeFrame and TradeFrame.TradeButton)
end
-- Solo clients on this branch can drop addon whispers. A normal whisper to
-- oneself carries only the request. The server answers privately via PBC.
local function request(kind, fields, cb)
if IsInGroup() then return net.Request(kind, fields, cb) end
net.seq = (net.seq % 0x7ffffffe) + 1
local seq = net.seq
net.pending[seq] = { cb = cb, mtype = kind, at = GetTime() }
local send = SendChatMessage or (C_ChatInfo and C_ChatInfo.SendChatMessage)
if not send or not pcall(send, string.format("FBTrade %x %s", seq, fields[1] or "view"),
"WHISPER", nil, UnitName("player")) then
net.pending[seq] = nil
cb(nil)
end
end
local function hideProtocol(_, _, message, sender)
return sender and Ambiguate(sender, "short") == UnitName("player") and
message:match("^FBTrade %x+ [%w:]+$") ~= nil
end
ChatFrame_AddMessageEventFilter("CHAT_MSG_WHISPER", hideProtocol)
ChatFrame_AddMessageEventFilter("CHAT_MSG_WHISPER_INFORM", hideProtocol)
local refresh
local function click()
if not open or not token then return end
if accepted then
CancelTradeAccept()
refresh()
return
end
local clicked, v = token, version
token = nil
button:Disable()
request("BOT_TRADE_ACCEPT", {clicked}, function(fields, kind)
if not open or v ~= version then return end
if not fields or kind ~= "BOT_TRADE_RESP" or fields[1] ~= "submitted" then
UIErrorsFrame:AddMessage(ERR_TRADE_REQUEST_CHANGED or "Please review the trade again.", 1, 0.6, 0)
end
refresh()
end)
end
local function ensureButton(stock)
if button then return end
button = CreateFrame("Button", nil, stock:GetParent(), "UIPanelButtonTemplate")
button:SetAllPoints(stock)
button:SetFrameLevel(stock:GetFrameLevel() + 5)
button:SetScript("OnClick", click)
end
refresh = function()
version = version + 1
local v = version
token = nil
if button and button:IsShown() then button:Disable() end
if not open then
if button then button:Hide() end
return
end
local stock = stockButton()
if stock and stock:IsVisible() then
ensureButton(stock)
button:SetText(stock:GetText())
button:Disable()
button:Show()
end
C_Timer.After(0.12, function()
if v ~= version or not open then return end
request("BOT_TRADE_VIEW", {}, function(fields, kind)
if v ~= version or not open then return end
local stock = stockButton()
local partner = UnitName("NPC") or (TradeFrameRecipientNameText and TradeFrameRecipientNameText:GetText())
if kind ~= "BOT_TRADE_RESP" or not fields or fields[1] ~= "ready" or
partner ~= fields[3] or not stock or not stock:IsVisible() then
if button then button:Hide() end
return
end
ensureButton(stock)
token, accepted = fields[2], fields[4] == "1"
button:SetText(accepted and (CANCEL or "Cancel") or stock:GetText())
button:SetEnabled(accepted or stock:IsEnabled())
button:Show()
end)
end)
end
for _, event in ipairs({"TRADE_SHOW", "TRADE_CLOSED", "TRADE_PLAYER_ITEM_CHANGED",
"TRADE_TARGET_ITEM_CHANGED", "TRADE_MONEY_CHANGED", "TRADE_ACCEPT_UPDATE"}) do
events:RegisterEvent(event)
end
events:SetScript("OnEvent", function(_, event)
if event == "TRADE_SHOW" then open = true end
if event == "TRADE_CLOSED" then open = false end
refresh()
end)
@@ -1,3 +1,4 @@
#include "Session/AddonControl.h"
// Playerbot V2 - script registration
// The module hangs off ScriptMgr: every core event it reacts to arrives through a WorldScript / PlayerScript /
// UnitScript / GroupScript / GuildScript hook and is forwarded to the matching Module handler. AddSC_playerbot_v2 is
@@ -242,6 +243,21 @@ public:
void OnChatSent(Player* player, uint32 /*type*/, uint32 /*lang*/, std::string const& msg, Player* receiver) override
{
if (player == receiver && msg.starts_with("FBTrade ") && msg.size() < 140)
{
auto split = msg.find(' ', 8);
if (split == std::string::npos)
return;
std::string seq = msg.substr(8, split - 8);
if (seq.empty() || seq.size() > 8 || seq.find_first_not_of("0123456789abcdef") != std::string::npos)
return;
std::string token = msg.substr(split + 1);
if (token != "view" && (token.empty() || token.find_first_not_of("0123456789:") != std::string::npos))
return;
Playerbot::V2::AddonControl::OnAddonWhisper(player, receiver, "PBC", "1|" + seq +
(token == "view" ? "|1|1|BOT_TRADE_VIEW|" : "|1|1|BOT_TRADE_ACCEPT|" + token));
return;
}
Module::instance().OnWhisperReceived(player, receiver, msg);
}
@@ -1,4 +1,4 @@
#include "AddonControl.h"
#include "AddonControl.h"
#include "Bot/BotAI.h"
#include "Bot/BotCommandParser.h"
@@ -39,6 +39,8 @@
#include "RBAC.h"
#include "World.h"
#include "WorldSession.h"
#include "TradeData.h"
#include "TradePackets.h"
#include <array>
#include <cctype>
@@ -202,7 +204,7 @@ void SendAddonRaw(WorldSession* sess, std::string const& body)
if (!sess) return;
Player* p = sess->GetPlayer();
if (!p) return;
if (SysReply(sess->GetAccountId()))
if (SysReply(sess->GetAccountId()) || body.find("|1|1|BOT_TRADE_RESP|") != std::string::npos)
{
WorldPackets::Chat::Chat sys;
sys.Initialize(CHAT_MSG_SYSTEM, LANG_UNIVERSAL, nullptr, nullptr, std::string(kSysMarker) + body);
@@ -1595,13 +1597,52 @@ void HandleLogoutReq(uint32 /*client_seq*/, WorldSession* sess,
// ----------------------------------------------------------------------------
// Main dispatcher. Called once a complete logical frame is assembled.
// ----------------------------------------------------------------------------
// Snapshot both offers and the unique trade window. Delayed addon messages
// cannot authorize a different trade or an offer that changed after the click.
void HandleBotTrade(WorldSession* session, uint32 seq, bool commit, std::vector<std::string> const& fields)
{
Player* player = session->GetPlayer();
TradeData* mine = player ? player->GetTradeData() : nullptr;
Player* partner = mine ? mine->GetTrader() : nullptr;
TradeData* theirs = partner ? partner->GetTradeData() : nullptr;
if (!theirs || theirs->GetTrader() != player || !Services::Initialized() ||
!Services::SessionMgr().IsHeadless(partner->GetGUID()))
{
SendFields(session, seq, "BOT_TRADE_RESP", {"unavailable"});
return;
}
std::string token = std::to_string(mine->GetSerial()) + ":" +
std::to_string(mine->GetServerStateIndex()) + ":" + std::to_string(theirs->GetServerStateIndex());
if (!commit)
{
SendFields(session, seq, "BOT_TRADE_RESP", {"ready", token, std::string(partner->GetName()), mine->IsAccepted() ? "1" : "0"});
return;
}
if (fields.size() != 1 || fields.front() != token || mine->IsAccepted() ||
mine->IsInAcceptProcess() || theirs->IsInAcceptProcess())
{
SendFields(session, seq, "BOT_TRADE_RESP", {"changed"});
return;
}
WorldPacket packet(CMSG_ACCEPT_TRADE);
packet << theirs->GetServerStateIndex();
WorldPackets::Trade::AcceptTrade accept(std::move(packet));
accept.Read();
session->HandleAcceptTradeOpcode(accept);
SendFields(session, seq, "BOT_TRADE_RESP", {"submitted"});
}
void DispatchAssembled(WorldSession* sess, uint32 seq,
std::string const& mtype, std::string const& body)
{
TC_LOG_DEBUG("playerbot.v2", "[addon_pbc] {} {} seq {:x} ({} bytes)",
sess->GetPlayer() ? sess->GetPlayer()->GetName() : std::string("?"), mtype, seq, body.size());
auto fields = SplitPipes(body);
if (mtype == "ROSTER_REQ")
if (mtype == "BOT_TRADE_VIEW" || mtype == "BOT_TRADE_ACCEPT")
HandleBotTrade(sess, seq, mtype == "BOT_TRADE_ACCEPT", fields);
else if (mtype == "ROSTER_REQ")
{
const uint32 flags = fields.empty() ? 15u
: uint32(std::strtoul(fields[0].c_str(), nullptr, 10));