-
Notifications
You must be signed in to change notification settings - Fork 9
LUA
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!
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.
id = exec("say Hi!");
sleep(1);
exec("edit "..id.." Hey*");
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.
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.
Since the events are registered through the registerEvent
function, you simply need to run them!
run your file.lua
function myFunc(event)
if event.Content == ":>" then
exec("channel "..event.ChannelID);
exec("say =)*");
end
end
registerEvent("my very awesome unique id", "myFunc");
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. |