I want to make script that use discord api and also can edit messages that sends

Do someone have expirience with this??
I have module script that handle the basic adding event. But I need to make that if you buy the item it wil overwrite the message to sold state.

here is example:

local function PostData(Data)
	local success,err = pcall(function()
		local payload = HttpServ:JSONEncode(Data)
		HttpServ:PostAsync(url,payload)
	end)
	if err then warn(err) return end
end

function DiscordModuleNew.LogBoughtItem(plr:Player, Item:string, Price:string, Amount:string, ItemId:string)
	
	local Data =
		{
			["username"] = "Pepek shop",
			["content"] = "",
			["embeds"] = {{
				["title"] = "(-) New bought item!",
				["description"] = string.format("[%s](https://www.roblox.com/users/%s/profile) bought new item!",plr.Name,plr.UserId),
				["color"] = tonumber(0xff0000),
				["fields"] = {
					{
						["name"] = "__**Item:**__",
						["value"] = string.format("_%s_",Item),
						["inline"] = true
					},
					{
						["name"] = "__**Price:**__",
						["value"] = string.format("_%s KÄŤ_",Price),
						["inline"] = true
					},
					{
						["name"] = "__**Amount:**__",
						["value"] = string.format("_%s Ks_",Amount),
						["inline"] = true
					},
					{
						["name"] = "ItemId:",
						["value"] = string.format("|| ```%s```|| ",ItemId),
						["inline"] = false
					},
				},
				["footer"] = {
					["icon_url"] = "https://cdn.discordapp.com/attachments/1223766428293791795/1284262254581256279/3d-icon-of-info-information-about-free-png.webp?ex=66e5fded&is=66e4ac6d&hm=7f5937a7d127dbc25af41a65ca792a009e9ca9a4a156b2739d3bba26b2b96f2e&",
					["text"] = "PepekShop  | Added:",
				},
				["flags"] = 4100,
				["timestamp"] = DateTime.now():ToIsoDate()
			}},
		}
	PostData(Data)
end

Thank you

You need the message ID to do it, but yes you can. Here’s how:

First, get the bot’s ID and token:

--your base URL looks something like this:
"https://discord.com/api/webhooks/111111/xxxxxx"

--here, 111111 is the webhook's ID and xxxxxx is it's token. You need these.

We can send a HTTP PATCH request to edit the message:

local token = "" --bot token
local id = "" --bot ID, THIS MUST be a string
local baseUrl = "https://www.discord.com/api/webhooks/111111/xxxxxx" --base webhook URL

--we can get the message ID of the message sent by the webhook by adding to the sending function.
--you need to add to your URL used in PostData.
local function PostData(Data)
	local success,err = pcall(function()
		local payload = HttpServ:JSONEncode(Data)
		local response = HttpServ:PostAsync(url.."?wait=true", payload) --adding the wait parameter makes it wait for the message to be sent
        --get the id from the response
        local id = HttpServ:JSONDecode(response).id --this will get the message ID
        --store the message ID somewhere for further use!
	end)
	if err then warn(err) return end
end

Now, we can update it.

local function update(id: string) --id is the message id you stored earlier.
    --add the necessary data to the base URL
    local msgUrl = url.."/messages/"..id
    --we can send a HTTP PATCH request to update the message
    local requestData = {
	    ["Url"] = msgUrl, --indicate our message URL
	    ["Method"] = "PATCH", --sending a HTTP PATCH request
	    ["Headers"] = {
		    ["Content-Type"] = "application/json" --indicate JavaScript Object Notation (JSON) content
	    },
	    ["Body"] = httpService:JSONEncode({
		    ["content"] = "Your new message content should go here."
	    })
    }

    local success, response = pcall(httpService.RequestAsync, httpService, requestData) --run it in protected mode

    if not success then
	    --do stuff with the error thrown by the response
    elseif response.StatusCode ~= 200 then
    	--the request failed, check the code and do stuff appropriately
    else
	    --the request succeeded, check your message to see if it was edited
    end
end

I hope this helps! Let me know if you have any questions.

2 Likes

Thank you so much. You saved me all my brain cells. XD

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.