discordrb 3.1.1 → 3.4.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of discordrb might be problematic. Click here for more details.
- checksums.yaml +5 -5
- data/.circleci/config.yml +126 -0
- data/.codeclimate.yml +16 -0
- data/.github/CONTRIBUTING.md +13 -0
- data/.github/ISSUE_TEMPLATE/bug_report.md +39 -0
- data/.github/ISSUE_TEMPLATE/feature_request.md +25 -0
- data/.github/pull_request_template.md +37 -0
- data/.gitignore +5 -0
- data/.rubocop.yml +39 -33
- data/.travis.yml +27 -2
- data/.yardopts +1 -1
- data/CHANGELOG.md +808 -208
- data/Gemfile +4 -1
- data/LICENSE.txt +1 -1
- data/README.md +108 -53
- data/Rakefile +14 -1
- data/bin/console +1 -0
- data/bin/travis_build_docs.sh +17 -0
- data/discordrb-webhooks.gemspec +26 -0
- data/discordrb.gemspec +24 -15
- data/lib/discordrb.rb +75 -2
- data/lib/discordrb/allowed_mentions.rb +36 -0
- data/lib/discordrb/api.rb +126 -27
- data/lib/discordrb/api/channel.rb +165 -43
- data/lib/discordrb/api/invite.rb +10 -7
- data/lib/discordrb/api/server.rb +240 -61
- data/lib/discordrb/api/user.rb +26 -24
- data/lib/discordrb/api/webhook.rb +83 -0
- data/lib/discordrb/await.rb +1 -2
- data/lib/discordrb/bot.rb +417 -149
- data/lib/discordrb/cache.rb +42 -10
- data/lib/discordrb/colour_rgb.rb +43 -0
- data/lib/discordrb/commands/command_bot.rb +186 -31
- data/lib/discordrb/commands/container.rb +30 -16
- data/lib/discordrb/commands/parser.rb +102 -47
- data/lib/discordrb/commands/rate_limiter.rb +18 -17
- data/lib/discordrb/container.rb +245 -41
- data/lib/discordrb/data.rb +27 -2511
- data/lib/discordrb/data/activity.rb +264 -0
- data/lib/discordrb/data/application.rb +50 -0
- data/lib/discordrb/data/attachment.rb +56 -0
- data/lib/discordrb/data/audit_logs.rb +345 -0
- data/lib/discordrb/data/channel.rb +849 -0
- data/lib/discordrb/data/embed.rb +251 -0
- data/lib/discordrb/data/emoji.rb +82 -0
- data/lib/discordrb/data/integration.rb +83 -0
- data/lib/discordrb/data/invite.rb +137 -0
- data/lib/discordrb/data/member.rb +297 -0
- data/lib/discordrb/data/message.rb +334 -0
- data/lib/discordrb/data/overwrite.rb +102 -0
- data/lib/discordrb/data/profile.rb +91 -0
- data/lib/discordrb/data/reaction.rb +33 -0
- data/lib/discordrb/data/recipient.rb +34 -0
- data/lib/discordrb/data/role.rb +191 -0
- data/lib/discordrb/data/server.rb +1002 -0
- data/lib/discordrb/data/user.rb +204 -0
- data/lib/discordrb/data/voice_region.rb +45 -0
- data/lib/discordrb/data/voice_state.rb +41 -0
- data/lib/discordrb/data/webhook.rb +145 -0
- data/lib/discordrb/errors.rb +36 -2
- data/lib/discordrb/events/bans.rb +7 -5
- data/lib/discordrb/events/channels.rb +2 -0
- data/lib/discordrb/events/generic.rb +19 -3
- data/lib/discordrb/events/guilds.rb +129 -6
- data/lib/discordrb/events/invites.rb +125 -0
- data/lib/discordrb/events/members.rb +6 -2
- data/lib/discordrb/events/message.rb +86 -36
- data/lib/discordrb/events/presence.rb +23 -16
- data/lib/discordrb/events/raw.rb +47 -0
- data/lib/discordrb/events/reactions.rb +159 -0
- data/lib/discordrb/events/roles.rb +7 -6
- data/lib/discordrb/events/typing.rb +9 -5
- data/lib/discordrb/events/voice_server_update.rb +47 -0
- data/lib/discordrb/events/voice_state_update.rb +29 -9
- data/lib/discordrb/events/webhooks.rb +64 -0
- data/lib/discordrb/gateway.rb +219 -88
- data/lib/discordrb/id_object.rb +39 -0
- data/lib/discordrb/light.rb +1 -1
- data/lib/discordrb/light/integrations.rb +1 -1
- data/lib/discordrb/light/light_bot.rb +1 -1
- data/lib/discordrb/logger.rb +12 -11
- data/lib/discordrb/paginator.rb +57 -0
- data/lib/discordrb/permissions.rb +148 -14
- data/lib/discordrb/version.rb +1 -1
- data/lib/discordrb/voice/encoder.rb +14 -15
- data/lib/discordrb/voice/network.rb +86 -45
- data/lib/discordrb/voice/sodium.rb +96 -0
- data/lib/discordrb/voice/voice_bot.rb +52 -40
- data/lib/discordrb/webhooks.rb +12 -0
- data/lib/discordrb/websocket.rb +2 -2
- metadata +137 -34
data/Gemfile
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,83 +1,95 @@
|
|
1
1
|
[![Gem](https://img.shields.io/gem/v/discordrb.svg)](https://rubygems.org/gems/discordrb)
|
2
2
|
[![Gem](https://img.shields.io/gem/dt/discordrb.svg)](https://rubygems.org/gems/discordrb)
|
3
|
-
[![
|
4
|
-
[![Inline docs](http://inch-ci.org/github/
|
5
|
-
[![Join Discord](https://img.shields.io/badge/discord-join-7289DA.svg)](https://discord.gg/
|
3
|
+
[![CircleCI](https://circleci.com/gh/shardlab/discordrb.svg?style=svg)](https://circleci.com/gh/shardlab/discordrb)
|
4
|
+
[![Inline docs](http://inch-ci.org/github/shardlab/discordrb.svg?branch=main)](http://inch-ci.org/github/shardlab/discordrb)
|
5
|
+
[![Join Discord](https://img.shields.io/badge/discord-join-7289DA.svg)](https://discord.gg/cyK3Hjm)
|
6
6
|
# discordrb
|
7
7
|
|
8
|
-
An implementation of the [Discord](https://
|
8
|
+
An implementation of the [Discord](https://discord.com/) API using Ruby.
|
9
9
|
|
10
10
|
## Quick links to sections
|
11
11
|
|
12
|
-
* [
|
13
|
-
* [
|
14
|
-
* [
|
15
|
-
* [
|
16
|
-
* [
|
17
|
-
* [
|
12
|
+
* [Introduction](https://github.com/shardlab/discordrb#introduction)
|
13
|
+
* [Dependencies](https://github.com/shardlab/discordrb#dependencies)
|
14
|
+
* [Installation](https://github.com/shardlab/discordrb#installation)
|
15
|
+
* [Usage](https://github.com/shardlab/discordrb#usage)
|
16
|
+
* [Webhooks Client](https://github.com/shardlab/discordrb#webhooks-client)
|
17
|
+
* [Support](https://github.com/shardlab/discordrb#support)
|
18
|
+
* [Development](https://github.com/shardlab/discordrb#development), [Contributing](https://github.com/shardlab/discordrb#contributing)
|
19
|
+
* [License](https://github.com/shardlab/discordrb#license)
|
18
20
|
|
19
|
-
See also: [Documentation](
|
21
|
+
See also: [Documentation](https://www.rubydoc.info/gems/discordrb), [Tutorials](https://github.com/shardlab/discordrb/wiki)
|
20
22
|
|
21
|
-
##
|
23
|
+
## Introduction
|
22
24
|
|
23
|
-
|
24
|
-
* An installed build system for native extensions (on Windows, try the [DevKit](http://rubyinstaller.org/downloads/); installation instructions [here](https://github.com/oneclick/rubyinstaller/wiki/Development-Kit#quick-start) - you only need to do the quick start)
|
25
|
+
`discordrb` aims to meet the following design goals:
|
25
26
|
|
26
|
-
|
27
|
+
1. Full coverage of the public bot API.
|
28
|
+
2. Expressive, high level abstractions for rapid development of common applications.
|
29
|
+
3. Friendly to Ruby beginners and beginners of open source contribution.
|
27
30
|
|
28
|
-
|
29
|
-
* [libsodium](https://github.com/meew0/discordrb/wiki/Installing-libsodium)
|
30
|
-
* A compiled libopus distribution for your system, anywhere the script can find it (on Windows, make sure it's named `opus.dll`)
|
31
|
-
* [FFmpeg](https://www.ffmpeg.org/download.html) installed and in your PATH
|
31
|
+
If you enjoy using the library, consider getting involved with the community to help us improve and meet these goals!
|
32
32
|
|
33
|
-
|
33
|
+
**You should consider using `discordrb` if:**
|
34
34
|
|
35
|
-
|
35
|
+
- You need a bot - and fast - for small or medium sized communities, and don't want to be bogged down with "low level" details. Getting started takes minutes, and utilities like a command parser and tools for modularization make it simple to quickly add or change your bots functionality.
|
36
|
+
- You like or want to learn Ruby, or want to contribute to a Ruby project. A lot of our users are new to Ruby, and eventually make their first open source contributions with us. We have an active Discord channel with experienced members who will happily help you get involved, either as a user or contributor.
|
37
|
+
- You want to experiment with Discord's API or prototype concepts for Discord bots without too much commitment.
|
36
38
|
|
37
|
-
|
39
|
+
**You should consider other libraries if:**
|
38
40
|
|
39
|
-
|
41
|
+
- You need to scale to large volumes of servers (>2,500) with lots of members. It's still possible, but it can be difficult to scale Ruby processes, and it requires more in depth knowledge to do so well. Especially if you already have a bot that is on a large amount of servers, porting to Ruby is unlikely to improve your performance in most cases.
|
42
|
+
- You want full control over the library that you're using. While we expose some "lower level" interfaces, they are unstable, and only exist to serve the more powerful abstractions in the library.
|
40
43
|
|
41
|
-
|
44
|
+
## Dependencies
|
42
45
|
|
43
|
-
|
46
|
+
* Ruby >= 2.5 supported
|
47
|
+
* An installed build system for native extensions (on Windows, make sure you download the "Ruby+Devkit" version of [RubyInstaller](https://rubyinstaller.org/downloads/))
|
44
48
|
|
45
|
-
|
49
|
+
### Voice dependencies
|
46
50
|
|
47
|
-
|
51
|
+
This section only applies to you if you want to use voice functionality.
|
52
|
+
* [libsodium](https://github.com/shardlab/discordrb/wiki/Installing-libsodium)
|
53
|
+
* A compiled libopus distribution for your system, anywhere the script can find it. See [here](https://github.com/shardlab/discordrb/wiki/Installing-libopus) for installation instructions.
|
54
|
+
* [FFmpeg](https://www.ffmpeg.org/download.html) installed and in your PATH
|
48
55
|
|
49
|
-
|
56
|
+
## Installation
|
50
57
|
|
51
|
-
|
58
|
+
### With Bundler
|
59
|
+
|
60
|
+
Using [Bundler](https://bundler.io/#getting-started), you can add discordrb to your Gemfile:
|
61
|
+
|
62
|
+
gem 'discordrb'
|
52
63
|
|
53
|
-
|
64
|
+
And then install via `bundle install`.
|
54
65
|
|
55
|
-
|
66
|
+
Run the [ping example](https://github.com/shardlab/discordrb/blob/master/examples/ping.rb) to verify that the installation works (make sure to replace the token and client ID in there with your bots'!):
|
56
67
|
|
57
|
-
|
58
|
-
The 'websocket-driver' native gem requires installed build tools.
|
68
|
+
To run the bot while using bundler:
|
59
69
|
|
60
|
-
|
70
|
+
bundle exec ruby ping.rb
|
61
71
|
|
62
|
-
|
63
|
-
ruby dk.rb install
|
72
|
+
### With Gem
|
64
73
|
|
65
|
-
|
74
|
+
Alternatively, while Bundler is the recommended option, you can also install discordrb without it.
|
75
|
+
|
76
|
+
#### Linux / macOS
|
66
77
|
|
67
|
-
gem uninstall discordrb
|
68
78
|
gem install discordrb
|
69
79
|
|
70
|
-
|
80
|
+
#### Windows
|
71
81
|
|
72
|
-
|
82
|
+
> **Make sure you have the DevKit installed! See the [Dependencies](https://github.com/shardlab/discordrb#dependencies) section)**
|
73
83
|
|
74
|
-
|
84
|
+
gem install discordrb --platform=ruby
|
75
85
|
|
76
|
-
|
86
|
+
To run the bot:
|
77
87
|
|
78
|
-
|
88
|
+
ruby ping.rb
|
79
89
|
|
80
|
-
|
90
|
+
### Installation Troubleshooting
|
91
|
+
|
92
|
+
See https://github.com/shardlab/discordrb/wiki/FAQ#installation for a list of common problems and solutions when installing `discordrb`.
|
81
93
|
|
82
94
|
## Usage
|
83
95
|
|
@@ -86,7 +98,7 @@ You can make a simple bot like this:
|
|
86
98
|
```ruby
|
87
99
|
require 'discordrb'
|
88
100
|
|
89
|
-
bot = Discordrb::Bot.new token: '<token here>'
|
101
|
+
bot = Discordrb::Bot.new token: '<token here>'
|
90
102
|
|
91
103
|
bot.message(with_text: 'Ping!') do |event|
|
92
104
|
event.respond 'Pong!'
|
@@ -97,23 +109,66 @@ bot.run
|
|
97
109
|
|
98
110
|
This bot responds to every "Ping!" with a "Pong!".
|
99
111
|
|
100
|
-
|
112
|
+
See [additional examples here](https://github.com/shardlab/discordrb/tree/master/examples).
|
101
113
|
|
102
|
-
You can find
|
114
|
+
You can find examples of projects that use discordrb by [searching for the discordrb topic on GitHub](https://github.com/topics/discordrb).
|
103
115
|
|
104
|
-
|
116
|
+
If you've made an open source project on GitHub that uses discordrb, consider adding the `discordrb` topic to your repo!
|
105
117
|
|
106
|
-
|
118
|
+
## Webhooks Client
|
107
119
|
|
108
|
-
|
120
|
+
Also included is a webhooks client, which can be used as a separate gem `discordrb-webhooks`. This special client can be used to form requests to Discord webhook URLs in a high-level manner.
|
109
121
|
|
110
|
-
|
122
|
+
- [`discordrb-webhooks` documentation](https://www.rubydoc.info/gems/discordrb-webhooks)
|
123
|
+
- [More information about webhooks](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks)
|
124
|
+
- [Embed visualizer tool](https://leovoel.github.io/embed-visualizer/) - Includes a discordrb code generator for forming embeds
|
125
|
+
|
126
|
+
### Usage
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
require 'discordrb/webhooks'
|
130
|
+
|
131
|
+
WEBHOOK_URL = 'https://discord.com/api/webhooks/424070213278105610/yByxDncRvHi02mhKQheviQI2erKkfRRwFcEp0MMBfib1ds6ZHN13xhPZNS2-fJo_ApSw'.freeze
|
132
|
+
|
133
|
+
client = Discordrb::Webhooks::Client.new(url: WEBHOOK_URL)
|
134
|
+
client.execute do |builder|
|
135
|
+
builder.content = 'Hello world!'
|
136
|
+
builder.add_embed do |embed|
|
137
|
+
embed.title = 'Embed title'
|
138
|
+
embed.description = 'Embed description'
|
139
|
+
embed.timestamp = Time.now
|
140
|
+
end
|
141
|
+
end
|
142
|
+
```
|
143
|
+
|
144
|
+
**Note:** The `discordrb` gem relies on `discordrb-webhooks`. If you already have `discordrb` installed, `require 'discordrb/webhooks'` will include all of the `Webhooks` features as well.
|
145
|
+
|
146
|
+
## Support
|
147
|
+
|
148
|
+
If you need help or have a question, you can:
|
149
|
+
|
150
|
+
1. Join our [Discord channel](https://discord.gg/cyK3Hjm). This is the fastest means of getting support.
|
151
|
+
2. [Open an issue](https://github.com/shardlab/discordrb/issues). Be sure to read the issue template, and provide as much detail as you can.
|
111
152
|
|
112
153
|
## Contributing
|
113
154
|
|
114
|
-
|
155
|
+
Thank you for your interest in contributing!
|
156
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/shardlab/discordrb.
|
157
|
+
|
158
|
+
In general, we recommend starting by discussing what you would like to contribute in the [Discord channel](https://discord.gg/cyK3Hjm).
|
159
|
+
There are usually a handful of people working on things for the library, and what you're looking for may already be on the way.
|
160
|
+
|
161
|
+
Additionally, there is a chance what you are looking for might already exist, or we decided not to pursue it for some reason.
|
162
|
+
Be sure to use the search feature on our documentation, GitHub, and Discord to see if this might be the case.
|
163
|
+
|
164
|
+
## Development setup
|
115
165
|
|
166
|
+
**This section is for developing discordrb itself! If you just want to make a bot, see the [Installation](https://github.com/shardlab/discordrb#installation) section.**
|
167
|
+
|
168
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can then run tests via `bundle exec rspec spec`. Make sure to run rubocop also: `bundle exec rubocop`. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
169
|
+
|
170
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
116
171
|
|
117
172
|
## License
|
118
173
|
|
119
|
-
The gem is available as open source under the terms of the [MIT License](
|
174
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
CHANGED
@@ -1,4 +1,17 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_helper'
|
4
|
+
|
5
|
+
namespace :main do
|
6
|
+
Bundler::GemHelper.install_tasks(name: 'discordrb')
|
7
|
+
end
|
8
|
+
|
9
|
+
namespace :webhooks do
|
10
|
+
Bundler::GemHelper.install_tasks(name: 'discordrb-webhooks')
|
11
|
+
end
|
12
|
+
|
13
|
+
task build: %i[main:build webhooks:build]
|
14
|
+
task release: %i[main:release webhooks:release]
|
2
15
|
|
3
16
|
# Make "build" the default task
|
4
17
|
task default: :build
|
data/bin/console
CHANGED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/bin/bash
|
2
|
+
# Script to build docs for travis usage
|
3
|
+
|
4
|
+
set -e
|
5
|
+
|
6
|
+
SOURCE_BRANCH=$TRAVIS_BRANCH
|
7
|
+
|
8
|
+
if [ -n "$TRAVIS_TAG" ]; then
|
9
|
+
SOURCE_BRANCH=$TRAVIS_TAG
|
10
|
+
fi
|
11
|
+
|
12
|
+
# Use YARD to build docs
|
13
|
+
bundle exec yard
|
14
|
+
|
15
|
+
# Move to correct folder
|
16
|
+
mkdir -p docs/$SOURCE_BRANCH
|
17
|
+
mv doc/* docs/$SOURCE_BRANCH/
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'discordrb/webhooks/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'discordrb-webhooks'
|
9
|
+
spec.version = Discordrb::Webhooks::VERSION
|
10
|
+
spec.authors = %w[meew0 swarley]
|
11
|
+
spec.email = ['']
|
12
|
+
|
13
|
+
spec.summary = 'Webhook client for discordrb'
|
14
|
+
spec.description = "A client for Discord's webhooks to fit alongside [discordrb](https://rubygems.org/gems/discordrb)."
|
15
|
+
spec.homepage = 'https://github.com/shardlab/discordrb'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z lib/discordrb/webhooks/`.split("\x0") + ['lib/discordrb/webhooks.rb']
|
19
|
+
spec.bindir = 'exe'
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ['lib']
|
22
|
+
|
23
|
+
spec.add_dependency 'rest-client', '>= 2.0.0'
|
24
|
+
|
25
|
+
spec.required_ruby_version = '>= 2.5'
|
26
|
+
end
|
data/discordrb.gemspec
CHANGED
@@ -1,35 +1,44 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
5
|
require 'discordrb/version'
|
5
6
|
|
6
7
|
Gem::Specification.new do |spec|
|
7
8
|
spec.name = 'discordrb'
|
8
9
|
spec.version = Discordrb::VERSION
|
9
|
-
spec.authors = [
|
10
|
+
spec.authors = %w[meew0 swarley]
|
10
11
|
spec.email = ['']
|
11
12
|
|
12
13
|
spec.summary = 'Discord API for Ruby'
|
13
|
-
spec.description = 'A Ruby implementation of the Discord (https://
|
14
|
-
spec.homepage = 'https://github.com/
|
14
|
+
spec.description = 'A Ruby implementation of the Discord (https://discord.com) API.'
|
15
|
+
spec.homepage = 'https://github.com/shardlab/discordrb'
|
15
16
|
spec.license = 'MIT'
|
16
17
|
|
17
|
-
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|examples)/}) }
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|examples|lib/discordrb/webhooks)/}) }
|
18
19
|
spec.bindir = 'exe'
|
19
20
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.metadata = {
|
22
|
+
'changelog_uri' => 'https://github.com/shardlab/discordrb/blob/master/CHANGELOG.md'
|
23
|
+
}
|
20
24
|
spec.require_paths = ['lib']
|
21
25
|
|
22
|
-
spec.add_dependency '
|
23
|
-
spec.add_dependency 'activesupport'
|
26
|
+
spec.add_dependency 'ffi', '>= 1.9.24'
|
24
27
|
spec.add_dependency 'opus-ruby'
|
28
|
+
spec.add_dependency 'rest-client', '>= 2.0.0'
|
25
29
|
spec.add_dependency 'websocket-client-simple', '>= 0.3.0'
|
26
|
-
spec.add_dependency 'rbnacl'
|
27
30
|
|
28
|
-
spec.
|
31
|
+
spec.add_dependency 'discordrb-webhooks', '~> 3.3.0'
|
32
|
+
|
33
|
+
spec.required_ruby_version = '>= 2.5'
|
29
34
|
|
30
|
-
spec.add_development_dependency 'bundler', '
|
31
|
-
spec.add_development_dependency 'rake', '~>
|
32
|
-
spec.add_development_dependency '
|
33
|
-
spec.add_development_dependency 'rspec', '~> 3.
|
34
|
-
spec.add_development_dependency '
|
35
|
+
spec.add_development_dependency 'bundler', '>= 1.10', '< 3'
|
36
|
+
spec.add_development_dependency 'rake', '~> 13.0'
|
37
|
+
spec.add_development_dependency 'redcarpet', '~> 3.5.0' # YARD markdown formatting
|
38
|
+
spec.add_development_dependency 'rspec', '~> 3.10.0'
|
39
|
+
spec.add_development_dependency 'rspec-prof', '~> 0.0.7'
|
40
|
+
spec.add_development_dependency 'rubocop', '~> 1.4.0'
|
41
|
+
spec.add_development_dependency 'rubocop-performance', '~> 1.0'
|
42
|
+
spec.add_development_dependency 'simplecov', '~> 0.19.0'
|
43
|
+
spec.add_development_dependency 'yard', '~> 0.9.9'
|
35
44
|
end
|
data/lib/discordrb.rb
CHANGED
@@ -11,11 +11,84 @@ module Discordrb
|
|
11
11
|
|
12
12
|
# The default debug logger used by discordrb.
|
13
13
|
LOGGER = Logger.new(ENV['DISCORDRB_FANCY_LOG'])
|
14
|
+
|
15
|
+
# The Unix timestamp Discord IDs are based on
|
16
|
+
DISCORD_EPOCH = 1_420_070_400_000
|
17
|
+
|
18
|
+
# Used to declare what events you wish to recieve from Discord.
|
19
|
+
# @see https://discordapp.com/developers/docs/topics/gateway#gateway-intents
|
20
|
+
INTENTS = {
|
21
|
+
servers: 1 << 0,
|
22
|
+
server_members: 1 << 1,
|
23
|
+
server_bans: 1 << 2,
|
24
|
+
server_emojis: 1 << 3,
|
25
|
+
server_integrations: 1 << 4,
|
26
|
+
server_webhooks: 1 << 5,
|
27
|
+
server_invites: 1 << 6,
|
28
|
+
server_voice_states: 1 << 7,
|
29
|
+
server_presences: 1 << 8,
|
30
|
+
server_messages: 1 << 9,
|
31
|
+
server_message_reactions: 1 << 10,
|
32
|
+
server_message_typing: 1 << 11,
|
33
|
+
direct_messages: 1 << 12,
|
34
|
+
direct_message_reactions: 1 << 13,
|
35
|
+
direct_message_typing: 1 << 14
|
36
|
+
}.freeze
|
37
|
+
|
38
|
+
# @return [Integer] All available intents
|
39
|
+
ALL_INTENTS = INTENTS.values.reduce(&:|)
|
40
|
+
|
41
|
+
# Compares two objects based on IDs - either the objects' IDs are equal, or one object is equal to the other's ID.
|
42
|
+
def self.id_compare(one_id, other)
|
43
|
+
other.respond_to?(:resolve_id) ? (one_id.resolve_id == other.resolve_id) : (one_id == other)
|
44
|
+
end
|
45
|
+
|
46
|
+
# The maximum length a Discord message can have
|
47
|
+
CHARACTER_LIMIT = 2000
|
48
|
+
|
49
|
+
# Splits a message into chunks of 2000 characters. Attempts to split by lines if possible.
|
50
|
+
# @param msg [String] The message to split.
|
51
|
+
# @return [Array<String>] the message split into chunks
|
52
|
+
def self.split_message(msg)
|
53
|
+
# If the messages is empty, return an empty array
|
54
|
+
return [] if msg.empty?
|
55
|
+
|
56
|
+
# Split the message into lines
|
57
|
+
lines = msg.lines
|
58
|
+
|
59
|
+
# Turn the message into a "triangle" of consecutively longer slices, for example the array [1,2,3,4] would become
|
60
|
+
# [
|
61
|
+
# [1],
|
62
|
+
# [1, 2],
|
63
|
+
# [1, 2, 3],
|
64
|
+
# [1, 2, 3, 4]
|
65
|
+
# ]
|
66
|
+
tri = (0...lines.length).map { |i| lines.combination(i + 1).first }
|
67
|
+
|
68
|
+
# Join the individual elements together to get an array of strings with consecutively more lines
|
69
|
+
joined = tri.map(&:join)
|
70
|
+
|
71
|
+
# Find the largest element that is still below the character limit, or if none such element exists return the first
|
72
|
+
ideal = joined.max_by { |e| e.length > CHARACTER_LIMIT ? -1 : e.length }
|
73
|
+
|
74
|
+
# If it's still larger than the character limit (none was smaller than it) split it into the largest chunk without
|
75
|
+
# cutting words apart, breaking on the nearest space within character limit, otherwise just return an array with one element
|
76
|
+
ideal_ary = ideal.length > CHARACTER_LIMIT ? ideal.split(/(.{1,#{CHARACTER_LIMIT}}\b|.{1,#{CHARACTER_LIMIT}})/o).reject(&:empty?) : [ideal]
|
77
|
+
|
78
|
+
# Slice off the ideal part and strip newlines
|
79
|
+
rest = msg[ideal.length..-1].strip
|
80
|
+
|
81
|
+
# If none remains, return an empty array -> we're done
|
82
|
+
return [] unless rest
|
83
|
+
|
84
|
+
# Otherwise, call the method recursively to split the rest of the string and add it onto the ideal array
|
85
|
+
ideal_ary + split_message(rest)
|
86
|
+
end
|
14
87
|
end
|
15
88
|
|
16
89
|
# In discordrb, Integer and {String} are monkey-patched to allow for easy resolution of IDs
|
17
90
|
class Integer
|
18
|
-
# @return [Integer] The Discord ID represented by this integer, i.
|
91
|
+
# @return [Integer] The Discord ID represented by this integer, i.e. the integer itself
|
19
92
|
def resolve_id
|
20
93
|
self
|
21
94
|
end
|
@@ -23,7 +96,7 @@ end
|
|
23
96
|
|
24
97
|
# In discordrb, {Integer} and String are monkey-patched to allow for easy resolution of IDs
|
25
98
|
class String
|
26
|
-
# @return [Integer] The Discord ID represented by this string, i.
|
99
|
+
# @return [Integer] The Discord ID represented by this string, i.e. the string converted to an integer
|
27
100
|
def resolve_id
|
28
101
|
to_i
|
29
102
|
end
|