Jump to content

[HELP] Send webhook on Discord using fetchRemote


Dzardys

Recommended Posts

Hi,
I've been learning scripting recently and I'm struggling right now when trying to send a Discord message using a fetchRemote() function. I think I did nothing wrong when initializing the webhook, but still it doesn't send anything. Is there anyone, who might know the cause of this problem?

 

function testWebhook()
    local url = "https://discordapp.com/api/webhooks/..."
    if url then
        sendOptions = {
            queueName = "default",
            connectionAttempts = 3,
            connectTimeout = 5000,
            formFields = {
                content = "test"
            },
        }
        fetchRemote (url, sendOptions, function()end)
    else
        outputDebugString("Webhook not provided.");
    end
end
addEventHandler("onResourceStart", getResourceRootElement(), testWebhook)

Thanks.

Link to comment

Firstly place your resource in acl as admin (bad practice but to test just do it)

<group name="Admin">
<object name="resource.dt"></object>
</group>

I have named my resource "dt"

now in server.lua place this

sendOptions = {
    queueName = "Discord Test Queue",
    connectionAttempts = 3,
    connectTimeout = 5000,
    formFields = {
        username= "your webhook bot name",
	    avatar_url= "",
    	content= "Hello World"
    }
}

function callback(data, info)
    iprint( "Discord Message Sent:" .. " success:" .. tostring(info.success) .. " statusCode:" .. tostring(info.statusCode) .. " data:" .. tostring(data))
end

fetchRemote( "your webhook url", sendOptions, callback )

Enjoy :) 

Edited by Ayush Rathore
  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...