Your first Module - OrfeasZ/GrooveCaster GitHub Wiki

Your First Module

Introduction

GrooveCaster Modules are simple Python scripts (in IronPython syntax).

All modules changes are propagated on-the-fly, meaning that changes to them are effective immediately, without requiring a server restart or reconfiguration.

In this example, we will be implementing a very simple module which registers a new chat command (with two aliases), which will echo back a message to the user.

First Things First

The first thing you need to do is to login to your GrooveCaster administration interface (usually at http://localhost:42278), and login as the admin user.

From there, you should navigate to the Module Management page, and when in that page, click on the New Module button.

Then go ahead and give your Module a name and a description.

For this example, we will be using the following details: enter image description here

Afterwards, you can start writing your script.

Your Script

For the sake of this example, we will be providing the complete script and will be explaining it line by line, so here's the final script:

from GS.Lib.Events import ChatMessageEvent

def OnEcho(p_Event, p_Data):
    ChatManager.SendChatMessage("Echo from %s: %s" % (p_Event.UserName, p_Data))

ChatManager.RegisterCommand('echo', ' <message>: Echoes a message back to the user.', Action[ChatMessageEvent, str](OnEcho), List[str]([ 'e', 'ec' ]))

def OnUnload():
    ChatManager.RemoveCommand('echo')

Now let's take a look at what this script does:

# This line imports the ChatMessageEvent class from the SharpShark library.
# When new chat messages are received, a ChatMessageEvent is being triggered
# with detailed information about the message (as seen below).

from GS.Lib.Events import ChatMessageEvent
# This is our handler function for when the echo command is triggered.
# It takes two parameters:
# - p_Event: A ChatMessageEvent object with details about the chat message.
# - p_Data: The message data of the command (excluding the command itself)

def OnEcho(p_Event, p_Data):
	# Upon receiving the echo command, we simply send a new chat message via
	# the ChatManager, containing the username of the user who triggered the
	# command, along with the data he sent.
	
    ChatManager.SendChatMessage("Echo from %s: %s" % (p_Event.UserName, p_Data))
# Here, we are registering our new echo command with the ChatManager as
# soon as our module loads.
#
# The RegisterCommand takes the following parameters:
# - p_Command: A string of the command name
# - p_Description: The description of the command which will be used
#   for help listings.
# - p_Callback: An action of the function to call when this command is
#   triggered by a user.
# - p_Aliases (optional): A list of aliases for this command.
#
# Accordingly, for this call the parameters are set as follows:
# - p_Command: 'echo'
# - p_Description: ' <message>: Echoes a message back to the user.'
# - p_Callback: Action[ChatMessageEvent, str](OnEcho)
# - p_Aliases: List[str]([ 'e', 'ec' ])

ChatManager.RegisterCommand('echo', ' <message>: Echoes a message back to the user.', Action[ChatMessageEvent, str](OnEcho), List[str]([ 'e', 'ec' ]))
# The OnUnload function will always get called when GrooveCaster
# unloads the module (either when disabling it, deleting it, or
# reloading it).
#
# You should perform all cleanup operations in this function, in
# order to avoid hanging functionality. If you don't require any
# cleanup, you can omit declaring this function.
#
# In this instance, we're simply removing the echo command from 
# the ChatManager, making it unavailable after the plugin has
# been disabled (or unloaded).

def OnUnload():
    ChatManager.RemoveCommand('echo')

Loading the Script

Now that your script is ready and you've typed it in the script editor, simply hit the Create button.

If everything worked as expected you should be able to go into your broadcast's chat and use your newly registered command by typing either !echo message, !e message, or !ec message.

If any errors occurred while your module was being compiled you should be able to see them in the Module Management page.

Wrapping it up

Even though this is a very simple module, GrooveCaster provides a wide range of functionality for modules.

For more information make sure to check the API documentation or other sample modules.

⚠️ **GitHub.com Fallback** ⚠️