Custom Commands - Mytherin/Tibialyzer GitHub Wiki

In the latest version of Tibialyzer, it is possible to add your own custom commands. When these commands popup in chat, they will perform a system call (i.e. a command like you type into the 'cmd' window). On this page, I will provide a short tutorial for calling a Python script using a custom command.

Play Music

On my computer, I have a bunch of Python scripts that I use to automate various actions. For this tutorial, I will use one of my Python scripts that searches for songs on my computer and then plays them.

The final Tibialyzer command will look like this.

Command Description
play@[songname] Plays all songs that match [songname].

You could then type play@michael jackson in Tibia and it will play all Michael Jackson songs that you have on your computer.

Prerequisites

If you want to add this specific command to Tibialyzer, you must have Python installed. Python can be downloaded from the official Python website, here. In addition, you will need some .mp3 files on your computer, otherwise there will be no songs to play.

Note that this is only necessary for the command described in this tutorial. If you want to create your own custom commands that do not involve Python, you do not need Python installed. If you are not interested in this specific command, skip to the Setting Up The Command section.

The Python Script

Prior to adding the commands, we need to have the Python script stored somewhere on our computer so we can call the script. The Python script that I use to play music files is shown below.

Note that there are two important settings that you have to customize, otherwise it will not work on your computer.

  • You will have to specify the path to your music player is. I personally use the foobar2000 music player. My music player is stored in the path C:\Program Files (x86)\foobar2000\foobar2000.exe. If you use a different music player or it is in a different location, you will have to change the player variable at the top of this script so that it points to your music player.

  • You will have to specify the path to where your .mp3 files are stored. My mp3 files are stored in the C:\Music directory. If your music files are stored somewhere else, you will need to alter the basedir directory so that it points to the location where your music is stored.

Note: When setting a path in Python, you will have to replace every single backslash (\) with two backslashes (\\)

After changing these settings, copy the below script in a text editor (such as Notepad) and save it somewhere on your computer. I have saved it in the C:\Bin directory and named the file play.py. I recommend saving this in a directory without spaces in the name, such as C:\Bin.

player = "C:\\Program Files (x86)\\foobar2000\\foobar2000.exe"
basedir = "C:\\Music"
music_ext = ".mp3"

import os
import urllib.request
import sys
import glob

arguments = sys.argv

if len(arguments) == 1: 
    print("No arguments specified.")
    exit(1)

search_term = arguments[1].lower()


recent_files = []
if search_term == 'recent':
    file_count = 100
    if len(arguments) > 2: 
        try: file_count = int(arguments[2])
        except: pass

def matches(song):
    for i in range(1, len(arguments)):
        if arguments[i].lower() not in song:
            return False
    return True


playlist = os.path.join(basedir, "temp_playlist.m3u")
_m3u = open(playlist, "wb")
for (path, subdirs, files) in os.walk(basedir):
    os.chdir(path)
    if glob.glob("*" + music_ext) != []:
        for song in glob.glob("*" + music_ext):
            songpath = os.path.join(path, song)
            if search_term == 'recent':
                recent_files.append((os.path.getmtime(songpath), songpath))
            elif search_term == 'all' or matches(song.lower()):
                _m3u.write(songpath.encode('utf-8'))
                _m3u.write(b"\n")

if len(recent_files) > 0:    
    recent_files.sort(key=lambda tup: -tup[0])
    for i in range(0, file_count):
        songpath = recent_files[i][1]
        _m3u.write(songpath.encode('utf-8'))
        _m3u.write(b"\n")
        
_m3u.close()

def launchWithoutConsole(commands):
    """Launches 'command' windowless and waits until finished"""
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    return subprocess.Popen(commands, startupinfo=startupinfo).wait()

import subprocess
launchWithoutConsole([player, playlist])

Setting Up The Command

In the System tab, you can add custom commands to Tibialyzer. First, in the Custom Commands box, rename the command to play. This will be the name of the command you will have to type in Tibia when you want to trigger the command.

After that, we have to set up the system command to call and the parameters to this command. As System Command, we will choose the Python interpreter python.exe. My Python interpreter is stored in the C:\Python34 directory. Thus my System Command is C:\Python34\python.exe.

Finally, we need to set up the parameters to pass to the command. First, we have to pass the play.py script we saved. Enter the full path to the script. In my case, this is C:\Bin\play.py. Note: if you have spaces in this path, you have to enclose the path quotes, such as "C:\Space Directory\play.py".

Then, we pass along the parameter that the player can pass in Tibia through the command play@[parameter]. This parameter can be passed by adding {0} for the first parameter. Note that there should be a space between the path to the script and {0}.

Custom Command Information

Note: In this case, we only support one parameter, but it is possible to add multiple parameters by adding {0}, {1}, {2}, etc. The parameters can be passed through the command by adding multiple @ symbols, such as command@first parameter@second parameter@third parameter.

Testing the Command

Now that we have added the command, we can test it by typing the play@[songname] command in Tibia. As we can see, the command works for me now.

Play Command In Action