@@ -83,10 +83,12 @@ if (!fs.existsSync("./configs/Discord-Chatter/discordactivity.json") ) {
8383}
8484
8585// BDSX Imports
86- import { bedrockServer , MinecraftPacketIds , command } from 'bdsx' ;
86+ import { MinecraftPacketIds } from 'bdsx' ;
8787import { events } from "bdsx/event" ;
8888import { CxxString } from "bdsx/nativetype" ;
8989import { NetworkIdentifier } from "bdsx/bds/networkidentifier" ;
90+ import { TextPacket } from 'bdsx/bds/packets' ;
91+ import { serverInstance } from 'bdsx/bds/server' ;
9092
9193
9294// Discord Bot Requirements
@@ -255,6 +257,17 @@ function SendToDiscordEvent(message: string, user: string) {
255257 }
256258} ;
257259
260+ // tellRaw code (originally "tellAllRaw()") is credited to 7dev7urandom (https://github.com/7dev7urandom/bdsx-discord-chat/blob/main/index.ts)
261+ function tellAllRaw ( text : string ) {
262+ const packet = TextPacket . create ( ) ;
263+ packet . type = TextPacket . Types . Raw ;
264+ packet . message = text ;
265+ for ( const i of serverInstance . minecraft . getLevel ( ) . players . toArray ( ) ) {
266+ i . sendPacket ( packet ) ;
267+ }
268+ packet . dispose ( ) ;
269+ }
270+
258271function SendToGame ( message : string , user : string ) {
259272
260273 if ( serverAlive == false ) {
@@ -284,7 +297,7 @@ function SendToGame(message: string, user: string) {
284297 }
285298
286299 // Actual Messages
287- bedrockServer . executeCommand ( "say <§2[DISCORD]§r " + user + "> " + message , false ) ;
300+ tellAllRaw ( " <§2[DISCORD]§r " + user + "> " + message ) ;
288301 if ( GetConfig ( "PostDiscordMessagesToConsole" ) == true ) { console . log ( "[" + timestamp + " CHAT] <[DISCORD] " + user + "> " + message ) } ;
289302} ;
290303
@@ -515,114 +528,128 @@ function UpdateActivity(key: string, value: string | boolean | undefined) {
515528
516529// On Server Open
517530events . serverOpen . on ( ( ) => {
518- // THIS WILL NO LONGER WORK IN MARCH 2022
519- let system = server . registerSystem ( 0 , 0 ) ;
520531
521- // Cheaty way to return output to a user
522- function tellRaw ( playerName : string , text : string ) {
532+ function tellRaw ( playerName : string , text : string ) {
523533 if ( playerName != "Server" ) {
524- system . executeCommand ( `/tellraw ${ playerName } {"rawtext":[{"text":"${ text } "}]}` , ( ) => { } ) ;
534+ const packet = TextPacket . create ( ) ;
535+ packet . type = TextPacket . Types . Raw ;
536+ packet . message = text ;
537+ for ( const i of serverInstance . minecraft . getLevel ( ) . players . toArray ( ) ) {
538+ console . log ( i )
539+ }
540+ packet . dispose ( ) ;
525541 } else {
526542 console . log ( text ) ;
527543 }
528544 }
529545
546+ /*
530547 // Register the "dc" Command for DiscordChatter (1 = Operator Perms Minimum Required)
531- command . register ( 'dc' , 'DiscordChatter Commands' , 1 ) . overload ( ( param , origin , output ) => {
532- let playerName = origin . getName ( ) ;
533- switch ( param . first ) {
534- case "help" :
535- tellRaw ( playerName , "§3----- DiscordChatter Help -----§r" )
536- tellRaw ( playerName , "/dc help - Shows this help text" )
537- tellRaw ( playerName , "/dc reload - Reloads the Discord Bot" )
538- tellRaw ( playerName , "/dc config - Used to change config options" )
539- tellRaw ( playerName , "" )
540- tellRaw ( playerName , "§4Please note that most commands require OP.§r" ) ;
541- return ;
542-
543- case "config" :
544- switch ( param . second ) {
545- case "token" :
546- if ( playerName == "Server" ) { // This should be safe as the odds of a player named "Server" AND being an OP are almost 0.
547- UpdateConfig ( "token" , param . third ) ;
548- tellRaw ( playerName , "Token Updated; Run `dc reload` to log in." ) ;
549- return ;
550- }
551- tellRaw ( playerName , "Tokens can only be updated via the server console." ) ;
548+ events.command.on((cmd, origin, ctx)=>{
549+ var params = cmd.replace('/', '').split(" ");
552550
553- case "chanID" :
554- var chanID = param . third ?. replace ( "chanID_" , "" ) ;
555- UpdateConfig ( "chanID" , chanID ) ;
556- tellRaw ( playerName , `\"chanID\" set to \"§b${ chanID } §r\"` ) ;
557- return ;
551+ var playerName = origin.name;
558552
559- case "BotEnabled" :
560- if ( UpdateConfig ( "BotEnabled" , param . third ) == 0 ) {
561- tellRaw ( playerName , `\"BotEnabled\" set to \"§a${ param . third } §r\"` ) ;
562- } else {
563- tellRaw ( playerName , `Invalid value \"${ param . third } \". Use \"dc config help\" for more info.` ) ;
564- }
565- return ;
553+ if (ctx.origin.isServerCommandOrigin()) {
554+ playerName = "Server"
555+ }
566556
567- case "PostDiscordMessagesToConsole" :
568- if ( UpdateConfig ( "PostDiscordMessagesToConsole" , param . third ) == 0 ) {
569- tellRaw ( playerName , `\"PostDiscordMessagesToConsole\" set to \"§a${ param . third } §r\"` ) ;
570- } else {
571- tellRaw ( playerName , `Invalid value \"${ param . third } \". Use \"dc config help\" for more info.` ) ;
572- }
557+ switch (params[0]){
558+ case 'dc':
559+ switch (params[1]) {
560+ case "help":
561+ tellRaw(playerName, "§3----- DiscordChatter Help -----§r")
562+ tellRaw(playerName, "/dc help - Shows this help text")
563+ tellRaw(playerName, "/dc reload - Reloads the Discord Bot")
564+ tellRaw(playerName, "/dc config - Used to change config options")
565+ tellRaw(playerName, "")
566+ tellRaw(playerName, "§4Please note that most commands require OP.§r");
573567 return;
574568
575- case "EnableJoinLeaveMessages" :
576- if ( UpdateConfig ( "EnableJoinLeaveMessages" , param . third ) == 0 ) {
577- tellRaw ( playerName , `\"EnableJoinLeaveMessages\" set to \"§a${ param . third } §r\"` ) ;
578- } else {
579- tellRaw ( playerName , `Invalid value \"${ param . third } \". Use \"dc config help\" for more info.` ) ;
569+ case "config":
570+ switch (params[2]) {
571+ case "token":
572+ if ( ctx.origin.isServerCommandOrigin() ) {
573+ UpdateConfig("token", params[3]);
574+ tellRaw(playerName, "Token Updated; Run `dc reload` to log in.");
575+ return;
576+ }
577+ tellRaw(playerName, "Tokens can only be updated via the server console.");
578+
579+ case "chanID":
580+ var chanID = params[3]?.replace("chanID_", "");
581+ UpdateConfig("chanID", chanID);
582+ tellRaw(playerName, `\"chanID\" set to \"§b${chanID}§r\"`);
583+ return;
584+
585+ case "BotEnabled":
586+ if ( UpdateConfig("BotEnabled", params[3]) == 0 ) {
587+ tellRaw(playerName, `\"BotEnabled\" set to \"§a${params[3]}§r\"`);
588+ } else {
589+ tellRaw(playerName, `Invalid value \"${params[3]}\". Use \"dc config help\" for more info.`);
590+ }
591+ return;
592+
593+ case "PostDiscordMessagesToConsole":
594+ if ( UpdateConfig("PostDiscordMessagesToConsole", params[3]) == 0 ) {
595+ tellRaw(playerName, `\"PostDiscordMessagesToConsole\" set to \"§a${params[3]}§r\"`);
596+ } else {
597+ tellRaw(playerName, `Invalid value \"${params[3]}\". Use \"dc config help\" for more info.`);
598+ }
599+ return;
600+
601+ case "EnableJoinLeaveMessages":
602+ if ( UpdateConfig("EnableJoinLeaveMessages", params[3]) == 0 ) {
603+ tellRaw(playerName, `\"EnableJoinLeaveMessages\" set to \"§a${params[3]}§r\"`);
604+ } else {
605+ tellRaw(playerName, `Invalid value \"${params[3]}\". Use \"dc config help\" for more info.`);
606+ }
607+ return;
608+
609+ case "EnableServerStartStopMessages":
610+ if ( UpdateConfig("EnableServerStartStopMessages", params[3]) == 0 ) {
611+ tellRaw(playerName, `\"EnableServerStartStopMessages\" set to \"§a${params[3]}§r\"`);
612+ } else {
613+ tellRaw(playerName, `Invalid value \"${params[3]}\". Use \"dc config help\" for more info.`);
614+ }
615+ return;
616+ case "help":
617+ tellRaw(playerName, "§3----- DiscordChatter Config Help -----§r")
618+ tellRaw(playerName, "/dc config {Key} {Value} - Set a config value (Case-Sensitive)")
619+ tellRaw(playerName, "A list of keys can be found at https://github.com/TheShadowEevee/BDSX-Discord-Chatter-Plugin#readme")
620+ tellRaw(playerName, "Instructions on how to get some values can be found there as well.")
621+ tellRaw(playerName, "Currently Discord Bot Activity must be set in the config.")
622+ tellRaw(playerName, "")
623+ tellRaw(playerName, "§4Please note that most commands require OP.§r");
624+ tellRaw(playerName, "§4You MUST be using the server console to modify the bot token.§r");
625+ return;
626+
627+ default:
628+ tellRaw(playerName, `Invalid argument \"${params[2]}\". Use \"dc config help\" for more info.`);
629+ return;
580630 }
581- return ;
582631
583- case "EnableServerStartStopMessages" :
584- if ( UpdateConfig ( "EnableServerStartStopMessages" , param . third ) == 0 ) {
585- tellRaw ( playerName , `\"EnableServerStartStopMessages\" set to \"§a${ param . third } §r\"` ) ;
586- } else {
587- tellRaw ( playerName , `Invalid value \"${ param . third } \". Use \"dc config help\" for more info.` ) ;
632+ case "reload":
633+ tellRaw(playerName, "Reloading DiscordChatter!");
634+ try {
635+ ReloadBot();
636+ } catch (e) {
637+ if (e == "Error: Bot is disabled!") {
638+ tellRaw(playerName, "DiscordChatter is disabled. Stopping the reload.");
639+ } else {
640+ throw e;
641+ }
588642 }
589643 return;
590- case "help" :
591- tellRaw ( playerName , "§3----- DiscordChatter Config Help -----§r" )
592- tellRaw ( playerName , "/dc config {Key} {Value} - Set a config value (Case-Sensitive)" )
593- tellRaw ( playerName , "A list of keys can be found at https://github.com/TheShadowEevee/BDSX-Discord-Chatter-Plugin#readme" )
594- tellRaw ( playerName , "Instructions on how to get some values can be found there as well." )
595- tellRaw ( playerName , "Currently Discord Bot Activity must be set in the config." )
596- tellRaw ( playerName , "" )
597- tellRaw ( playerName , "§4Please note that most commands require OP.§r" ) ;
598- tellRaw ( playerName , "§4You MUST be using the server console to modify the bot token.§r" ) ;
599- return ;
600644
601645 default:
602- tellRaw ( playerName , `Invalid argument \"${ param . second } \". Use \"dc config help\" for more info .` ) ;
646+ tellRaw(playerName, `Invalid argument \"${params[1] }\". Use \"dc help\" for a list of commands .`);
603647 return;
604648 }
605-
606- case "reload" :
607- tellRaw ( playerName , "Reloading DiscordChatter!" ) ;
608- try {
609- ReloadBot ( ) ;
610- } catch ( e ) {
611- if ( e == "Error: Bot is disabled!" ) {
612- tellRaw ( playerName , "DiscordChatter is disabled. Stopping the reload." ) ;
613- } else {
614- throw e ;
615- }
616- }
617- return ;
618-
619649 default:
620- tellRaw ( playerName , `Invalid argument \"${ param . first } \". Use \"dc help\" for a list of commands.` ) ;
621650 return;
622651 }
623- } , {
624- first : [ CxxString , true ] , // Help, Config, Reload, Other
625- second : [ CxxString , true ] , // Config Types
626- third : [ CxxString , true ] // New Config Values
652+ return 0;
627653 });
654+ */
628655} ) ;
0 commit comments