Skip to content

Modules

You may want to define functions, classes, or constants to reuse later. If you want to keep them when TGPy restarts, save their definitions to modules.

Modules are code snippets that run at every startup.

Add a module

Say TGPy ran your message. Then you can reply to your message with this method:

modules.add(name)

Alternatively, you can add a module from a string with modules.add(name, source).

Example

  1. Define a square function:

    def square(x):
       return x * x
    

    None
    

  2. Save the definition to modules:

    # in reply to the previous message
    modules.add('square')
    

    Added module 'square'.
    The module will be executed every time TGPy starts.
    

Info

If a module with this name already exists, its code will be replaced.

Remove a module

Remove a module by name:

modules.remove(name)

Manage modules

Use the string value of modules to list all of your modules:

modules

The modules object provides handy ways to manage your modules. You can iterate over it to get names of your modules or use modules[name] to get info about the module.

Storage

Modules are stored as separate Python files in tgpy/modules directory. You can safely edit them manually.

Modules run each time TGPy starts. By default, they run in the order they were added.

Each module file contains module metadata.

Features

By default, all variables from a module are saved for future use. You can specify ones the with the __all__ variable.

Standard modules

TGPy has a number of features implemented via standard modules, such as ping() and restart() functions. You may want to disable these features, for example to reimplement them. Use the core.disabled_modules config key to specify the disabled modules. For example, you can use the following code to disable the prevent_eval module which provides // and cancel features:

tgpy.api.config.set('core.disabled_modules', ['prevent_eval'])

All standard modules in the repo