Skip to content

nss_libs - Discord ​

Creates an API to send messages to a Discord channel.

WARNING

The Discord API is (for security reasons) only available on server side.


Examples ​

You can decide if you want to create a Discord API object based only on a webhook url (one hook + multiple players) or based on a specific player (one hook + one player).

One hook + one player ​

In this case an API object is created for a specific player and avatar image. This API object is used to send messages in the name of that specific player given in API object creation.

lua
-- Creates a reusable Discord API object. In this example for the example player "John Doe".
---@type NssLibsDiscordApi
local discord_api = exports.nss_libs:getDiscordApi(
        GetCurrentResourceName(),
        'WEBHOOK_URL',
        'USERNAME_OR_PLAYER_NAME_OR_BOT_NAME', -- E.g. as example the current player name "John Doe"
        'AVATAR_URL'
)

-- Send a message in the name of the example player "John Doe"
discord_api.message('Cheese stinks!')

Citizen.Wait(1000)

-- Send another message in the name of the example player "John Doe"
discord_api.message('Cheese still stinks!')

Citizen.Wait(1000)

-- Special case for method 'message': Alternative webhook URL
-- Send another message in the name of the example player "John Doe" to a different webhook url (but the alternate
-- webhook url is only used for this message)
discord_api.message('Player loves stinking cheese!', 'ALTERNATE_WEBHOOK_URL')

One webhook + multiple players ​

lua
-- Creates a reusable Discord API object.
---@type NssLibsDiscordApi
local discord_api = exports.nss_libs:getDiscordApi(
        GetCurrentResourceName(),
        'WEBHOOK_URL'
)

-- Create a function to simplify sending messages
---@param username string
---@param avatar string
---@param message string
local sendMessage = function(username, avatar, message)
    discord_api.setUsername(username)
    discord_api.setAvatarUrl(avatar)
    discord_api.message(message)
end

sendMessage('John Doe', 'https://example.com/john_doe_avatar.png', 'Cheese stinks!')

Citizen.Wait(1000)

sendMessage('John Doe', 'https://example.com/john_doe_avatar.png', 'Cheese still stinks!')

Citizen.Wait(1000)

sendMessage('Jack Blue', 'https://example.com/jack_blue_avatar.png', 'Cheese still!')
lua
-- Creates a reusable Discord API object.
---@type NssLibsDiscordApi
local discord_api = exports.nss_libs:getDiscordApi(
        GetCurrentResourceName(),
        'WEBHOOK_URL'
)

-- Create a function to simplify sending messages
-- This function uses the request method instead of the simplified message method. The request object is used in later
-- examples, e.g. for embeds.
---@param username string
---@param avatar string
---@param message string
local sendMessage = function(username, avatar, message)
    local request = discord_api.create(username, avatar, message)
    request.send()
end

sendMessage('John Doe', 'https://example.com/john_doe_avatar.png', 'Cheese stinks!')

Citizen.Wait(1000)

sendMessage('John Doe', 'https://example.com/john_doe_avatar.png', 'Cheese still stinks!')

Citizen.Wait(1000)

sendMessage('Jack Blue', 'https://example.com/jack_blue_avatar.png', 'Cheese still!')

Example with modifiable embeds ​

lua
-- Creates a reusable Discord API object. In this example for the example player "John Doe".
---@type NssLibsDiscordApi
local discord_api = exports.nss_libs:getDiscordApi(
        GetCurrentResourceName(),
        'WEBHOOK_URL',
        'USERNAME_OR_PLAYER_NAME_OR_BOT_NAME', -- E.g. as example the current player name "John Doe"
        'AVATAR_URL'
)

-- Creates a Discord request object for the example player "John Doe".
---@type NssLibsDiscordRequestApi
local request = discord_api.create()

-- Creates a Discord embed object
---@type NssLibsDiscordRequestEmbedApi
local embed = request.addEmbed()
embed.setTitle('Cheese stinks!')
embed.setColor('#000000')
embed.setDescription('Everyone loves you.')

-- Sends the request
request.send()

Methods ​

exports.nss_libs:getDiscordApi(resource_name, webhook_url, username?, avatar_url?) ​

Creates the Discord API object.

ParamTypeDefaultDescription
resource_namestring—The name of the resource that uses the Discord API (used for console identification)
webhook_urlstring—The webhook url of the Discord channel
usernamestring?—The username of the Discord bot or the player name
avatar_urlstring?—The avatar url of the Discord bot or the player avatar

Returns NssLibsDiscordApi


NssLibsDiscordApi ​

.create(username?, avatar_url?, message?) ​

Creates a request object for sending Discord messages.

ParamTypeDefaultDescription
usernamestring?—Overrides the username given in getDiscordApi
avatar_urlstring?—Overrides the avatar url given in getDiscordApi
messagestring?—The default message body, can be nil if using embeds only

Returns NssLibsDiscordRequestApi


.message(message, temporary_webhook_url?) ​

Sends a simple message to the Discord channel.

ParamTypeDefaultDescription
messagestring—The message shown in the default message body
temporary_webhook_urlstring?—Overrides the webhook url for this message only

.setUsername(username) ​

Sets the username for subsequent messages.

ParamTypeDefaultDescription
usernamestring—The username of the Discord bot or the player name

.setAvatarUrl(avatar_url) ​

Sets the avatar url for subsequent messages.

ParamTypeDefaultDescription
avatar_urlstring—The avatar url of the Discord bot or the player

.setWebhookUrl(webhook_url) ​

Sets the webhook url for subsequent messages.

ParamTypeDefaultDescription
webhook_urlstring—The webhook url of the Discord channel

NssLibsDiscordRequestApi ​

.setUsername(username) ​

Sets the username for that specific request.

ParamTypeDefaultDescription
usernamestring—The username of the Discord bot or the player name

.setAvatarUrl(avatar_url) ​

Sets the avatar url for that specific request.

ParamTypeDefaultDescription
avatar_urlstring—The avatar url of the Discord bot or the player

.setContent(message) ​

Sets the message content for that specific request.

ParamTypeDefaultDescription
messagestring—The message shown in the default message body

.enableTextToSpeech() ​

Enables text to speech for that specific request.


.addEmbed() ​

Adds an embed object to the request.

Returns NssLibsDiscordRequestEmbedApi


.send(on_response?) ​

Sends the request.

ParamTypeDefaultDescription
on_responsefunction?—Callback called after the request completes

The callback receives the following arguments:

ParamTypeDescription
erranyError if the request failed
textstringResponse body text
headerstableResponse headers
webhook_urlstringThe webhook URL used for this request
lua
request.send(function(err, text, headers, webhook_url)
    if err then
        print('Discord webhook failed:', err)
    else
        print('Message sent to:', webhook_url)
    end
end)

NssLibsDiscordRequestEmbedApi ​

TIP

See Discord webhook documentation for more information about Discord message embeds.

.setTitle(title) ​

Sets the title of the embed.

ParamTypeDefaultDescription
titlestring—The title of the embed

.setAuthor(name, url?, icon_url?) ​

Sets the author of the embed.

ParamTypeDefaultDescription
namestring—The name of the author
urlstring?—The url of the author (name becomes a hyperlink)
icon_urlstring?—The icon url of the author

.setDescription(description) ​

Sets the description (content) of the embed.

ParamTypeDefaultDescription
descriptionstring—The description of the embed

.setUrl(url) ​

Sets the url of the embed.

ParamTypeDefaultDescription
urlstring—The url of the embed

.setColor(color) ​

Sets the color of the embed.

ParamTypeDefaultDescription
colorstring—Hex color code or decimal color code

.setImage(url) ​

Sets the image of the embed.

ParamTypeDefaultDescription
urlstring—The url of the image

.setThumbnail(url) ​

Sets the thumbnail of the embed.

ParamTypeDefaultDescription
urlstring—The url of the thumbnail

.setFooter(text, icon_url?) ​

Sets the footer of the embed.

ParamTypeDefaultDescription
textstring—The text of the footer
icon_urlstring?—The icon url of the footer

.setTimestamp(utc_timestamp?) ​

Sets the timestamp of the embed.

ParamTypeDefaultDescription
utc_timestampstring?—ISO-8601 timestamp (YYYY-MM-DDTHH:MM:SS.000Z), uses current UTC time if nil

.addField(name, value, inline?) ​

Adds a field to the embed.

ParamTypeDefaultDescription
namestring—The name of the field
valuestring—The value of the field
inlineboolean?—If the field should be inline

NIGHTSHIFT STUDIO Documentation