Mudlet Discord Webhook Support

Share your scripts and packages with other Mudlet users.
Post Reply
SynecticLabs
Posts: 1
Joined: Thu Jul 16, 2020 6:49 pm

Mudlet Discord Webhook Support

Post by SynecticLabs »

The function below will allow a Mudlet user to send a message to a Discord server via a webhook.

Code: Select all

function sendToDiscordWebhook(discord_username, avatarurl, message)
  local httpdone = registerAnonymousEventHandler('sysPostHttpDone', 
    function(event, rurl, response)
      if rurl == url then display(r) else return true end
    end, true)

  local httperror = registerAnonymousEventHandler('sysPostHttpError', 
    function(event, response, rurl)
      if rurl == url then display(r) else return true end
    end, true)

  local url = ""
  local data = {
    username = discord_username, 
    avatar_url = avatarurl,
    content = message
  }
  local header = {
    ["Content-Type"] = "application/json",
    --["Content-Length"] = data:len()
  }
  
  postHTTP(yajl.to_string(data), url, header)
  
  killAnonymousEventHandler(httpdone)
  killAnonymousEventHandler(httperror)
end
Place this into a script object, then insert your webhook endpoint into the url variable.

To send a message, you can do something akin to:

Code: Select all

sendToDiscordWebhook("MyBotName", false, "My boring message.")
Put this into a trigger to do fancy things like announcing kills, forwarding clan tells into your server, etc.

Enjoy!

Post Reply