{"id":17681211,"url":"https://github.com/z64/discordcr-middleware","last_synced_at":"2025-08-17T03:40:58.304Z","repository":{"id":80745994,"uuid":"112450196","full_name":"z64/discordcr-middleware","owner":"z64","description":"A simple extension to discordcr, for webserver-like middlewares. Stock middleware ready to use.","archived":false,"fork":false,"pushed_at":"2019-07-14T21:07:25.000Z","size":167,"stargazers_count":12,"open_issues_count":3,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-12T23:11:13.427Z","etag":null,"topics":["crystal","discord-bot","discordcr","discordcr-middleware"],"latest_commit_sha":null,"homepage":"https://z64.github.io/discordcr-middleware/","language":"Crystal","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/z64.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-11-29T08:49:07.000Z","updated_at":"2021-05-04T11:10:40.000Z","dependencies_parsed_at":"2023-03-12T12:05:52.529Z","dependency_job_id":null,"html_url":"https://github.com/z64/discordcr-middleware","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/z64/discordcr-middleware","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/z64%2Fdiscordcr-middleware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/z64%2Fdiscordcr-middleware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/z64%2Fdiscordcr-middleware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/z64%2Fdiscordcr-middleware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/z64","download_url":"https://codeload.github.com/z64/discordcr-middleware/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/z64%2Fdiscordcr-middleware/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270803531,"owners_count":24648688,"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","status":"online","status_checked_at":"2025-08-17T02:00:09.016Z","response_time":129,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["crystal","discord-bot","discordcr","discordcr-middleware"],"created_at":"2024-10-24T09:10:31.417Z","updated_at":"2025-08-17T03:40:58.263Z","avatar_url":"https://github.com/z64.png","language":"Crystal","funding_links":[],"categories":[],"sub_categories":[],"readme":"# discordcr-middleware\n\nAn extension of [discordcr's](https://github.com/meew0/discordcr) `Client` that\nadds middleware functionality, similar to that of webserver libraries. The goal\nis to provide a very customizable way to conditionally execute event handlers,\nand make great reuse of code and state between events.\n\n- [Documentation](https://z64.github.io/discordcr-middleware)\n\n## Deprecation notice\n\nThe functionality provided by this shard is now built into discordcr. It\n**should not** be used, as it would duplicate a bunch of code.\n\nEventually this repo will be reorganized to host *only* the stock middleware.\nIf you still want to use them, you can just specifically require them instead\nof requiring the entire extension.\n\n## Installation\n\nAdd this to your application's `shard.yml`:\n\n```yaml\ndependencies:\n  discordcr-middleware:\n    github: z64/discordcr-middleware\n```\n\n## Usage\n\nRequire the extension:\n```crystal\nrequire \"discordcr-middleware\"\n\n```\n\n### Creating Middleware\n\nAny class that implements `def call(payload, context, \u0026block)` can be used as\nmiddleware:\n\n```crystal\nclass Middleware\n  def call(payload, context)\n    # Do things with payload and context..\n    yield\n  end\nend\n```\n\n`payload` is the `Discord` event payload that triggered the event handler. A\nmiddleware can be designed to handle multiple kinds of payloads by overloading\n`call` and restricting `payload` to different types:\n\n```crystal\nclass Middleware\n  def call(payload : Discord::Message, context)\n    # Do things with a message..\n    yield\n  end\n\n  def call(payload : Discord::Gateway::PresenceUpdatePayload, context)\n    # Do things with a presence update..\n    yield\n  end\nend\n```\n\nIf you try to use a middleware class with an event that it doesn't support,\nthis will result in a compile-time error.\n\n`context` is an instance of `Context`. This is a special class that allows\nyou to store arbitrary `class` objects to be referenced later in the middleware\nchain. How the library handles `context` and how it should be used will be\noutlined in the next section, but in brief:\n\n```crystal\nclass Foo\nend\n\nfoo = Foo.new\ncontext.put(foo)\ncontext[Foo] == foo # =\u003e true\n```\n\nThe `\u0026block` passed to `call` is a block to be yielded to that\ndetermines whether or not middleware that follow should be executed.\n\nYou can define an `#initialize` for your middleware that will let you configure\nhow you want your middleware to behave for different event handlers, as opposed\nto creating another middleware class with extremely similar behavior.\n\n### Usage with Client\n\nAny event handler can have a middleware chain applied to it.\n\nThe event handling process goes like this:\n\n1. The message event is received by the client\n2. An \"empty\" `Context` is initialized, and the receiving `Client` is added\nto it\n3. Each middleware in the chain is added to `Context`. This allows you to\naccess the middleware that have run previously in the chain, either\nfrom inside one of the middleware or in the event handler itself.\n4. The event is passed through each middleware, providing that each one\nsuccessfully `yield`s. If any middleware does not `yield`, execution\nof the rest of the chain will stop.\n\n```crystal\nclient.on_message_create(MiddlewareA.new, MiddlewareB.new) do |payload, context|\n  payload # =\u003e Discord::Message\n  context # =\u003e Discord::Context\n  context[Discord::Client] == client # =\u003e true\n  context[MiddlewareA]               # =\u003e MiddlewareA\n  context[MiddlewareB]               # =\u003e MiddlewareB\nend\n```\n\nIt's also worth noting you can share the same instance of a middleware between\nmultiple event handlers:\n\n```crystal\nmiddleware = Middleware.new\n\nclient.on_message_create(middleware) do |payload|\n  # ...\nend\n\nclient.on_message_update(middleware) do |payload|\n  # ...\nend\n```\n\nThis is useful, for example, for a middleware that has some kind of state that\naffects multiple handlers. You could have a single `Prefix` middleware instance\nthat stores the client's prefix in memory, so that when you update the prefix,\nthe runtime behavior on all of your handlers updates at once.\n\nThe event handler block is also optional, making it possible to have\npure-middleware chains:\n\n```crystal\nclient.on_message_create(MiddlewareA.new, MiddlewareB.new)\n```\n\n### [Additional Examples](https://github.com/z64/discordcr-middleware/tree/master/examples)\n\n## Stock Middleware\n\nA collection of basic, common use-case middleware are provided in [`discordcr-middleware/middleware`](src/discordcr-middleware/middleware).\n\nRequire them explicitly to make use of them:\n\n```crystal\nrequire \"discordcr-middleware/middleware/prefix\"\n\nDiscordMiddleware::Prefix.new(\"!help\")\n```\n\n## Contributors\n\n- [z64](https://github.com/z64)  - creator, maintainer\n\n*Inspired by the [raze](https://razecr.com/) web framework and its [middleware system](https://razecr.com/docs/middlewares).*\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fz64%2Fdiscordcr-middleware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fz64%2Fdiscordcr-middleware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fz64%2Fdiscordcr-middleware/lists"}