Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
ThatKid

Discord Webhook Program

Recommended Posts

I'm wondering if there are any successful Discord Webhook programs that work for OpenOS. I can't find anything using google or this forum except a Computercraft program which works successfully, but requires Computercraft's textutils and http APIs.

I'm new to programming and have no idea what I'm doing or what I should be doing, but I've tried debunking what CraftOS commands are incompatible with OpenOS and looked at what I can use to replace them, writing CraftOS's bios to an EEPROM (it's too big), booting into CraftOS from OpenOS, and running a CraftOS emulator in OpenOS. The latter few are clearly too big of projects for me which is why I'm asking for a step in the right direction.

I'm not going to do nothing and wait for a response, I'm going to keep learning lua, OpenOS, and CraftOS to see what I can do but I doubt I'll get anywhere soon.

Link to post
Share on other sites

Hey

based on the program you linked, I made a discord webhook api that should work with OpenOS. But I haven't tested it yet, because I don't have a discord server. So try it and tell me if you run into any issues.

First, download a JSON lib:

wget https://raw.githubusercontent.com/CptMercury/lunar-json/main/json.lua lunar-json.lua

This is the api:

--****************************************************************************--
-- # loosly based on HydroNitrogen's Discord webhook api for ComputerCraft
-- # See: https://github.com/Wendelstein7/DiscordHook-CC
--****************************************************************************--
local checkArg, error = checkArg, error
local byte, format = string.byte, string.format
--****************************************************************************--
local component = require "component"
local inet = require "internet"
local ljson = require "lunar-json"
--****************************************************************************--
local discord = {}

local msg_header = {
  ["Content-Type"] = "application/x-www-form-urlencoded",
  ["Source"] = "Minecraft/OpenComputers/Discord"
}
local json_header = {
  ["Content-Type"] = "application/json",
  ["Source"] = "Minecraft/OpenComputers/Discord"
}

local function escape(s)
  return s:gsub('[+=&%%%c]', function (c)
    return format('%%%02X', byte(c))
  end)
end

function discord.createWebhook(url)
  checkArg(1, url, "string")

  if not component.isAvailable("internet") then
    error("Discord Webhook requires an internet card to operate.")
  end

  local webhook = {
    url = url
  }

  function webhook.send(msg, username, avatar)
    checkArg(1, msg, "string")
    checkArg(2, username, "string", "nil")
    checkArg(3, avatar, "string", "nil")
    msg = escape(msg)

    if username then
      msg = msg.."&username="..escape(username)
    end

    if avatar then
      msg = msg.."&avatar_url="..escape(avatar)
    end

    local res, msg = inet.request(url, msg, msg_header)
    return res and true or false
  end

  function webhook.sendJSON(json)
    checkArg(1, json, "string")
    local res, msg = inet.request(url, json, json_header)
    return res and true or false
  end

  function webhook.sendEmbed(msg, title, desc, link, color, img, img_thumb,
                             username, avatar)
    checkArg(1, msg, "string")
    checkArg(2, title, "string", "nil")
    checkArg(3, desc, "string", "nil")
    checkArg(4, link, "string", "nil")
    checkArg(5, color, "number", "nil")
    checkArg(6, img, "string", "nil")
    checkArg(7, img_thumb, "string", "nil")
    checkArg(8, username, "string", "nil")
    checkArg(9, avatar, "string", "nil")

    local data = {
      content = msg,
      username = username,
      avatar_url = avatar,
      embeds = {
        {
          title = title,
          description = desc,
          link = link,
          color = color
        }
      }
    }

    if img then
      data.embeds[1].image = {url = img}
    end
    if img_thumb then
      data.embeds[1].thumbnail = {url = img_thumb}
    end

    local success, json = ljson.encode(data, true, false, true, true, true)
    if not success then
      error("Unable to encode data as JSON")
    end

    local res, msg = inet.request(url, json, json_header)
    return res and true or false
  end

  return webhook
end
--****************************************************************************--
return discord

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.