Python Script to Use a specific npc

How can you code a python script to use a specific npc and use its option then buy or sell specific items to him ?

First you need to identify the ID of the npc you want to interact with. You can do so with something like this:

def getNPCID():
    npcs = get_npcs()
    for id, npc in npcs.items():
        npc_name = npc['name']
        if npc_name[:5] == 'Daily':
            return id

Then you need to generate a struct with that NPC ID that you will inject to joymax. Like this:

p = struct.pack('I',npc_id)

Injecting that will open the NPC dialog window with your desired NPC if you are within range of it.

You will then want to keep adding to that struct with the id of the option you want to select from the NPC dialog window, like this:

p += struct.pack('B',2)

For more info on the inject_joymax function check out the phBot API documentation here: Packet Injection - phBot Plugins

If you are struggling to build your packets to inject, I’d recommend using PacketLogger or xPacketTool and manually checking what packets are sent to the server when you click on the desired dialog options from the NPC.

Hope it was useful.
If you want more info, maybe you will find it helpful to check out the code of this plugin i created to get and return the daily SP quests: phBot-plugins/ at main · joranro1997/phBot-plugins · GitHub

1 Like