Most of a script's work is reacting to what the game says. Here you'll build the smallest useful example: a script that writes every chat line the bot receives to the bot's console.
Test it on a MITM bot (man-in-the-middle: the bot relays the game session of the Dofus client you play). A Full socket bot doesn't get into the game until a connection script takes it there, and chat only starts once a character is in game. Your first bot sets up a MITM bot.
Find the message
Before you write a handler, you need the message's name and the names of its fields. The Network tool shows both.
- On your MITM bot's page, open Tools > Network > Settings.
- Type
Chatin the filter box and tickChatChannelMessageEvent. - Click Apply. A toast says Now mirroring 1 message type(s).
- Open Traffic, then say something in the Dofus chat.
- A
ChatChannelMessageEventrow appears: the server sends your own line back to you, as it sends everyone else's. Click the row to see its fields.
The fields have the names a script reads, in camelCase: content is the text, senderName the speaker's character name, and channel the channel it was said on, as a name such as GLOBAL. There's one difference: 64-bit numbers such as senderCharacterId show in quotes here, while a script receives them as BigInt values. The Dofus protocol reference says what every field means.
The Live chat switch on the bot's Console tab turns on this same message: both switches are one setting.
Write the handler
Replace the contents of index.js with:
import { session, botInfo } from "asterobot:bot";
import { on, send } from "asterobot:protocol";
export default async function behavior(launch) {
on("ChatChannelMessageEvent", (traffic) => {
// A message Asterobot couldn't decode has no payload, and an error
// thrown in a handler stops the whole script.
if (!traffic.payload) return;
const { channel, senderName, content } = traffic.payload;
botInfo(`[${channel}] ${senderName}: ${content}`);
});
// A new game connection starts with this message. MITM bots never launch
// with "initial": the Dofus client has already identified their session.
if (launch.reason === "initial") {
await send("IdentificationRequest", {
ticketKey: session.gameToken,
languageCode: session.language,
});
}
}
on("ChatChannelMessageEvent", handler) registers handler for every inbound message with that name. It returns immediately, and the handler then runs each time such a message arrives, until the script stops. That's why the default export can finish right after registering it.
The handler receives each message as a traffic object, and traffic.payload holds its fields. The check at the top of the handler matters: a handler that throws an error stops the whole script, and a message Asterobot couldn't decode has no payload to read.
The handler is registered before the identification on purpose. Registering a handler sends nothing, and while send() is being awaited, the messages that arrive already reach the handlers registered before it.
on() also takes "*" to receive every inbound message, or a function that decides which messages to keep. Listening covers those options, the order handlers run in, and off().
Test it
- In the editor, pick your MITM bot and click Run.
- On the bot's page, open Console. In Sources, keep only Script.
- Say something in the Dofus chat.
A line such as [GLOBAL] Airelle: hello appears in the console.
With Live chat on and Chat in Sources, the same line also shows as a chat line: one comes from Asteroboard's live chat, the other from your script. Your script receives chat lines either way, because what you select in the Network tool only changes what Asteroboard shows.
Next, Send messages makes the script answer. Traffic lists every property of a traffic object.
Aucun avis à afficher.