Developers Other addons
Your commands from Discord
A world's owner can let people run Minecraft commands from Discord, and that includes your addon's own commands. Here's what your command sees when it's run that way, and how to make it work well.
How it reaches your addon
The owner allows a command by name on the world's Commands tab, and then anyone allowed can type it in Discord:
/relay run command: myaddon:option 12
BedrockRelay runs it in the world the way any script does, with Dimension.runCommand(). Your command can't tell it came from Discord. It's the same on dedicated servers and Realms, with cheats on or off. The owner can also run commands from the console on the dashboard, which works the same way.
Permission level
Scripts run commands at the GameDirectors level, so the permissionLevel you register with decides whether your command can be run from Discord at all:
| Permission level | From Discord |
|---|---|
Any | Runs |
GameDirectors | Runs |
Admin, Host, Owner | Refused, without an error |
cheatsRequired makes no difference: commands run by a script work with cheats off. If your command changes settings that only operators should touch, GameDirectors is the level to use.
There's no player behind it
Run from Discord, your command comes from the server: origin.sourceType is Server and origin.sourceEntity is undefined. A command that acts on "the player who typed it" has nobody to act on, so check rather than assume:
customCommandRegistry.registerCommand(
{
name: "myaddon:option",
description: "Sets your option.",
permissionLevel: CommandPermissionLevel.GameDirectors,
cheatsRequired: false,
mandatoryParameters: [{ name: "value", type: CustomCommandParamType.Integer }],
},
(origin, value) => {
const player = origin.sourceEntity;
if (player?.typeId !== "minecraft:player") {
return { status: CustomCommandStatus.Failure, message: "Run this as a player." };
}
system.run(() => player.setDynamicProperty("myaddon:option", value));
return { status: CustomCommandStatus.Success };
},
);
Minecraft's execute supplies the player. Run like this, origin.sourceEntity is Steve, exactly as if he'd typed it himself:
/relay run command: execute as Steve run myaddon:option 12
Commands that act on the whole world need neither.
What Discord sees
Discord is told whether the command worked, and nothing more. Minecraft gives a script a command's success, not its output, so the message you return never reaches Discord. Minecraft's own syntax errors do, such as a missing argument.
If someone in Discord needs to know what happened, post it yourself with a discord:message script event. It goes to the channels the owner chose for those messages.
Script events work too
If your addon listens for script events instead of registering commands, those can be run from Discord the same way:
/relay run command: scriptevent myaddon:reward Steve
/relay run command: execute as Steve run scriptevent myaddon:reward
Your system.afterEvents.scriptEventReceive handler gets the first with sourceType Server, and the second with Steve as sourceEntity. Both work on Realms with cheats off.
What to tell owners
Owners allow commands by name, so tell them which names to allow on the Commands tab: myaddon:option, say. To use execute as, they allow execute as well. BedrockRelay checks the command after run against the same list, so allowing execute doesn't allow anything else.
Running a Minecraft world rather than building for one? The help covers everything from setup to troubleshooting.
Read the help