#how to create custom chat commands?

1 messages · Page 1 of 1 (latest)

ancient blade
#

i was hoping to create a way where if i do something like :ban playernamehere in chat it tags that player with a tag only if i have the admin tag, i was following this guide here but it does not seem to work even for the template https://wiki.bedrock.dev/scripting/custom-command.html

icy slate
#

What do you have so far? (In terms of code)

ancient blade
icy slate
ancient blade
#

how can i discover thats the problem in the future

icy slate
# ancient blade how can i discover thats the problem in the future

When ever there is a new update it's a good idea to check the changelog to see if any changes were made to the api especially when using the beta version of it. But also you can always download the newest addon sample pack for the current version from Microsoft here: https://aka.ms/MCAddonPacks

Once you download the samples pack you can check this folder: ..\bedrock-samples-1.20.50.3\metadata\script_modules\@minecraft
and in that folder you'll see the available versions of the api.

In the case of 1.20.50 the folder doesnt have server_1.6.0-beta.json anymore the folder now has server_1.8.0-beta.json so that means you'll have to use 1.8.0-beta instead of 1.6.0-beta now.

GitHub

Samples and assets for building Minecraft: Bedrock Edition add-ons - Mojang/bedrock-samples

elder mist
#
import { world, system } from '@minecraft/server';

const prefix = ':';

world.beforeEvents.chatSend.subscribe((eventData) => {
    const player = eventData.sender;
    const message = eventData.message;

    if (message.startsWith(prefix)) {
        const args = message.slice(prefix.length).split(/\s+/g);
        const command = args.shift();

        switch (command) {
            case 'ban':
                eventData.cancel = true;
                if (!player.hasTag('myAdminTag')) return;
                system.run(() => {
                    const targetPlayer = world.getAllPlayers().find(plr => plr.name === args[0]);
                    if (!targetPlayer) return player.sendMessage(`Invalid player!`);
                    player.sendMessage(`Player ${args[0]} was banned!`)
                    targetPlayer.addTag('banned');
                })
                break;
            default:
                player.sendMessage('Invalid command!')
                break;
        }
    }
});
ancient blade
#

hey @elder mist i got a question for ya

#

i was hoping to be able to keep each of my : commands in a seperate folder like this

#

is this possible?

#

or do i need to keep them all my commands in one script

elder mist
#

You can do multiple files and folders!

#

Use the import './ban.js';

ancient blade
# elder mist You can do multiple files and folders!

what i hope to end up making is a recreation of one of the first roblox admin scripts that i used to know very well,

say i have a user named ruralnoobkiller918 or whatever, typing in :ban ruralno would tag that player with ban

#

im trying to use bing ai to make the command files for me because idk what i am doing 😭

#

i do commands not this

elder mist
#

Bing AI is not very good at the Minecraft Script API but you can use it

#

I can assist you!

ancient blade
ancient blade
#

where i do not have to type the entire ign

elder mist
#

Is your question "Can I use Bing AI to write Script API code?"

#

Yes and No

ancient blade
elder mist
#

Hmm, I'll try something

ancient blade
elder mist
#

Yes

#

I have a miss typed or auto filler name

#

So if you type a part of the name it'll auto select the name

elder mist
#

I just need to get permission to distribute it

ancient blade
# elder mist I just need to get permission to distribute it

here is the ban.js bing ai made me

const system = server.registerSystem(0, 0);

// Setup which events to listen for
system.initialize = function () {
    system.listenForEvent("minecraft:chat", onChat);
};

// Event listener function
function onChat(eventData) {
    let message = eventData.data.message;
    let sender = eventData.data.sender;
    
    // Check if the message starts with ":ban "
    if (message.startsWith(":ban ")) {
        let playerName = message.slice(5); // Get the player name from the message

        // Find the player with the given name
        let playerEntities = system.getEntitiesFromQuery(system.registerQuery());
        for (let i = 0; i < playerEntities.length; i++) {
            if (playerEntities[i].name.startsWith(playerName)) {
                // Tag the player
                system.executeCommand(`tag ${playerEntities[i].id} add doBan`, () => {});

                // Send a message back to the sender
                system.executeCommand(`tellraw ${sender.id} {"rawtext":[{"text":"You banned ${playerEntities[i].name}"}]}`, () => {});
                break;
            }
        }
    }
}
#

this looks quite different from the snippet on bedrock.dev

elder mist
#

system.executeCommand 💀💀💀

ancient blade
elder mist
#

Yes 💀

ancient blade
#

LMFAO

elder mist
#

Give it the typings and see what it does

ancient blade
elder mist
#

The auto fill for vs code

ancient blade
#

let me open that

elder mist
#

Give it snippets of related code 💀

ancient blade
elder mist
#

🙌

ancient blade
#

ok

#

its typing

#

suspense

#
import { world } from '@minecraft/server';

world.beforeEvents.chatSend.subscribe((eventData) => {
    const player = eventData.sender;
    const message = eventData.message;

    if (message.startsWith(":ban ")) {
        let playerName = message.slice(5); // Get the player name from the message

        // Find the player with the given name
        let playerEntities = system.getEntitiesFromQuery(system.registerQuery());
        for (let i = 0; i < playerEntities.length; i++) {
            if (playerEntities[i].name.startsWith(playerName)) {
                // Tag the player
                player.runCommandAsync(`tag ${playerEntities[i].id} add doBan`);

                // Send a message back to the sender
                player.runCommandAsync(`tellraw ${sender.id} {"rawtext":[{"text":"You banned ${playerEntities[i].name}"}]}`);

                // Cancel the chat message
                eventData.cancel = true;
                break;
            }
        }
    }
});
#

@elder mist are you there senor

#

:(

#

HE RETURNS

elder mist
#

What is this code supposed to do?

#

ancient blade
#

supposedly tag the player after :ban with the tag doBan and tell me i banned them

elder mist
#

Try this:```js
import { world } from '@minecraft/server';

world.beforeEvents.chatSend.subscribe((eventData) => {
const player = eventData.sender;
const message = eventData.message;

if (message.startsWith(":ban ")) {
let playerName = message.slice(5); // Get the player name from the message

// Find the player with the given name
const target = world.getPlayers({ name: playerName })
// Tag the player
player.runCommandAsync(`tag ${target[0].name} add doBan`);

// Send a message back to the sender
player.runCommandAsync(`tellraw ${player.name} {"rawtext":[{"text":"You banned ${target[0].name}"}]}`);

// Cancel the chat message
eventData.cancel = true;

}
});

ancient blade
#

alrigty

#

ah

#

i got an error

#

not from your code but from mine

#

i have a scripts folder and inside that i have a main.js which is called in manifest, and then i have a commands folder

#

main.js

import { World, Commands } from 'mojang-minecraft'
import commands from './commands/import.js';

World.events.beforeChat.subscribe((chat) => {
    if (chat.message.startsWith('.')) {
        const args = chat.message.slice('.'.length).trim().split(/ +/g)
        const cmd = args.shift().toLowerCase()
        const command = commands[cmd]
        try { var tags = Commands.run(`/tag "${chat.sender.name}" list`, World.getDimension('overworld')).statusMessage } catch (e) { }
        if (tags.search(/(admin)/g) == -1) return
        chat.cancel = true
        if (command) {
            if (command.args.length > args.length) {
                Commands.run(`/tellraw "${chat.sender.name}" {"rawtext":[{"text":"§cImproper Usage: ${command.usage}"}]}`, World.getDimension('overworld'))
            } else {
                command.execute(chat, args)
            }
        } else {
            Commands.run(`/tellraw "${chat.sender.name}" {"rawtext":[{"text":"§cThe command ${cmd} doesnt exist"}]}`, World.getDimension('overworld'))
        }
    }
})
#
[Scripting][error]-Plugin [Adonis Admin v13 - 1.0.0] - [main.js] ran with error: [ReferenceError: Module [mojang-minecraft.js] not found. Native module error or file not found.]```
#

ping me when you write back

ancient blade
#

@elder mist you have died

#

😔

neon spindle
#

The module that imports the various classes is not mojang-minecraft.
It is currently @minecraft/server

//For example 
import { world } from '@minecraft/server';
ancient blade
#

sorry i wrote this with bing ai and have no prior scripting knowledge

#

🤣

neon spindle
#

To begin with, if you are importing files from several different module systems, you need to separate them.
Just as it is no good to import one file that is different from the others, it is no good to import files that are different from each other.

//For example 
import { world } from '@minecraft/server';
import { Commands } from 'commands.js';

There is no Commands class in @minecraft/server

ancient blade
#

ah

ancient blade
#

anyone got any ideas on what I can do?

#

i feel like this should be relatively simple

#

i think

#

custom chat commands

#

how to create custom chat commands?

ancient blade
#

@magic rivet

#

D:

magic rivet
#

I don't think I can help you with the scripting I do just RP and deferred

ancient blade
#

@elder mist you got any more ideas?

dense kindle
ancient blade
elder mist
#

I went to sleep, Sorry about that

#

So, Back to business 🙌

ancient blade
ancient blade
#

how do i actually get it to work though

ancient blade
#

bump

#

:(

ancient blade
#

i sound really dumb i am im so sorry

elder mist
#

You require a pack!

ancient blade
#

THANK YOU SENOR