In case you missed it, this series begins here: Let's Build a Elder Scrolls Online Mod - Part 1
When we last left off we had built a core Elder Scrolls Online add-on that notified the player when it was loaded.
Let's build on this and tackle how to implement slash commands and also how to read the chat window so we can parse it for our call-to-action string.
I've setup a GitHub repo for the complete code here: This One Invites.
Implementing listing for slash commands is very simple. You need to use the ESO API "SLASH_COMMANDS" and provide it with a command to listen for and a function to call.
We're going to setup three slash commands:
- /toi will call a function that displays how to use This One Invites.
- /toion will call a function to start listening to chat. This function will also take a single argument, which will be the listening string.
- /toioff will call a function that stops all listening.
Three Slash Commands
At the end of the file we will put three lines:
SLASH_COMMANDS["/toi"] = ThisOneInvites.help SLASH_COMMANDS["/toion"] = ThisOneInvites.listen SLASH_COMMANDS["/toioff"] = ThisOneInvites.quit
These three lines use SLASH_COMMANDS and call three different functions: help, listen and quit.
Let's write those functions now.
We will have our help function display some text about how to use the add-on.
function ThisOneInvites.help() d("To start auto inviting, type /toion [string] in your chat where [string] is what you want the activator to be. For example: /toion x") d("To shut off inviting, type /toioff") end
When someone types /toion [string] we need to start listening. Also we'll need to guard against someone just typing in /toion without any arguments.
In this function we will do three main things:
- We will look for the argument to be nil or empty and send a help message to the screen.
- We will tie into the chat event and wire up a callback function to run whenever there's a message
- We will set the argument string (activatorText) so that we can use it later.
The event is called: EVENT_CHAT_MESSAGE_CHANNEL. This event happens for each message to the chat window and the returning data is the message type, the message itself, who sent the message and if it's from customer service.
ThisOneInvites.activatorText = "" -- setting this up for later use
function ThisOneInvites.listen(activatorText) if activatorText == nil or activatorText == '' then d("It looks like you wanted to start auto inviting. You need to specify a string as well like this: /toion x") else d("This One Invites lisening is now turned on and listening for text: " .. activatorText) ThisOneInvites.activatorText = activatorText EVENT_MANAGER:RegisterForEvent(ThisOneInvites.name, EVENT_CHAT_MESSAGE_CHANNEL, ThisOneInvites.callback) end end
At some point the user may want to turn off listening. We will accomplish this by simply unregistering for the event (EVENT_CHAT_MESSAGE_CHANNEL).
This function is called on /toioff.
function ThisOneInvites.quit() d("This One Invites listening is now turned off.") EVENT_MANAGER:UnRegisterForEvent(ThisOneInvites.name, EVENT_CHAT_MESSAGE_CHANNEL) end
Chat Event Callback
Finally, we need to wire up the callback that will be run each time the EVENT_CHAT_MESSAGE_CHANNEL event is fired.
At this time we are going to put in the plumbing that will hook into the chat window messages and echo out a debug message containing the text the add-on sees and who sent it. This will confirm that we're on the right track. Later we will develop this functionality out further.
One of the main things to note is we need to scrub the "from" argument, because you'll get data like this:
We only want the name of the character so that we can eventually send them a party invite.
Let's scrub that name and echo out the debug message.
ThisOneInvites.callback = function(_, messageType, from, message) name = from:gsub("%^.+", "") d("Checking message " .. string.lower(message) .. " from: " .. name .. " msg type " .. messageType ) end
Time To Test!
We now have a Elder Scrolls Online add-on that responds to slash commands and listens to the chat window.
Here's an image of the add-on in action:
In the next part we're going to make a code correction with an event and look into wiring up the listening callback function more.