{"id":13410570,"url":"https://github.com/Mehgugs/lacord","last_synced_at":"2025-03-14T16:32:25.781Z","repository":{"id":49808882,"uuid":"210912737","full_name":"Mehgugs/lacord","owner":"Mehgugs","description":"A low level, lightweight discord API library.","archived":false,"fork":false,"pushed_at":"2022-09-12T21:21:15.000Z","size":361,"stargazers_count":6,"open_issues_count":5,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-07-31T20:43:10.343Z","etag":null,"topics":["discord","discord-bot","discord-library","lua"],"latest_commit_sha":null,"homepage":"","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Mehgugs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-25T18:24:08.000Z","updated_at":"2024-07-20T02:01:37.000Z","dependencies_parsed_at":"2023-01-18T05:15:40.529Z","dependency_job_id":null,"html_url":"https://github.com/Mehgugs/lacord","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mehgugs%2Flacord","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mehgugs%2Flacord/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mehgugs%2Flacord/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mehgugs%2Flacord/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Mehgugs","download_url":"https://codeload.github.com/Mehgugs/lacord/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243610259,"owners_count":20318931,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["discord","discord-bot","discord-library","lua"],"created_at":"2024-07-30T20:01:07.739Z","updated_at":"2025-03-14T16:32:25.314Z","avatar_url":"https://github.com/Mehgugs.png","language":"Lua","funding_links":[],"categories":["Libraries"],"sub_categories":["Lua"],"readme":"## lacord\n\nlacord is a small discord library providing low level clients for the discord rest and gateway API.\nAll data is given to the user as raw JSON.\n\nDocumentation is sparsely provided in the form of LDoc comments which can be processed into a document using LDoc.\nThere's hand written documentation in markdown format [here](docs) which can be viewed online [here](https://mehgugs.github.io/lacord/index.html).\n\n## Example\n\nThis example sends lines inputed at the terminal to discord over a supplied webhook.\n\n```lua\nlocal api = require\"lacord.api\"\nlocal cqs = require\"cqueues\"\nlocal errno = require\"cqueues.errno\"\nlocal thread = require\"cqueues.thread\"\nlocal logger = require\"lacord.util.logger\"\nlocal webhook = require\"lacord.cli\".webhook\n\nlocal webhook_id, webhook_token = webhook:match\"^(.+):(.+)$\"\n\nlocal loop = cqs.new()\n\nlocal discord = api.new_webhook(webhook_id,webhook_token)\n\nlocal function starts(s, prefix)\n    return s:sub(1, #prefix) == prefix\nend\n\nlocal function suffix(s, pre)\n    local len = #pre\n    return s:sub(1, len) == pre and s:sub(len + 1) or s\nend\n\nlocal thr, con = thread.start(function(con)\n    print\"Write messages to send over the webhook here!\"\n    for input in io.stdin:lines() do\n        if input == \":quit\" then break end\n        con:write(input, \"\\n\")\n    end\nend)\n\nloop:wrap(function()\n    local username = \"lacord webhook example\"\n    for line in con:lines() do\n        if starts(line, \":\") then\n            if starts(line, \":username \") then\n                username = suffix(line, \":username \")\n            end\n        else\n            local success = discord:execute_webhook{\n                content = line,\n                username = username,\n            }\n            if not success then io.stdin:write\":quit\" break end\n        end\n    end\n\n    local ok, why = thr:join()\n\n    if not ok then logger.error(\"error in reader thread (%s, %q)\", why, errno.strerror(why)) end\nend)\n\nassert(loop:loop())\n```\n\n## CDN Client Example\n\n```lua\nlocal cqs = require\"cqueues\"\nlocal api = require\"lacord.api\"\nlocal cdn = require\"lacord.cdn\"\nlocal util = require\"lacord.util\"\n\nlocal loop = cqs.new()\n\nlocal discord_api = api.init{\n    token = \"Bot \"..require\"lacord.cli\".token\n   ,accept_encoding = true\n   ,track_ratelimits = false\n   ,route_delay = 0\n}\n\nlocal a_cdn = cdn.new{\n    accept_encoding = true\n}\n\nloop:wrap(function()\n    local success, data =  discord_api:get_current_user()\n    if success then\n        local avatar = a_cdn:get_user_avatar(data.id, data.avatar, 'png')\n        local fname, content = util.blob_for_file(avatar, \"avatar\")\n        local fd\u003cclose\u003e = io.open(fname, \"wb\")\n        fd:write(content)\n    end\nend)\n\nassert(loop:loop())\n```\n\n## Installation\n\nThis project depends on [`lua-http`](https://github.com/daurnimator/lua-http) and thus [`cqueues`](https://25thandclement.com/~william/projects/cqueues.html). This means that you must\nbe able to install `cqueues` on your platform.\n\nYou can consult the respective projects for\ndetailed instructions but as a general guide the following tools/libraries should be installed and available on your system:\n\n- m4\n- awk\n- zlib-dev\n- libssl-dev (or equiv.)[¹](#note-1)\n\nOnce you have the pre-requisites in order you can install this library with luarocks:\n\n- Directly `luarocks install lacord`\n- Via this repository\n    - `git clone https://github.com/Mehgugs/lacord.git \u0026\u0026 cd lacord`\n    - optionally checkout a specific commit\n    - `luarocks make`\n\n## Slash Commands\n\nThis library provides support for slash commands naturally over the gateway and\nalso provides a https server module under `lacord.outoing-webhook-server` for interfacing\nwith discord over outgoing webhook. When using this method there are a couple of things to keep in mind:\n\n- You must use TLS. By default this module accepts two file paths after the server options table.\n  The first one should be your full certificate chain in pem format and the second should be your private key in pem format.\n  Should you wish to do more advanced TLS configuration, you can attach a ctx object to the options under `.ctx`.\n  If you are using an external service to provide TLS upstream (e.g an nginx reverse proxy), you can forcefully disable TLS\n  by setting `.tls` to `false`.\n\n- The first argument, the options table, is passed to `http.server.listen`. So please refer to the http library docs\n  for a full list of network options.\n  In addition to the `http` library's fields, the following are expected:\n    - The string field `route` is the path component of the URL you configure your application to use.\n      In the URL `https://example.com/interactions` this would be `/interactions`. Once again if you're\n      redirecting traffic to lacord from an external service make sure the path is adjusted if necessary.\n    - The function field `interact` is called when a discord interaction event is received by the webhook.\n      The first argument is the json object payload discord sent, the next argument is the https response object.\n      Return a valid json object from the function to send it to discord; if you do not it will respond with 500.\n      Any error in this function is caught and will respond with 503, logging the message internally.\n      You can also manipulate the response object to set the body directly, but this should be avoided unless necessary.\n    - The function field `fallthrough` receives a response object, and is called with any other request (i.e requests to paths other than the `route`).\n    - The string field `public_key` is your application's public key, necessary for signature verification.\n\nHere is a minimal example of configuration:\n\n```lua\nlocal server = require\"lacord.outgoing-webhook-server\"\n\nlocal function interact(event, resp)\n    if event.data.command == \"hello\" then\n        return {\n            type = 4,\n            data = {\n                content = \"Hello, world!\"\n            }\n        }\n    else\n        resp:set_code_and_reply(404, \"Command not found.\", \"text/plain; charset=UTF-8\")\n    end\nend\n\nlocal loop = server.new({\n    public_key = os.getenv\"PUBLIC_KEY\",\n    fallthrough = function(resp) resp:set_code_and_reply(404, \"Page not found.\", \"text/plain; charset=UTF-8\") end,\n    interact = interact,\n    host = \"localhost\",\n    port = 8888,\n    route = \"/interactions\"\n})\n\n\nassert(loop:loop())\n```\n\nThe `loop` object has `.cq` field which can be used to `:wrap` asynchronous code.\n\n## Notes\n\n#### Note 1\nI would recommend manually installing openssl with a version in the current stable series.\nAt the time of writing this is the **1.1.1** series.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMehgugs%2Flacord","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FMehgugs%2Flacord","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMehgugs%2Flacord/lists"}