# Introduction

These docs are for OcelotBOT, for custom commands and for interacting with the dashboard API.


# Message Object

The message object is a global value passed into the custom command when it's run. It contains information about the message that triggered it such as the command or message containing the trigger word. Here is an example of the layout:

```javascript
{
  "guild": {
    "id": "622757587489914880",
    "name": "Operarting System",
    "icon": "https://cdn.discordapp.com/icons/622757587489914880/4faeafcf30de330ce7b08b4dd40d616a.webp"
  },
  "channel": {
    "id": "622761218532179968",
    "name": "robit-commands",
    "type": "text"
  },
  "author": {
    "id": "139871249567318017",
    "bot": false,
    "avatar": "https://cdn.discordapp.com/avatars/139871249567318017/6c7e28a3cfa7d3fde9ffc66c8734e45c.png?size=32",
    "nickname": "Peter",
    "username": "Big P",
    "colour": "#f1c40f",
    "roles": [
      {
        "guild": "622757587489914880",
        "id": "666984345055264799",
        "name": "Big P",
        "color": 15844367,
        "hoist": true,
        "rawPosition": 45,
        "permissions": 104324808,
        "managed": false,
        "mentionable": false,
        "deleted": false,
        "createdTimestamp": 1579091859831
      },
      {
        "guild": "622757587489914880",
        "id": "622757587489914880",
        "name": "@everyone",
        "color": 0,
        "hoist": false,
        "rawPosition": 0,
        "permissions": 104193728,
        "managed": false,
        "mentionable": false,
        "deleted": false,
        "createdTimestamp": 1568547379134
      }
    ]
  },
  "content": "!test hello world https://www.youtube.com/watch?v=_htnaGN8eOs",
  "reference": {
    "channelID":"622761218532179968",
    "guildID":"622757587489914880",
    "messageID":"828587624310505513"
  },
  "id": "828587624310505513",
  "timestamp": 1617621084049,
  "attachments": ["https://media.discordapp.net/attachments/622761218532179968/828423074046017546/ahhh.jpg?width=576&height=544"],
  "embeds": [{"title":"Spaghetti Again","type":"rich","description":null,"url":"https://www.youtube.com/watch?v=_htnaGN8eOs","timestamp":null,"color":16711680,"fields":[],"thumbnail":{"url":"https://i.ytimg.com/vi/_htnaGN8eOs/maxresdefault.jpg","proxyURL":"https://images-ext-1.discordapp.net/external/V4xQcbX77M7O021x0z7DCxaWXYxsoWlfLHZILZTcj8U/https/i.ytimg.com/vi/_htnaGN8eOs/maxresdefault.jpg","height":720,"width":1280},"image":null,"author":{"name":"Spaghetti Again","url":"https://www.youtube.com/channel/UC_F3TvFNvFkX5V8d3StkbLQ"},"footer":null}]
}
```

## Field Reference

### guild

This is the Discord server that the message was executed in.

| Field | Content                           |
| ----- | --------------------------------- |
| id    | The ID of the Guild               |
| name  | The name of the Guild             |
| icon  | The URL of the current Guild Icon |

### channel

The channel that the command was executed in.

| Field | Content                                                                                                            |
| ----- | ------------------------------------------------------------------------------------------------------------------ |
| id    | The ID of the Channel                                                                                              |
| name  | The name of the Channel                                                                                            |
| type  | The Type of the channel, technically can be one of these values: `dm, text, voice, category, news, store, unknown` |

### author

The User that triggered the custom function

| Field       | Content                                                                                                    |
| ----------- | ---------------------------------------------------------------------------------------------------------- |
| id          | The ID of the User                                                                                         |
| bot         | `true` if the user is a bot, as bots are unable to trigger custom functions this is always false           |
| avatar      | The URL of the User's current avatar                                                                       |
| nickname    | The Users nickname in this server, if it isn't set this is blank                                           |
| username    | The Users username                                                                                         |
| colour      | The display colour of the users name, set by the hoist role                                                |
| roles       | An array of roles the User has. Always contains at least one role which is @everyone                       |
| content     | The raw content of the message                                                                             |
| reference   | If the message is a reply to another message, the guild, channel and message IDs of that message are here. |
| id          | The message ID                                                                                             |
| timestamp   | The timestamp that the message was sent at, in milliseconds                                                |
| attachments | An array of URLs attached to this message                                                                  |
| embeds      | An array of embeds attached to this message                                                                |

#### author.roles

| Field            | Content                                                           |
| ---------------- | ----------------------------------------------------------------- |
| guild            | The Guild ID that this role belongs to                            |
| id               | The ID of the role                                                |
| name             | The name of the role                                              |
| color            | The colour of the role, represented as an integer                 |
| hoist            | Whether or not this role is displayed separately in the user list |
| rawPosition      | The position of the role in the role list                         |
| permissions      | The bitfield of the role's permissions                            |
| managed          | Whether the role belongs to an integration                        |
| mentionable      | Whether or not you can mention the role                           |
| deleted          | Whether or not the role has been deleted                          |
| createdTimestamp | The timestamp the role was created, in milliseconds               |

#### embeds

| Field       | Content                                                 |
| ----------- | ------------------------------------------------------- |
| title       | The title of the embed                                  |
| type        | Embed type, usually "rich"                              |
| description | The text body of the embed                              |
| url         | The URL that the embed title links to                   |
| timestamp   | Timestamp set in the embed                              |
| color       | The colour of the embed, as a decimal                   |
| fields      | An array of fields                                      |
| thumbnail   | The thumbnail in the embed with direct URL and proxyURL |
| image       | Image attached to the embed                             |
| author      | The author specifying in the embed as name, url         |
| footer      | Embed footer                                            |


# Discord module

The Discord module is an importable module that allows you to interact with Discord. To use it, import it like this:

```lua
local d = require('discord');
d.send("Hello world!")
```

## send(message: string)

Sends a text message in Discord. You can send up to 5 messages per custom function. You can also use this to mention people, but @everyone and @here and mentioning roles will not work.

```lua
local d = require('discord');
d.send("Hello, <@"..message.author.id..">!")
-- Responds: Hello @Big P!
```

## run(command: string)

Runs an OcelotBOT command as if the user running the custom function had run it. The command is run in the context of the triggering message, as if the user that triggered the function and will send the output into the channel. Some commands can't be run inside a custom command, and some commands may not act as expected.&#x20;

{% hint style="info" %}
**Commands do not have to contain the prefix to work**, including the prefix will still work but if you change the prefix after the fact the custom command will need to be updated.
{% endhint %}

```lua
local d = require('discord');
d.run("8ball am I running inside a custom command?")
```

## runWithSettings(command: string, settings: table)

Runs an OcelotBOT command with specific settings. This is an advanced function that will allow you to customise OcelotBOT command responses beyond what is possible normally. For a list of possible uses, check the [Command Settings ](/custom-commands/discord-module/command-settings)page.

```lua
local d = require('discord');
-- Displays the 5th page from the command !reddit aww and disabled the page buttons
d.runWithSettings("!reddit aww", {["pagination.page"]=5,["pagination.disabled"]=1})
```

## playAudio(url: string)

Plays a single audio track, can be a YouTube URL or any audio file or stream. Can always be stopped with !music stop, and will not interrupt currently playing music or guessing games. Joins the voice channel the triggering user is in.

```lua
local d = require('discord');
d.playAudio("https://www.youtube.com/watch?v=_htnaGN8eOs");
```

## paginate(pages: string\[])

Paginate uses OcelotBOT's standard reaction-based pagination, to display each page in `pages` as a separate message.&#x20;

```lua
local d = require('discord');
d.paginate({"This is page 1", "This is page 2", "This is page 3"})
```

## react(emoji: string)

Reacts to the trigger message with the specified emoji ID.

To get the emoji ID, type a backslash (\\) then the emoji you want and send it in Discord. You will see something like this: <:yikes:813555460598267924> &#x20;

&#x20;The ID is the number, so "813555460598267924" in this case.&#x20;

```lua
local d = require('discord');
d.react("813555460598267924"); -- The yikes emoji from above
```

## webhook(message: string, username: string, avatar: string)

Allows you to send a message as a webhook, with a specific username and avatar.&#x20;

```lua
local d = require('discord');
d.webhook("Identity theft is not a joke", message.author.username, message.author.avatar);
```


# Command Settings

Command Settings are used in the runWithSettings function.

### pagination.page

The page to start on for all pagination commands. The page number is zero indexed. Starting at a page that doesn't exist leads to bad times

### pagination.disabled

Disables the pagination buttons on paginated commands, so will just display the first page, or the pagination.page if it's set.

### 8ball.rig

Only applies to the !8ball command. Rigs the 8ball response to a specific one, has to be a number between 0 and 14.

### clap.emoji

Only applies to the !clap command. Sets the emoji/character put in between each word.

```lua
local discord = require('discord');
discord.runWithSettings("!clap this has 💀 inbetween", {["clap.emoji": 💀})
-- this💀has💀💀💀inbetween
```

### emoji.count

The number of emojis in the !emoji command, should not be set too high otherwise emojis won't show up.

### spongebob.url

The URL of the image used in the !spongebob command


# HTTP Module

The HTTP Module allows you to make GET requests and receive back information. POST requests are not yet implemented.

## get(url: string)

Returns the `body` and `statusCode` of the response to the request.

```lua
local http = require('http');
local resp = http.get("https://ocelotbot.xyz/");
d.send(resp.statusCode)
```


# JSON Module

Parses JSON

### parse(json: string)

Converts a JSON object into a lua table.

```lua
local j = require('json');
local obj = j.parse('{"field": "value"}');
d.send(obj.field);
```


# Store Module

The store module lets you store and retrieve values on a per-server basis. Values can be set/retrieved by any custom function in that server, regardless of who made it or who executes the function. Key and value length are limited to 191 characters.

## set(key: string, value: any)

Set a value, non-string values will be converted to a string

```lua
local store = require('store');
store.set("myVariable", "value");
return "Value set!";
```

## get(key: string)

Returns a string value stored at `key`

```lua
local store = require('store');
return store.get("myVariable");
```


# Built-in Modules

Built-in modules do not need to be required and are available just through their name. These are native LUA modules and their documentation is linked below

* [table](http://lua-users.org/wiki/TableLibraryTutorial)
* [math](http://lua-users.org/wiki/MathLibraryTutorial)
* [string](http://lua-users.org/wiki/StringLibraryTutorial)


# Making a simple custom command

This guide will take you through making a simple custom command that responds with a fixed message

## Formatting your code

The custom commands require that you enter your code in a specific format, the simplest and quickest way is to just enter your code right after the command:

```
!custom add command test return "One liner"
```

This has drawbacks though, as you can only enter one line and for schedules or autoresponders you can only enter one word as your trigger. To get around this, you can use lua code blocks:

````
!custom add autorespond hello bot
```lua
return "Hello, "..message.author.username
```
````

## Testing your code

Before creating your command, it's useful to try and run the code. This will run as if you had just performed your custom command, with all the same data attached.&#x20;

```
!custom run return "Hello World!"
```

## Adding your command

Once you're happy with your response, you can add it as an actual command using **!custom add**. For example, if you wanted your command to be !hello, you would run:

```
!custom add command hello return "Hello World!"
```

This tells the bot to add a new **command** called **hello** which returns the text **Hello World!**


