Discord Webhook


  • Today I'm going to show you how to make a event fire a message into a Discord chat.

    1. Make A Webhook
      Go to your server and press server settings
      Go to webhooks
      Press the button that says create webhook
      Custimize it to your liking
      Find the url it gives you and paste it into the seach bar
      Your going to need the token and the id
    2. Module Script (Replicated Storage)

    Code:

    local webHook = {}
    webHook.__index = webHook
    
    function webHook.new(id,key) 
    	local mt = setmetatable({id=id,key=key}, webHook) 
    	return mt
    end
    
    function webHook:post(config,host)
    	local data = nil
    	local success = pcall(function()
    		data = game:GetService("HttpService"):JSONEncode(config)
    	end)
    	if not success then warn("Webhook can't be converted to JSON") return end
    	game:GetService("HttpService"):PostAsync("https://discordapp.com/api/webhooks/"..self.id.."/"..self.key,data)
    end
    
    return webHook
    

    This script will get the Discord API so you can post messages

    1. Server Script
    local APIModule  = require(game.ReplicatedStorage.WebhookAPI)
    
    
    
    local Player = APIModule.new("Id","Token")
    
    
    
    local players = 0
    
    game.Players.PlayerAdded:connect(function(Player)
    	players =  players+1
    Player:post{
    			username = "Player Joinned",
    			content = "**" .. Player.Name .. "** Has Joinned ".. game.Name .. " And Now Are ".. players .. " Player(s)"
    		}
    end)
    
    game.Players.PlayerRemoving:Connect(function(Player)
    	players = players-1
    Player:post{
    			username = "Player Left",
    			content = "**" .. Player.Name .. "** Has Left ".. game.Name .. " And Now Are ".. players .. " Player(s)"
    		}
    end)
    
    
    
    

    Change The Id And Token To Yours
    The Script Will Tell The Webhook To Post A Message
    The Content Is What The Webhook Will Say
    The Username Is The Name That Is Displayed


  • @CarMaster17 why do you use connect for one and Connect for the other, that's inconsistent. make both Connect


  • Because I dont care its depreciated and I use it in all my scripts


  • @CarMaster17 Consistency is important. Even if it doesn't affect the result of the code it's important to be consistent.

    "Because I dont care its depreciated and I use it in all my scripts"

    *deprecated

    And you know it's deprecated? So you're voluntarily choosing to break your scripts should connect ever be removed?

  • Global Moderator

    @incapaz Yeah, some people are very stubborn.

    Suit yourself. Keep writing bad code then. One of your games is bound to break at some point. @CarMaster17


  • This post is deleted!
Log in to reply
 

Looks like your connection to Scripting Helpers was lost, please wait while we try to reconnect.