Webhook Discord

Tutorial to help set up the discord webhook.

It has notification functions to start, save, shut down, and raids.

Credits:

wizardlink (Discord Webhook)

Majesty (Tutorial)

1 - Configuration - Discord:

  • Create a server in Discord for your project/community.

  • Access the server configuration:

  • Access Integrations, Webhooks and click the Create webhook button:

  • Set up a name for the webhook and channel for notifications:

  • Copy the webhook URL.

2 - Configuration - Server:

  • Open the file config.lua / config.lua.dist server OTServBR-Global.

  • Look for:

-- Sends Discord webhook notifications on startup, raids and shutdown.
-- The URL layout is https://discord.com/api/webhooks/:id/:token
-- Leave empty if you wish to disable.
discordWebhookURL = ""
  • Add the URL of your webhook, example:

-- Sends Discord webhook notifications on startup, raids and shutdown.
-- The URL layout is https://discord.com/api/webhooks/:id/:token
-- Leave empty if you wish to disable.
discordWebhookURL = "https://discord.com/api/webhooks/811627385782403083/6hrWa2Aq38i4Ct17_l4D1mTr6YZjYRplKrvUYkvV6OuoPHDiJoi8bxPdcjTik4scQu37"

3 - Result:

4 - Additional Information:

  • The webhook system comes pre-installed in OTServBR-Global for various functions such as save, close, intrusions, etc., but is fully flexible to use in any type of notification that you would like your users to receive on your discord server. It runs with the function:

Webhook.send("Title", "message", WEBHOOK_COLOR_WARNING)

The color can also be changed, the ones available in OTServBR-Global are as follows (with possibility of expansion):

enum Webhook_Colors_t : uint32_t {
    WEBHOOK_COLOR_ONLINE = 0x00FF00,
    WEBHOOK_COLOR_OFFLINE = 0xFF0000,
    WEBHOOK_COLOR_WARNING = 0xFFFF00,
    WEBHOOK_COLOR_RAID = 0x0000FF
};

5 - Special Webhooks:

  • With this you configure channels webhooks in discord and paste the discord api url in this file:

-- Sends Discord webhook special notifications.
-- The URL layout is https://discord.com/api/webhooks/:id/:token
-- Leave empty if you wish to disable.

announcementChannels = {
    ["serverAnnouncements"] = "", -- Used for an announcement channel on your discord
    ["raids"] = "", -- Used to isolate raids on your discord
    ["player-kills"] = "", -- Self-explaining
}

-- Example of notification (After you do the config):
-- Webhook.send("Server save", message, WEBHOOK_COLOR_WARNING, announcementChannels["serverAnnouncements"]) -- This is going to send a message into your server announcements channel

--[[ 
	Dev Comment: This lib can be used to add special webhook channels 
	where you are going to send your messages. Webhook.specialSend was designed
	to be used with countless possibilities.	
]]

Examples:

Broadcast:

local broadcast = TalkAction("/b")
function broadcast.onSay(player, words, param)
    if not player:getGroup():getAccess() or player:getAccountType() < ACCOUNT_TYPE_GOD then
        return true
    end

    if param == "" then
        player:sendCancelMessage("Command param required.")
        return false
    end

    print("> " .. player:getName() .. " broadcasted: \"" .. param .. "\".")
    for _, targetPlayer in ipairs(Game.getPlayers()) do
        targetPlayer:sendPrivateMessage(player, param, TALKTYPE_BROADCAST)
    end    
   Webhook.send(player:getName().." has broadcasted", param, WEBHOOK_COLOR_WARNING, announcementChannels["serverAnnouncements"])
return false
end

broadcast:separator(" ")
broadcast:register()

OpenServer:

local openServer = TalkAction("/openserver")

function openServer.onSay(player, words, param)
    if not player:getGroup():getAccess() or player:getAccountType() < ACCOUNT_TYPE_GOD then
        return true
    end

Game.setGameState(GAME_STATE_NORMAL)     
player:sendTextMessage(MESSAGE_ADMINISTRADOR, "Server is now open.")
Webhook.send("Server Open", "Server is now open", WEBHOOK_COLOR_ONLINE, announcementChannels["serverAnnouncements"])
    return false
end

openServer:separator(" ")
openServer:register()

Ban:

local banDays = 7

local ban = TalkAction("/ban")

function ban.onSay(player, words, param)

	if not player:getGroup():getAccess() or player:getAccountType() < ACCOUNT_TYPE_GOD then
		return true
	end

	if param == "" then
		player:sendCancelMessage("Command param required.")
		return false
	end

	local name = param
	local reason = ''

	local separatorPos = param:find(',')
	if separatorPos then
		name = param:sub(0, separatorPos - 1)
		reason = string.trim(param:sub(separatorPos + 1))
	end

	local accountId = getAccountNumberByPlayerName(name)
	if accountId == 0 then
		return false
	end

	local resultId = db.storeQuery("SELECT 1 FROM `account_bans` WHERE `account_id` = " .. accountId)
	if resultId ~= false then
		result.free(resultId)
		return false
	end

	local timeNow = os.time()
	db.query("INSERT INTO `account_bans` (`account_id`, `reason`, `banned_at`, `expires_at`, `banned_by`) VALUES (" ..
			accountId .. ", " .. db.escapeString(reason) .. ", " .. timeNow .. ", " .. timeNow + (banDays * 86400) .. ", " .. player:getGuid() .. ")")

	local target = Player(name)
	if target then
		player:sendTextMessage(MESSAGE_ADMINISTRADOR, target:getName() .. " has been banned.")
		Webhook.send(name.. " has been banned", "reason: " ..reason.. ".", WEBHOOK_COLOR_WARNING, announcementChannels["serverAnnouncements"])
		target:remove()
	else
		player:sendTextMessage(MESSAGE_ADMINISTRADOR, name .. " has been banned.")
	end
end

ban:separator(" ")
ban:register()

Player Record:

local playerrecord = GlobalEvent("playerrecord")
function playerrecord.onRecord(current, old)
	addEvent(Game.broadcastMessage, 150, 'New record: ' .. current .. ' players online.', MESSAGE_EVENT_ADVANCE)
	Webhook.send("New record online", "Player count: " .. current, WEBHOOK_COLOR_ONLINE, announcementChannels["serverAnnouncements"])
	return true
end
playerrecord:register()