Skip to content

How to make a plugin.

Ccode-lang edited this page May 17, 2024 · 10 revisions

Introduction

When you run a Xander instance you may notice this line in your log:

[14/04/2023 11:47:35] Loading plugins

This is one of the most useful features of Xander at work. You can load Python scripts with a certain format as plugins to add extra commands (and features) to the bot without modifying the bot.py file at all.

Making your first plugin

A basic plugin looks like the below:

# You can import extra modules here if you need to

# Import the Xander plugin API bindings
from xander_plugin import *


# Called when the plugin is loaded.
def onload():
    # A simple call to the log() function from bot.py which prints to the log file.
    log("Example plugin loaded!")

# Called when a message is received. It must be async and has an input of a discord message object. (More on how to use them here https://discordpy.readthedocs.io/en/stable/api.html#discord.Message)
async def onmessage(message):
    # An example of what we can do with a message object.  The message object can be read about using the link above.
    log(f"The message \"{message.content}\" was sent by {message.author.name}")
    
    # The onmessage has to return a value based on if it should block more hooks from being run on the current message. If this was False it would block the rest of the onmessage hooks from running.
    return True

# A function called when the bot program exits.
def onexit():
    obj("Example plugin exit run.")

Examples

Every plugin in the plugins folder of the project will give you a good idea of how to make a plugin. It is good to know the discord.py API as objects from it are accessed frequently in making a plugin.

Clone this wiki locally