Skip to content
LEGOlord208 edited this page Apr 22, 2017 · 1 revision

LUA Scripting in DiscordConsole

Welcome to LUA: A popular and modifiable scripting language.
We use LUA in DiscordConsole to allow scripting repetetive tasks and other conveniences.

To script in LUA is just like any other LUA file.
When you're done, run them using:

run <lua file>

Easy, huh? Now let's get into the details!

LUA Specs

We use LUA 5.2 using the go-lua library.

Added methods:
exec(string command): string - Execute a DiscordConsole command. It returns the most important element.
replace(string original, string search, string replacement): string - Replaces search with replacement in original and returns.
sleep(int seconds) - Waits for seconds seconds.

Example!

id = exec("say Hi!");
sleep(1);
exec("edit "..id.." Hey*");

Arguments!

You can also run LUA with arguments!
They are stored in the arg array.

Example:

exec("say "..arg[1]);

But... how do we run it?
Simple!
Add a colon after run <file> and then your arguments!

Example:

run test.lua: arg1 arg2 arg3 etc

Since we only use arg[1], it'll just say arg1.
But following LUA SplitJoin wiki,
we see that we may use table.concat(arg, " ") to use all arguments as one string.

Events!

Another feature of DiscordConsole's LUA are events.
The only event available yet is on message event.

To register an event, you first create a function.
This is quite simply what will execute once the event happens.

Now, call the following function:

registerEvent(string id, string handler)

The ID is anything. Anything at all. Well, it gotta be of the type string.
It is only there, so when you reload the script, it doesn't create a new event.

The handler is the name of your function.

Activating the events

Since the events are registered through the registerEvent function, you simply need to run them!
run your file.lua

Example event

function myFunc(event)
	if event.Content == ":>" then
		exec("channel "..event.ChannelID);
		exec("say =)*");
	end
end

registerEvent("my very awesome unique id", "myFunc");

Event params!

Oi there, wait a second... What the heck am I doing? Where did i get that event.Content from?
Explain myself!

So, I forgot to tell you the function... has a parameter.
It includes information about the message.
Here it is:

Name Description
ID The message ID.
Content The message content.
ChannelID The channel ID.
Timestamp The timestamp (ANSIC).
AuthorID The author's user ID.
AuthorBot Either true or false.
AuthorAvatar The author's avatar key.
AuthorName The author's name.
Clone this wiki locally