Package: mcbews
Minecraft Bedrock Edition WebsocketQuickstartInstallationInstall withpip install mcbewsand import it.frommcbewsimportwsfrommcbews.chatimportColor,StyleContentsCreate a simple botMinecraft EventsAdvanced bot optionsCreate a simple botCredits to discord.py for the awesome syntax
inspiration.bot=ws.Bot(name="My Bot",prefix="!")@bot.event# this is the only event that is not asyncdefon_ready(ctx):print(f"Ready @{ctx.host}:{ctx.port}")@bot.eventasyncdefon_connect(ctx):print("Connected!")ctx.msg("Hello World.")@bot.eventasyncdefon_disconnect(ctx):print("Disconnected!")@bot.command()asyncdefping(ctx):ctx.msg("Pong")ctx.cmd("title @a actionbar Welcome")bot.run("localhost",6464)We will go with6464as the port but you can
choose any other four-digit number.Now type/connect localhost:6464in the Minecraft
Chat and wait for a response.You may have to disable "Require Encrypted
Websockets" in order to make it work.Most of the time thectxargument have methods
for sending messages in the game. These are:cmd(command)Executes a minecraft command.
The slash prefix is optional.
Also accessable withcommand(...)andexecute(...)raw(message, target = "@a")Sends a raw message to the specified target.msg(message, target = "@a")Sends a message to the specified target.
Also accessable withmessage(...)debug(message, target = "@a")Sends a debug message to the specified target.
The output level must be set todebugor higher.info(message, targt = "@a")Sends an info message to the specified target.
The output level must be set toinfoor higher.err(message, target = "@a")Sends a error message to the specified target.
The output level must be set toerroror higher.
Also accessable witherror(...)warning(message, target = "@a")Sends a warning to the specified target.
The output level must be set towarningor higher.
Also accessable withwarn(...)critical(message, target = "@a")Sends a critical message to the specified target.
The output level must be set tocritical.
Also accessable withfatal(...)Events@bot.eventdefon_ready(ctx):# notice here is no asyncprint(f"Ready @{ctx.host}:{ctx.port}")@bot.eventasyncdefon_connect(ctx):print("Connected!")ctx.info("Connected!")@bot.eventasyncdefon_commandresponse(ctx):pass@bot.eventasyncdefon_disconnect(ctx):print("Disconnected!")print(f"Code:{ctx.code}")print(f"Reason:{ctx.reason}")raiseSystemExit("Disconnected")@bot.eventasyncdefon_unknown_command(ctx):ctx.err(f"Unknown command '{ctx.command}'. Type '{bot.prefix}help' for more information.")@bot.eventasyncdefon_convert_error(ctx):ctx.err(f"{ctx.argument}must be type of{ctx.converter}.")@bot.eventasyncdefon_too_many_arguments(ctx):ctx.err(f"Too many arguments:{', '.join(ctx.arguments)}. Type '{bot.prefix}help{ctx.command}' for more information.")@bot.eventasyncdefon_missing_arguments(ctx):ctx.err(f"Missing argument(s):{', '.join(ctx.arguments)}. Type '{bot.prefix}help{ctx.command}' for more information.")Minecraft EventsMinecraft sends several events to the bot.
You can react on them with theminectafteventdecorator:@bot.minecrafteventasyncdefblock_broken(ctx):ctx.info(f"{ctx.block_name}has been broken.")This will display a message which block got
broken.Some events have supported methods (e.g. in
this caseblock_name) but some don't.
You can access the data withctx.data["<name>"]where name could be"name".Keep in mind that you have to change the
event name into snakecase format. That means
thatBlockBrokenbecomesblock_broken.