2017-11-26 01:14:02 +00:00
const nmv = require ( '../dist/index' ) ;
2017-11-21 15:09:26 +00:00
const loginParameters = new nmv . LoginParameters ( ) ;
const parameters = require ( './loginParameters.json' ) ;
2017-12-15 19:13:45 +00:00
const uuid = require ( 'uuid' ) ;
2017-11-21 15:09:26 +00:00
loginParameters . firstName = parameters . firstName ;
loginParameters . lastName = parameters . lastName ;
loginParameters . password = parameters . password ;
loginParameters . start = "last" ;
2017-12-14 02:06:28 +00:00
//const options = nmv.BotOptionFlags.None;
// If you don't intend to use the object store (i.e you have no interest in inworld objects, textures, etc,
// using ObjectStoreLite will drastically reduce the footprint
//
2017-12-14 02:18:50 +00:00
const options = nmv . BotOptionFlags . LiteObjectStore | nmv . BotOptionFlags . StoreMyAttachmentsOnly ;
2017-12-14 02:06:28 +00:00
const bot = new nmv . Bot ( loginParameters , options ) ;
2017-11-21 15:09:26 +00:00
2017-12-14 01:29:21 +00:00
let isConnected = false ;
2017-11-26 03:10:54 +00:00
2017-12-14 01:21:18 +00:00
const master = 'd1cd5b71-6209-4595-9bf0-771bf689ce00' ;
2017-12-15 19:13:45 +00:00
let loginResponse = null ;
2017-12-14 01:21:18 +00:00
bot . clientEvents . onLure . subscribe ( ( lureEvent ) =>
2017-11-21 15:09:26 +00:00
{
2017-12-14 01:21:18 +00:00
bot . clientCommands . grid . getRegionMapInfo ( lureEvent . gridX , lureEvent . gridY ) . then ( ( regionInfo ) =>
2017-11-30 04:11:59 +00:00
{
2017-12-14 01:21:18 +00:00
if ( lureEvent . from . toString ( ) === master )
2017-11-30 04:11:59 +00:00
{
2017-12-14 01:21:18 +00:00
console . log ( 'Accepting teleport lure to ' + regionInfo . name + ' (' + regionInfo . avatars . length + ' avatar' + ( ( regionInfo . avatars . length === 1 ) ? '' : 's' ) + ' present) from ' + lureEvent . fromName + ' with message: ' + lureEvent . lureMessage ) ;
bot . clientCommands . teleport . acceptTeleport ( lureEvent ) ;
}
else
{
console . log ( 'Ignoring teleport lure to ' + regionInfo . name + ' (' + regionInfo . avatars . length + ' avatar' + ( ( regionInfo . avatars . length === 1 ) ? '' : 's' ) + ' present) from ' + lureEvent . fromName + ' with message: ' + lureEvent . lureMessage ) ;
}
} ) ;
} ) ;
2017-11-30 04:11:59 +00:00
2017-12-14 01:21:18 +00:00
bot . clientEvents . onInstantMessage . subscribe ( ( IMEvent ) =>
{
if ( IMEvent . source === nmv . ChatSourceType . Agent )
{
if ( ! ( IMEvent . flags & nmv . InstantMessageEventFlags . startTyping || IMEvent . flags & nmv . InstantMessageEventFlags . finishTyping ) )
2017-12-13 19:55:08 +00:00
{
2017-12-14 01:21:18 +00:00
bot . clientCommands . comms . typeInstantMessage ( IMEvent . from , 'Thanks for the message! This account is a scripted agent (bot), so cannot reply to your query. Sorry!' ) ;
}
}
} ) ;
2017-11-30 04:11:59 +00:00
2017-12-19 19:59:06 +00:00
bot . clientEvents . onFriendRequest . subscribe ( ( event ) =>
{
if ( event . from . toString ( ) === master )
{
console . log ( "Accepting friend request from " + event . fromName ) ;
bot . clientCommands . comms . acceptFriendRequest ( event ) ;
}
else
{
console . log ( "Rejecting friend request from " + event . fromName ) ;
bot . clientCommands . comms . rejectFriendRequest ( event ) ;
}
} ) ;
bot . clientEvents . onInventoryOffered . subscribe ( ( event ) =>
{
if ( event . from . toString ( ) === master )
{
console . log ( "Accepting inventory offer from " + event . fromName ) ;
bot . clientCommands . comms . acceptInventoryOffer ( event ) ;
}
else
{
console . log ( "Rejecting inventory offer from " + event . fromName ) ;
bot . clientCommands . comms . rejectInventoryOffer ( event ) ;
}
} ) ;
2017-12-14 01:21:18 +00:00
bot . clientEvents . onDisconnected . subscribe ( ( DisconnectEvent ) =>
{
2017-12-14 01:29:21 +00:00
isConnected = false ;
2017-12-14 01:21:18 +00:00
console . log ( "Disconnected from simulator: " + DisconnectEvent . message ) ;
if ( ! DisconnectEvent . requested )
{
setTimeout ( ( ) =>
2017-12-13 19:55:08 +00:00
{
2017-12-14 01:21:18 +00:00
console . log ( "Reconnecting" ) ;
connect ( ) ;
} , 5000 )
}
} ) ;
2017-12-15 19:13:45 +00:00
let pings = { } ;
2017-12-14 18:22:41 +00:00
bot . clientEvents . onGroupChat . subscribe ( ( GroupChatEvent ) =>
{
console . log ( "Group chat: " + GroupChatEvent . fromName + ': ' + GroupChatEvent . message ) ;
2017-12-15 19:13:45 +00:00
if ( GroupChatEvent . message === '!ping' )
2017-12-14 18:22:41 +00:00
{
2017-12-15 19:13:45 +00:00
let ping = uuid . v4 ( ) ;
pings [ ping ] = Math . floor ( new Date ( ) . getTime ( ) ) ;
2017-12-19 17:58:25 +00:00
bot . clientCommands . comms . sendGroupMessage ( GroupChatEvent . groupID , 'ping ' + ping ) . then ( ( memberCount ) =>
{
console . log ( 'Group message sent to ' + memberCount + ' members' ) ;
} ) ;
2017-12-15 19:13:45 +00:00
}
else if ( GroupChatEvent . from . toString ( ) === loginResponse . agent . agentID . toString ( ) )
{
if ( GroupChatEvent . message . substr ( 0 , 5 ) === 'ping ' )
{
const pingID = GroupChatEvent . message . substr ( 5 ) ;
if ( pings [ pingID ] )
{
console . log ( "found ping" ) ;
const time = ( new Date ( ) . getTime ( ) ) - pings [ pingID ] ;
delete pings [ pingID ] ;
bot . clientCommands . comms . sendGroupMessage ( GroupChatEvent . groupID , 'Chat lag: ' + time + 'ms' ) ;
}
else
{
console . log ( "ping not found |" + pingID + "|" ) ;
}
}
2017-12-14 18:22:41 +00:00
}
} ) ;
bot . clientEvents . onGroupInvite . subscribe ( ( GroupInviteEvent ) =>
{
console . log ( 'Group invite from ' + GroupInviteEvent . fromName + ': ' + GroupInviteEvent . message ) ;
//Resolve avatar key
bot . clientCommands . grid . name2Key ( GroupInviteEvent . fromName ) . then ( ( key ) =>
{
if ( key . toString ( ) === master )
{
console . log ( 'Accepting' ) ;
bot . clientCommands . comms . acceptGroupInvite ( GroupInviteEvent ) ;
}
else
{
console . log ( 'Unauthorised - rejecting' ) ;
bot . clientCommands . comms . rejectGroupInvite ( GroupInviteEvent ) ;
}
} ) . catch ( ( err ) =>
{
console . error ( err ) ;
console . log ( 'Unknown avatar - rejecting' ) ;
bot . clientCommands . comms . rejectGroupInvite ( GroupInviteEvent ) ;
} ) ;
} ) ;
2017-12-19 20:25:42 +00:00
bot . clientEvents . onFriendResponse . subscribe ( ( response ) =>
{
if ( response . accepted )
{
console . log ( response . fromName + ' accepted your friend request' ) ;
}
else
{
console . log ( response . fromName + ' declined your friend request' ) ;
}
} ) ;
2017-12-14 01:21:18 +00:00
function connect ( )
{
console . log ( "Logging in.." ) ;
bot . login ( ) . then ( ( response ) =>
{
2017-12-15 19:13:45 +00:00
loginResponse = response ;
2017-12-14 01:21:18 +00:00
console . log ( "Login complete" ) ;
2017-12-13 19:55:08 +00:00
2017-12-14 01:29:21 +00:00
//Establish circuit with region
2017-12-13 19:55:08 +00:00
return bot . connectToSim ( ) ;
} ) . then ( ( ) =>
{
2017-12-15 19:27:12 +00:00
console . log ( "Connected to simulator" ) ;
2017-12-14 01:29:21 +00:00
isConnected = true ;
2017-12-13 19:55:08 +00:00
// Do some stuff
//bot.clientCommands.comms.typeLocalMessage('Never fear, I am here!', 2000);
//bot.clientCommands.group.sendGroupNotice('503e8ef6-e119-ff5e-2524-24f290dd3867', 'Test', 'testy testy test');
2017-12-15 21:28:45 +00:00
// Group invite example
// Just omit the role parameter for "everyone" role
//
// bot.clientCommands.group.sendGroupInvite("c6424e05-6e2c-fb03-220b-ca7904d11e04", "d1cd5b71-6209-4595-9bf0-771bf689ce00");
// Advanced group invite example
//
// Retrieve group roles
const userToInvite = new nmv . UUID ( "d1cd5b71-6209-4595-9bf0-771bf689ce00" ) ;
2017-12-19 23:43:00 +00:00
const groupID = new nmv . UUID ( "c6424e05-6e2c-fb03-220b-ca7904d11e04" ) ;
2017-12-15 21:28:45 +00:00
bot . clientCommands . group . getGroupRoles ( groupID ) . then ( ( roles ) =>
{
roles . forEach ( ( role ) =>
{
if ( role . Name === 'Officers' )
{
2017-12-15 21:30:38 +00:00
// IMPORTANT: IN PRODUCTION, IT IS HIGHLY RECOMMENDED TO CACHE THIS LIST.
//
2017-12-15 21:28:45 +00:00
bot . clientCommands . group . getMemberList ( groupID ) . then ( ( members ) =>
{
let found = true ;
members . forEach ( ( member ) =>
{
if ( member . AgentID . toString ( ) === userToInvite . toString ( ) )
{
found = true ;
}
} ) ;
if ( found )
{
console . log ( "User already in group, skipping invite" ) ;
}
else
{
2017-12-19 23:43:00 +00:00
bot . clientCommands . group . sendGroupInvite ( groupID , userToInvite , role . RoleID ) ;
2017-12-15 21:28:45 +00:00
}
} ) ;
}
} ) ;
} ) ;
2017-12-16 06:42:41 +00:00
setTimeout ( ( ) => // TODO: This 5 second delay is a fudge. We need to wait for the eventqueue to start properly
{
2017-12-19 23:43:00 +00:00
bot . clientCommands . teleport . teleportTo ( 'Izanagi' , new nmv . Vector3 ( [ 128 , 128 , 20 ] ) , new nmv . Vector3 ( [ 0 ,
1.0 ,
0 ] ) ) . then ( ( ) =>
2017-12-16 06:42:41 +00:00
{
console . log ( "Teleport completed" ) ;
2017-12-19 23:43:00 +00:00
} ) . catch ( ( err ) =>
{
2017-12-16 06:42:41 +00:00
console . error ( err ) ;
} ) ;
} , 5000 ) ;
2017-12-19 20:25:42 +00:00
bot . clientCommands . comms . sendFriendRequest ( master , 'Be friends with me?' ) ;
2017-12-19 23:43:00 +00:00
const folders = bot . clientCommands . inventory . getInventoryRoot ( ) . getChildFolders ( ) ;
folders . forEach ( ( folder ) =>
{
console . log ( 'Top level folder: ' + folder . name ) ;
2017-12-20 02:00:12 +00:00
folder . populate ( ) ;
2017-12-19 23:43:00 +00:00
} ) ;
2017-12-13 19:55:08 +00:00
// When it's time to go home, call bot.close();
} ) . catch ( ( error ) =>
{
2017-12-14 01:29:21 +00:00
isConnected = false ;
2017-12-13 19:55:08 +00:00
console . log ( "Error:" ) ;
console . error ( error ) ;
setTimeout ( ( ) =>
{
connect ( ) ;
} , 5000 )
} ) ;
}
2017-11-21 15:09:26 +00:00
2017-12-13 19:55:08 +00:00
connect ( ) ;
2017-12-14 01:29:21 +00:00
function exitHandler ( options , err )
{
2017-12-14 18:22:41 +00:00
if ( err )
{
console . log ( err . stack ) ;
}
2017-12-14 01:29:21 +00:00
if ( isConnected )
{
console . log ( "Disconnecting" ) ;
bot . close ( ) . then ( ( ) =>
{
process . exit ( )
} ) ;
return ;
}
if ( options . exit )
{
process . exit ( ) ;
}
}
//do something when app is closing
process . on ( 'exit' , exitHandler . bind ( null , { } ) ) ;
//catches ctrl+c event
process . on ( 'SIGINT' , exitHandler . bind ( null , { exit : true } ) ) ;
// catches "kill pid" (for example: nodemon restart)
process . on ( 'SIGUSR1' , exitHandler . bind ( null , { exit : true } ) ) ;
process . on ( 'SIGUSR2' , exitHandler . bind ( null , { exit : true } ) ) ;
//catches uncaught exceptions
process . on ( 'uncaughtException' , exitHandler . bind ( null , { exit : true } ) ) ;