ReconnectedCC Chatbox v2 module for Node.js written in TypeScript
npm i reconnectedchatIn-depth documentation can be found at docs.reconnected.cc:
This example will show you how to connect to the ReconnectedCC Chatbox API with a basic Hello World bot.
- Obtain a Chatbox token by running
/chatbox licensein-game. You can click the token to copy it to your clipboard. - Set up your project with
npm init, and install reconnectedchat by runningnpm i reconnectedchat.- To make sure ESM modules are used, open
package.jsonand set"type": "module".
- To make sure ESM modules are used, open
- Create a file called
index.jswith the following code:
// Import ReconnectedChat
import {Client} from "reconnectedchat";
// Create the Chatbox client
const sc = new Client("YOUR-CHATBOX-TOKEN", {
// Optional - set the default name and markdown formatting mode for all `.say()` and `.tell()` calls
defaultName: "Hello World Bot",
defaultFormattingMode: "markdown",
});
// Add event listener for when a backslash command is received
sc.on("command", async (cmd) => {
// Only respond if the command is `\helloworld`
if (cmd.command === "helloworld") {
// Send message back to the user: "Hello World!"
await sc.tell(cmd.user.name, "Hello World!");
}
});
// Event listener for when the client is connected to ReconnectedCC and ready to
// receive messages and events
sc.on("ready", () => {
console.log("Connected!");
});
// Connect to ReconnectedCC
sc.connect();- Run your code! If you used JavaScript you can run it with
node index.js. When it says "Connected!", run\helloworldin-game, and the Chatbox should respond to you!
You are able to listen after the following events, which directly map to the events sent by the Chatbox websocket API:
| Friendly name | Name | Trigger |
|---|---|---|
| In-game chat | chat_ingame |
A player sent a message in-game |
| Discord chat | chat_discord |
A player sent a message from Discord |
| Chatbox message | chat_chatbox |
A chatbox sent a public message |
| Chatbox command | command |
A player sent a chatbox command in-game |
| Join | join |
A player joined the server |
| Leave | leave |
A player left the server |
| Death | death |
A player died |
| World change | world_change |
A player changed worlds |
| AFK | afk |
A player went AFK |
| AFK return | afk_return |
A player returned from being AFK |
| Restart scheduled | server_restart_scheduled |
The server will restart soon |
| Restart cancelled | server_restart_cancelled |
The scheduled server restart was cancelled |
- user -
Userthat went AFK
(Websocket API docs – API reference)
The event received when a player posts a message in public chat.
- text -
stringof the contents of the chat message - rawText -
stringof the raw contents of the chat message - renderedText -
RenderedTextObjectof the rendered text of the chat message - user -
Userthat sent the message
(Websocket API docs – API reference)
The event received when a player posts a message in Discord.
- text -
stringof the contents of the chat message, without formatting codes - rawText -
stringof the raw contents of the chat message, with its original formatting codes - renderedText -
RenderedTextObjectof the rendered text of the chat message - discordId -
stringof the Discord snowflake ID of the message - discordUser -
DiscordUserthat sent the message - edited -
booleanif the message has been edited
(Websocket API docs – API reference)
The event received when another chatbox sends a message.
- text -
stringof the contents of the message, without formatting codes - rawText -
stringof the raw contents of the message, with its original formatting codes - renderedText -
RenderedTextObjectof the rendered text of the message - user -
Userof the owner of the chatbox - name -
stringname of the chatbox, without formatting codes - rawName -
stringname of the chatbox, with its original formatting codes
(Websocket API docs – API reference)
The event received when a player runs a chatbox command (public backslash commands: \command, private owner-only
caret/pipe commands: ^command) in-game. The command capability is required to receive command events.
- user -
Userthat ran the chatbox command - command - The name of the command (the word immediately following the backslash/caret/pipe, excluding the backslash/caret/pipe). Example: \command arg1 arg2
- args - Array of space-separated
stringarguments after the command. - ownerOnly -
booleanif the command is an owner-only command (^command)
(Websocket API docs – API reference)
The event received when a player joins the game.
- user -
Userthat joined the server
(Websocket API docs – API reference)
The event received when a player leaves the game.
- user -
Userthat left the server
(Websocket API docs – API reference)
The event received when a player dies in-game.
- text -
stringof the death message contents, without formatting codes - rawText -
stringof the death message contents, with its original formatting codes - renderedText -
RenderedTextObjectof the death message - user -
Userthat died - source -
User|nullthat killed the user. Only set if they were killed by another player
(Websocket API docs – API reference)
The event received when a player changes worlds.
- user -
Userthat changed worlds - origin -
stringof the world the player moved from - destination -
stringof the world the player is now in
(Websocket API docs – API reference)
The event received when a player goes AFK in-game.
- user -
Userthat goes AFK
(Websocket API docs – API reference)
The event received when a player returns from being AFK in-game.
(Websocket API docs – API reference)
The event received when a server restart has been scheduled. At the time of restartAt, the server will restart and the
ReconnectedChat client will be disconnected.
If a server restart was scheduled before the websocket connected, then the server_restart_scheduled event will be
sent after the hello/ready events. In this case, restartSeconds will not be the time until the restart, but
instead the time that was initially specified for the restart. time will still be the initial time the restart was
scheduled, and restartAt will be the time the restart will happen.
- restartType -
stringthat tells you what type of restart it is ("automatic"or"manual") - restartSeconds -
numberthat tells you how many seconds until the restart - restartAt -
Datethat tells you when the restart will happen
(Websocket API docs – API reference)
The event received when a previously scheduled server restart has now been cancelled.
- restartType -
stringthat tells you what type of restart it was ("automatic"or"manual")
The Chatbox server has a ratelimit of 500ms per license, with up to 5 messages queued at once.
When the methods client.say and client.tell are called, the data is enqueued internally before being sent to the
server.
This internal queue makes it safe to go well over the limits, because it processes each message every 500ms to avoid hitting the rate limit of the server.
You are also able to listen after these packets that are sent by ReconnectedCC to the client.
| Friendly name | Name | Trigger |
|---|---|---|
| Hello packet | ready |
When ReconnectedChat has successfully connected to ReconnectedCC |
| Player list packet | players |
When you connect, and when a player joins/leaves |
| Error packet | error |
When there is an error |
| Closing packet | closing |
When the server is closing the connection |
(Websocket API docs – API reference)
- guest -
booleanthat says if you are authenticated with the guest key - licenseOwner -
stringthat represents who owns the chatbox license - licenseOwnerUser -
Userthat represents who owns the chatbox license, or null if this is a guest connection - capabilities - Array of
strings that can tell you what capabilities you have
(Websocket API docs – API reference)
- players - Array of
Users that are currently online on the server.
(Websocket API docs – API reference)
- error -
stringthat tells you what type of error that occurred (possible errors). - text -
stringthat is a human-readable version oferror.
(Websocket API docs – API reference)
- closeReason -
stringreason for closing the connection (possible values). - reason -
stringthat is a human-readable version ofcloseReason
You can run these methods on your Client
instance.
(Websocket API docs – API reference)
- Syntax:
await client.say(text, name, mode) - Returns: Promise<Success>
Sends a message to the in-game public chat. Returns a Promise that resolves to a
Success object, which will tell you if the
message
was sent (reason is "message_sent").
| Argument | Type | Description |
|---|---|---|
text |
string |
The message to send. |
name |
string (optional) |
The name of the chatbox to show. If no name is specified, it will default to the username of the license owner, or client.defaultName. |
mode |
FormattingMode (optional) |
The formatting mode to use ("markdown", "format" or "minimessage"). Defaults to client.defaultFormattingMode or "markdown". |
(Websocket API docs – API reference)
- Syntax:
await client.tell(user, text, name, mode) - Returns: Promise<Success>
Sends a private message to an in-game player. Returns a Promise that resolves to a
Success object, which will tell you if the
message
was sent (reason is "message_sent").
| Argument | Type | Description |
|---|---|---|
user |
string, User |
The username, UUID or the User object of the user to send the message to. |
text |
string |
The message to send. |
name |
string (optional) |
The name of the chatbox to show. If no name is specified, it will default to the username of the license owner, or client.defaultName. |
mode |
FormattingMode (optional) |
The formatting mode to use ("markdown", "format" or "minimessage"). Defaults to client.defaultFormattingMode or "markdown". |
- Syntax:
client.connect()
Connects to ReconnectedCC.
- Syntax:
client.close()
Disconnects the connection to ReconnectedCC.
- Syntax:
client.reconnect()
Reconnects to ReconnectedCC.
(Websocket API docs – API reference)
The user object represents an in-game player.
- name - The Minecraft username of the player.
- uuid - The Minecraft UUID of the player, with hyphens.
- displayName - The user's name as it appears in chat. May differ from
name. - group - The rank of the player, usually
"default","moderator"or"admin". - pronouns - The pronouns set by the user by running
/pronouns. This may benullif the player has not set any preferred pronouns. Where reasonably possible, you should attempt to use the user's preferred pronouns, or avoid using pronouns entirely. If you are unable to do this, you should use the player's name instead. - world - The world the player is in, or
nullif this information is not available. It will be a Minecraft namespaced registry key, for example:minecraft:overworld- The overworldminecraft:the_nether- The Netherminecraft:the_end- The End
- afk - If the player is AFK
- alt - If the player is an alt account
- bot - If the player is a bot
- supporter - the current public tier of the player's supporter status. This will be
0if the player is not a supporter or has opted out of showing their supporter tag,1for a Tier 1 supporter,2for a Tier 2 supporter, and3for a Tier 3 supporter.
(Websocket API docs – API reference)
The Discord user object represents a user on Discord.
- id - The Discord snowflake ID of the user
- name - The Discord name of the user
- displayName - The user's server nickname on Discord, or their username if it is not set
- discriminator - The user's discriminator on Discord
- avatar - URL to the avatar of the user
- roles - Array consisting of roles.
(Websocket API docs – API reference)
Minecraft's serialised raw JSON text format. See the ReconnectedCC documentation for more information on how this is used.
The mode to use for outgoing chatbox messages (.say(), .tell()).
- markdown - Discord-like Markdown syntax. Supports URLs, but not colours.
- format - Minecraft-like formatting codes using ampersands (e.g.
&efor yellow). Supports colours, but not URLs. - minimessage - HTML-like tags (e.g.
<yellow></yellow>for yellow). Supports colours and hover events.