Sha256: 7a28a0716a2e87b218cb04af9ddc7ad16080d7f16f547d1870ab4c524be2dc8e

Contents?: true

Size: 1.63 KB

Versions: 14

Compression:

Stored size: 1.63 KB

Contents

# @title Extension

# Extension

Extension allows you to split your code into multiple files.

## Make a new extension

Make a new module, and extend {Discorb::Extension}.

```ruby
module MyExtension
  extend Discorb::Extension
  
  # ...
end
```

## Register Event

Use {Discorb::Extension.event} to register event, or {Discorb::Extension.once_event} to register event only once.

```ruby
module MyExtension
  extend Discorb::Extension

  event :message do |message|
    # ...
  end

  once_event :ready do |message|
    # ...
  end
end
```

## Register Command

Since v0.5.2, {Discorb::Extension} includes {Discorb::ApplicationCommand::Handler} module, so you can register command with {Discorb::ApplicationCommand::Handler#slash} and {Discorb::ApplicationCommand::Handler#slash_group}.

```ruby
module MyExtension
  extend Discorb::Extension

  slash("command", "Command") do |interaction|
    # ...
  end

  slash_group("group", "Group") do |group|
    group.slash("subcommand", "Subcommand") do |interaction|
      # ...
    end

    group.group("subgroup", "Subcommand group") do |group|
      group.slash("group_subcommand", "Command in Subcommand group") do |interaction|
        # ...
      end
    end
  end
end
```


## Load extension

Use {Discorb::Client#extend} to load extension.

```ruby
module MyExtension
  extend Discorb::Extension

  event :message do |message|
    # ...
  end
end

client.extend MyExtension
```

## Access Client from extension

You can access {Discorb::Client} from extension with `@client`.

```ruby
module MyExtension
  extend Discorb::Extension

  event :ready do |message|
    puts "Logged in as #{@client.user}"
  end
end
```

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
discorb-0.9.6 docs/extension.md
discorb-0.9.5 docs/extension.md
discorb-0.9.4 docs/extension.md
discorb-0.9.3 docs/extension.md
discorb-0.9.1 docs/extension.md
discorb-0.9.0 docs/extension.md
discorb-0.8.2 docs/extension.md
discorb-0.8.1 docs/extension.md
discorb-0.8.0 docs/extension.md
discorb-0.7.6 docs/extension.md
discorb-0.7.3 docs/extension.md
discorb-0.7.2 docs/extension.md
discorb-0.7.1 docs/extension.md
discorb-0.7.0 docs/extension.md