[How-To] Send Discord Webhooks

Hi,

You probably know the default way of sending a message to discord if not look here. Now I’ve been working on some stuff that handles Discord Embeds (that I will talk about later) (and here are their limits). And because I want you all to learn something I will explain it in here.

For this tutorial, we’re going to make a basic function that lets us send embed’s easier for that were going to make a function. As given in the basic tutorial on how to send Discord Messages were going to edit a few things about that. Let us start with the PerformHttpRequest

PerformHttpRequest('DISCORD_URL', function(err, text, headers) end, 'POST', json.encode({username = name, content = message}), { ['Content-Type'] = 'application/json' })

Now to have an embedded message we are going to change content = message to embeds = embed now for this tutorial we’re going to make a basic function first

function sendToDiscord()
  PerformHttpRequest('DISCORD_URL', function(err, text, headers) end, 'POST', json.encode({username = name, embeds = embed}), { ['Content-Type'] = 'application/json' })
end

Now this, of course, wouldn’t work because it doesn’t know what name and embed is. We’re going to fix name later, let us make the embed first.

function sendToDiscord()
  local embed = {
        {
            --stuff
        }
    }

  PerformHttpRequest('DISCORD_URL', function(err, text, headers) end, 'POST', json.encode({username = name, embeds = embed}), { ['Content-Type'] = 'application/json' })
end

Now where stuff is located were going to place some information about the message. Like the color on the left side and the title and the text it contains. For Lua it is like any other language but it doesn’t really use fields, i’ven’t tried it though. here is an example. So let us use this example for our own embed

function sendToDiscord()
  local embed = {
        {
            ["color"] = color,
            ["title"] = "**".. name .."**",
            ["description"] = message,
            ["footer"] = {
                ["text"] = footer,
            },
        }
    }

  PerformHttpRequest('DISCORD_URL', function(err, text, headers) end, 'POST', json.encode({username = name, embeds = embed}), { ['Content-Type'] = 'application/json' })
end

Now were still going to need some inputs before it could work. as you can see we need: color, name, message, footer. This can be done in any way so let us do that in the () in sendToDiscord() because there we need to put it.

function sendToDiscord(color, name, message, footer)
  local embed = {
        {
            ["color"] = color,
            ["title"] = "**".. name .."**",
            ["description"] = message,
            ["footer"] = {
                ["text"] = footer,
            },
        }
    }

  PerformHttpRequest('DISCORD_URL', function(err, text, headers) end, 'POST', json.encode({username = name, embeds = embed}), { ['Content-Type'] = 'application/json' })
end

Before we can put anything in this function we need some understanding in what the it needs.

For color we need a DECIMAL color, like orange = 16753920.
For name we need a STRING like "System"
For message we need a STRING like "The apple doesn't fall far from the tree"
For footer we need a STRING like "Made by Tazio"

As you can see we use name for the title and username you can change that if you like.
and to active, here is an example

sendToDiscord(16753920, "System", "Look an apple", "Made by Tazio")

I hope y’all learned something new and if you see any error please comment, don’t forget to :heart: if it helped you and have a good day. And here is how I use Discord Embeds.

-Tazio

19 Likes

When doing embeds [" "] is not required. color, title, footer, etc. work just fine without.

1 Like

This is why Tazio is [GOD]

1 Like

I dont know but I get the message doubled, like if i type in chat “test” i will get on Discord:
“test”
“test”

You might be calling the function 2 times

fixed. i had a disable chat script and that’s why.

1 Like

function sendToDiscord(color, name, message, footer)
local embed = {
{
[“color”] = color,
[“title”] = “"… name …"”,
[“description”] = message,
[“footer”] = {
[“text”] = footer,
},
}
}
I am trying to get this tekst more then 2 lines. i use it for chat logging, and the rp chat only gives us the RP-name. I wanted to add a line more to the content. like the steam name or at least the ingame ID to be able to track ppl that abuse the chat. I just cant see how. Can you help me?

It’s best to refer to the Discord API documentation. In this particular example, you’re sending an embed object.

For example, you could use embed fields to display multiple lines of content.

1 Like

I almost spent 5h trying to figure out how to do it but i could not.
Can you please help me?

bad :heart:

1 Like

hello, I also commented a section of script to no longer have duplicate logs in the discord, I made an update and I still have double logs in the discord … maybe tell me what to do or which sections to comment on please?

Maybe you have the function twice

Its not working on my server. I get a error msg " object is not a string “PerformHttpRequest” "!

can anyone help?

How can I make another line?

Hello, u can use something like that:
sidenote: you dont need anything sending from clientside to the serverside exept this:

TriggerServerEvent('factionrp:ugh9h9823r8wfgnw', message)

local function ExtractIdentifiers(playerId)
    local identifiers = {}

    for i = 0, GetNumPlayerIdentifiers(playerId) - 1 do
        local id = GetPlayerIdentifier(playerId, i)

        if string.find(id, "steam:") then
            identifiers['steam'] = id
        elseif string.find(id, "discord:") then
            identifiers['discord'] = id
        elseif string.find(id, "license:") then
            identifiers['license'] = id
        elseif string.find(id, "license2:") then
            identifiers['license2'] = id
        end
    end

    return identifiers
end

RegisterServerEvent('factionrp:ugh9h9823r8wfgnw')
AddEventHandler('factionrp:ugh9h9823r8wfgnw', function(message)
    local playerId = source
    local playerName = GetPlayerName(playerId)
    local playerServerId = playerId
    local identifiers = ExtractIdentifiers(playerId)
    local date = os.date('*t')
    if date.day < 10 then date.day = '0' .. tostring(date.day) end
    if date.month < 10 then date.month = '0' .. tostring(date.month) end
    if date.hour < 10 then date.hour = '0' .. tostring(date.hour) end
    if date.min < 10 then date.min = '0' .. tostring(date.min) end
    if date.sec < 10 then date.sec = '0' .. tostring(date.sec) end
    local chatlogs = {
         {
              ['color'] = '16711680', -- color red
              ['title'] = 'Chatlog',
              ['description'] = 'Information',


              fields = {
                { name = "Steamname", value = playerName },
                { name = "Discord", value = identifiers['discord'] or "dosen´t exist or not available" },
                { name = "Steam", value = identifiers['steam'] or "dosen´t exist or not available" },
                { name = "Server ID", value = playerServerId },
                { name = "License", value = identifiers['license'] or "dosen´t exist or not available" },
                { name = "Command", value = message },
              },
              ['footer'] = {
                   ['icon_url'] = ' your_icon_URL',
                   ['text'] = 'Date: '.. date.day ..'/'.. date.month ..'/'.. date.year ..' - '.. date.hour ..':'.. date.min ..':'.. date.sec ..'',
              },
         }
    }
     PerformHttpRequest('Your_Webhook_URL', function(err, text, headers) end, 'POST', json.encode({username = 'FACTIONRP | CHATLOG', embeds = chatlogs}), { ['Content-Type'] = 'application/json' })
end)