Requesting help for Discord Webhook script

:wave: Hello, I am currently looking to fix a script of mine.
For whatever reason, whenever I update the Text value (UsernameBox), it still sends a message using the old string.
I’m pretty new to scripting, and no errors are being thrown into the Output.

Also, I’m not putting the actual URL of my Discord webhook here for security reasons.

Code:

   local http = game:GetService("HttpService")

    function onClick()
    local Data = {
    ["content"] = script.Parent.Parent.UsernameBox.Text.." Pressed"}
    Data = http:JSONEncode(Data)
    http:PostAsync("https://discordapp.com/api/webhooks/URL", Data)
end
script.Parent.MouseButton1Down:connect(onClick)

EDIT: This is a Script rather than a LocalScript.

Any attempts, regardless of the outcome, are highly appreciated. Thanks! :grin:

2 Likes

Make sure this is done on the client, not the server. [you’re sending the text to the server via a remote event, and then send the text to Discord on the server]

AKA use your localscript to send the event with the new text to the server, on the server send the text received by the server to Discord.

1 Like

There should be an error. You can’t use HttpService from a LocalScript, so you’re going to need a remote. A remote event, specifically, would work great here.

2 Likes

Thanks for the reply! :grin:

My only question is how would I do this? I’m very new to scripting and don’t really understand how to do this kind of thing, but I believe reading and writing more will help me learn.

I recommend reading this, this’ll explain how to use RemoteEvents/Functions, and will probably be useful to you in the future.

This is all required because of FilteringEnabled

LocalScripts must be ran on the client, for example in tools, guis, startplayerscripts,etc.
Scripts need to be ran on the server, and they can not see unreplicated data from the client.

1 Like

I forgot to mention that this is located inside of a Script rather than a LocalScript, I added it to the post.
Anyways, thanks for the reply :slightly_smiling_face:

So, if you want to use this, I made a whole API for interacting with Discord from Roblox. Here’s the Webhook module: DiscordWebhook.lua (1.5 KB)

(There are some things in the module you can ignore like _bot and _token as those are for Discord Bot stuff with Discord tokens)

Basically you would put this in, require it (require(game.PathToModule)), which gives you the holder, then you can create as many as you would like with the following code:

local webhookManager = require(game:GetService("ServerStorage"):FindFirstChild("DiscordWebhook"));
local webhookOne = webhookManager.new(123, Webhook_Token); --123 would be the channel it's for.
local webhookTwo = webhookManager.new(456, Webhook_Token2); --456 would be the channel it's for.

(This is a server script. To hook it up to the client do as @sjr04 said and use a RemoteEvent to fire the data over to the server to send to Discord through the webhook.)

--Client
  --Rest of onClick
  game.ReplicatedStorage.RemoteEvent:FireServer(script.Parent.Parent.UsernameBox.Text);
end
script.Parent.MouseButton1Down:Connect(onClick)
--Server
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, username)
  webhookOne:Execute({content = username .. " Pressed";});
end);
2 Likes

This is more concerning. A server script should not be handling GUIs. Please use a LocalScript to do that, then send the text to the server via a remote.

1 Like

@sjr04 Forgot to mention why it’s more concerning. You can’t access client input events like mouse button or touch from server scripts. It has to be a local script.

2 Likes

Okay, thank you for helping :slightly_smiling_face:

Thank you so much for helping out! :grin:
However, I’m still quite confused, I’m presuming I should insert “DiscordWebhook.lua” as a ModuleScript into ServerStorage and add my webhook URL into the Webhooks table… but I don’t understand where to go from there, sorry for my personal confusion.

After you create a webhook

local ourWebhook = require(MODULE).new(ChannelId, WebhookToken) --Returns a lua object that has the method :Execute()
--ourWebhook:Execute() will fire the webhook to the Discord api after JSON encoding the table of data inside.
--Making it easy for you to have content, usernames, pictures, etc. As you just add it as an index in the table.
--For the basic call you're doing you simply include content as a key, with your content.
local dataToSend = {
  content = "Hello Wold!"; --Set our content to "Hello World!"
};

ourWebhook:Execute(dataToSend); --Send our content to the Discord API

It’s basically just a pre-made, don’t have to make your own, function for firing the Discord API.
This is all the code is behind the scenes.

function Webhook:Execute(data)
	local succ, err = pcall(function()
		local response = httpService:RequestAsync({
			Url = string.format("https://discordapp.com/api/webhooks/%s/%s", tostring(self._channelId), tostring(self._webhookToken));
			Method = "POST";
			Headers = {
				['Content-Type'] = "application/json";
			};
			Body = httpService:JSONEncode(data)
		});
		
		if not response.Success then
			self.warn("The request failed:", response.StatusCode, response.StatusMessage);
		end;
	end);
	
	if not succ then
		self.warn("Http Request Failed:",err);
	end;
end;