Skip to content

BotMessageEvent

Triggered when a bot gets a text that a player would see: a chat message, a system message, a title, a subtitle, or a text above the hotbar.

A bot has no screen, so this event is how a spell reads what a bot is shown, for example to play-test a mini-game: does the player get the title, does the whisper arrive, what does the command answer. It fires once for each bot that gets the text, so a chat message that three bots receive fires three times. kind tells where the player would see the text.

message is the bare text, without colors and formatting. For the chat kinds it is what was written, without the name in front of it; the name is in senderName, and sender is who wrote it: a player, another entity that a command ran as, like a zombie with /execute as ... run say, or a spell, as for ChatMessageEvent. A bot also gets what it says itself, as every player does. It doesn't get an event for the copy of its own whisper or team message that the game shows the one who wrote it.

This event cannot be canceled.

Example: Wait for a title

local bot = Bot:spawn("Bob", spell.pos)
local queue = spell:collect("BotMessageEvent")
spell:execute('title Bob title "Go!"')
local evt = queue:next(20) --[[@as BotMessageEvent?]]
if evt and evt.kind == "title" then
  print(evt.bot.name .. " sees the title " .. evt.message)
end

Example: A bot that answers a whisper

local queue = spell:collect("BotMessageEvent")
while true do
  local evt = queue:next() --[[@as BotMessageEvent]]
  if evt.kind == "whisper" and instanceOf(Player, evt.sender) then
    evt.bot:execute("msg " .. evt.sender.name .. " You said: " .. evt.message)
  end
end

Fields

bot: Bot — Read-Only

A reference to the bot that got the text.

kind: BotMessageKind — Read-Only

Where a player would see the text.

message: string — Read-Only

The text, without colors and formatting.

name: string — Read-Only

The name of the event.

sender: Entity|Spell|nil — Read-Only

A reference to who sent the message, for the chat kinds: the player, or the entity that a command like /say ran as, or the spell that ran it. nil for the console, a command block, a player who has left, and the other kinds. A message that a player wrote in the chat on a server in offline mode comes with a name only: then this is the player who is shown in the chat under that name.

senderName: string? — Read-Only

The name the message came with, for the chat kinds: the player's name, or "Server", or the name of a spell. nil for the other kinds.