Skip to content

How to make a plugin.

Ccode-lang edited this page Oct 22, 2023 · 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 directly.

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 information in discord.py docs)
async def onmessage(message):
    # An example of what we can do with a message object.  The message object is defined in the discord.py API.
    log(f"The message \"{message.content}\" was sent by {message.author.name}")

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

xander_plugin API

config

Config is a module that holds all Xander configurations. It is written in Python for simplicity and compatibility with Python. (Exact definitions in config.py)

def log(line)

A function that prints line to the terminal with a timestamp and saves the message to log.txt. It returns nothing.

async def modlog(string, message, delete=False)

A function that can be used to create moderator logs in the modlog channel defined in config.py. string is the message that is sent to the modlog channel but the bot. message is the message variable of the moderated message. (message is required because it is used to differentiate servers) When delete is True the message referenced to by message is deleted after sending string the the modlog channel.

client

client is the client object of the bot. More information is in the discord.py documentation.

help_menu

help_menu is the help menu dictionary. It can be interacted with by plugins to add help entries for them.

voice_client

The voice client object of the bot if it is connected to a voice channel. Equals None if not connected to a channel. Check discord.py documentation to see how a voice client object works.

def getother(name)

Get the variable associated with a keyword from the bot.py file.

def help_menu_edit(feature_name, info)

Add a help menu entry for the plugin under the name feature_name and with the information provided in info.

async def join_vc(channel)

Join a voice channel passed in channel. If connected already this method does nothing.

async def leave_vc()

Leave the currently connected voice channel. If not connected this method does nothing.

Clone this wiki locally