HyperScript
Docs

← All of the Docs Data and the outside

Discord

effectsend discord message <text> to channel <id>since 1.7.20

The bot writes in that channel. The id is the long number from Discord's "Copy Channel ID". & colour codes are removed, and nobody is pinged

on player join:
    send discord message "%player% joined the server" to channel "123456789012345678"
Try it
structureon discord message <id>:since 1.7.20

Runs when somebody writes in that channel; without an id, in any channel the bot can read. {_message} is the text, {_user} who wrote it, {_user_id}, {_channel} and {_channel_id}

on discord message "123456789012345678":
    broadcast "&9[Discord] &f%{_user}%&7: %{_message}%"
Try it
effectsend webhook <name or URL>

A name from config.yml, or a full https:// URL

send webhook "alerts" with content "%player.name% joined the server" as "HyperScript"
Try it
effectwith content <text>

The message, up to 2000 characters; & colour codes are removed

send webhook "alerts" with content "%player.name% joined the server" as "HyperScript"
Try it
expressionas <text>

Optional: the name the message is posted under

send webhook "alerts" with content "%player.name% joined the server" as "HyperScript"
Try it
Guide and examples

Webhooks post a message to a Discord channel. They need no bot: create a webhook in the channel's settings, then give it a name in config.yml.

discord:
  webhooks:
    alerts: "https://discord.com/api/webhooks/123/abc"
on player join:
    send webhook "alerts" with content "%player.name% joined the server" as "HyperScript"
PartMeaning
send discord message <text> to channel <id>The bot writes in that channel. The id is the long number from Discord's "Copy Channel ID". & colour codes are removed, and nobody is pinged
on discord message <id>:Runs when somebody writes in that channel; without an id, in any channel the bot can read. {_message} is the text, {_user} who wrote it, {_user_id}, {_channel} and {_channel_id}
send webhook <name or URL>A name from config.yml, or a full https:// URL
with content <text>The message, up to 2000 characters; & colour codes are removed
as <text>Optional: the name the message is posted under
  • Sending happens in the background and never slows the server. Failures are logged as warnings.
  • Messages cannot ping @everyone, roles or users, whatever text players manage to put in them.
  • A webhook URL is a password for the channel. Prefer names from config.yml over URLs in scripts.

Slash commands let Discord users run script code. They need a bot: create an application at the Discord developer portal, add a bot, invite it to your server with the applications.commands scope, and put its token in config.yml.

discord:
  bot:
    enabled: true
    token: "your-bot-token"
    guild-id: "123456789012345678"
on discord command "/online":
    reply "%server.online_players% player(s) online: %server.players%"

on discord command "/link":
    if arg.1 is not set:
        reply "Usage: /link <code>  (get a code with /link in game)"
        stop
    if {link.code.%arg.1%} is not set:
        reply "That code is not valid."
        stop
    set {_player} to {link.code.%arg.1%}
    set {link.discord.%{_player}%} to discord.user_id
    delete {link.code.%arg.1%}
    reply "Linked to Minecraft account %{_player}%."
  • reply <text> answers the person who ran the command. Reply at least once; if a trigger ends without replying, HyperScript answers "Done." so Discord is not left waiting.
  • Every slash command has one optional text option, args. It is split on spaces into %arg.1%, %arg.2% and so on, exactly like an in-game command.
  • There is no Minecraft player in a Discord trigger, so {%player%.*} variables are unavailable. Use global variables or tables keyed by name, as the /link example does.
  • With guild-id set, commands appear in that server immediately. Without it they are registered globally, which Discord can take up to an hour to show.
  • Slash commands are registered and removed as scripts load, including on hot reload.
  • The bot uses the JDA library, which the server downloads on first start (see Installation and setup).

A bot that writes and listens. With the bot switched on (discord.bot in config.yml), send discord message lets a script write in any channel the bot is in, which a webhook cannot do without one address per channel. To hear what is written there, also set discord.bot.read-messages: true, and first switch on "Message Content Intent" for the bot in Discord's developer portal: without that, Discord refuses the connection. The bot never hears itself or other bots, so a script that answers cannot answer itself for ever.