https://github.com/z64/discordcr-plugin
Proof of concept for collections of event handlers for meew0/discordcr using annotations
https://github.com/z64/discordcr-plugin
api crystal discord discordcr
Last synced: 12 months ago
JSON representation
Proof of concept for collections of event handlers for meew0/discordcr using annotations
- Host: GitHub
- URL: https://github.com/z64/discordcr-plugin
- Owner: z64
- License: mit
- Created: 2018-07-19T09:43:48.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-07-14T20:53:25.000Z (over 6 years ago)
- Last Synced: 2025-02-05T21:41:21.636Z (about 1 year ago)
- Topics: api, crystal, discord, discordcr
- Language: Crystal
- Size: 34.2 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# discordcr-plugin
TODO: Write a description here
## Installation
Add this to your application's `shard.yml`:
```yaml
dependencies:
discordcr-plugin:
github: z64/discordcr-plugin
```
## Usage
```crystal
# my_commands.cr
# Describe a plugin with two middleware that will filter incoming
# events for every event handler in this plugin
@[Discord::Plugin::Options(middleware: {Prefix.new("!"), ChannelFilter.new(123)})]
class MyCommands
include Discord::Plugin
# Create a message_create handler method
@[Discord::Handler(event: :message_create)]
def do_something(payload, ctx)
# Access the client anywhere:
client.create_message(payload.channel_id, "test")
end
@[Discord::Handler(event: :message_create)]
def command_b(payload, ctx)
# ..
end
# Functions without annotations are ignored, can be defined for internal
# helper methods
def some_helper_function(arg)
# ..
end
# Use JSON/YAML configure hooks:
record Config, some_option : Bool do
include JSON::Serializable
include YAML::Serializable
end
@config : Config?
def configure(parser : JSON::PullParser)
@config = Config.new(parser)
end
def configure(parser : YAML::PullParser)
@config = Config.new(parser)
end
end
# main.cr
require "discordcr-plugin"
require "./my_commands"
# Make a client (or many!)
client = Discord::Client.new(token: ENV["TOKEN"])
# Configure all plugins based on their class name:
File.open("config.json", "r") do |file|
parser = JSON::PullParser.new(file)
parser.read_object do |key|
Discord::Plugin.plugins.each do |plugin|
if plugin.class.to_s.underscore == key
plugin.configure(parser)
else
parser.skip
end
end
end
end
# Register all plugins defined across the codebase:
Discord::Plugin.plugins.each do |plugin|
client.register(plugin)
end
# Let's go!
client.run
```
TODO: Write usage instructions here
## Contributors
- [z64](https://github.com/z64) Zac Nowicki - creator, maintainer