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
#how to create custom chat commands?
1 messages · Page 1 of 1 (latest)
What do you have so far? (In terms of code)
just to see if i could get something working i just took the code as is off the website
Well if you're on the latest version of mc (1.20.50) you'll have to update the version from 1.6.0-beta to 1.8.0-beta in the dependencies section of the manifest like this:
"dependencies": [
{
// Minecraft native module - needed to use the "@minecraft/server" module
"module_name": "@minecraft/server",
"version": "1.8.0-beta"
}
]
oooh ok im sure that’s the issue
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.
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;
}
}
});
awesome! thank you!
:D
thank you!
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
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
Bing AI is not very good at the Minecraft Script API but you can use it
I can assist you!
i hate asking for help, if you're sure
is this something doable?
where i do not have to type the entire ign
my question is "is it possible if i have a player named ruralnoobkiller9457 and i type :ban rural to have it work instead of typing their entire ign?"
Hmm, I'll try something
i'll try to make a ban.js with AI and you can give me feedback?
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
awesome!
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
system.executeCommand 💀💀💀
i take it that's bs? 🤣
Yes 💀
LMFAO
Give it the typings and see what it does
hm?
The auto fill for vs code
i should be using vscode and not notepad probably
let me open that
npm
Contains many types related to manipulating a Minecraft world, including entities, blocks, dimensions, and more.. Latest version: 1.7.0, last published: 2 days ago. Start using @minecraft/server in your project by running npm i @minecraft/server. There are 13 other projects in the npm registry using @minecraft/server.
Give it snippets of related code 💀
good idea
🙌
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
supposedly tag the player after :ban with the tag doBan and tell me i banned them
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;
}
});
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
The module that imports the various classes is not mojang-minecraft.
It is currently @minecraft/server
//For example
import { world } from '@minecraft/server';
do i need to import commands as welll
sorry i wrote this with bing ai and have no prior scripting knowledge
🤣
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
ah
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?
Sup
I don't think I can help you with the scripting I do just RP and deferred
dang
@elder mist you got any more ideas?
he did give you a working code
yes but i dont know what i need in the other files
hooray
how do i actually get it to work though
how do i actually use this script?
i sound really dumb i am im so sorry
You require a pack!
THANK YOU SENOR