dispander.rb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f3e930967666092faf411622028c20fa7d31796cd0fe5e0eed7995fd82c79d30
4
+ data.tar.gz: b34675bc0a76a89e04efec3a7dbb8ec25372b5797b7ac5e1ee00255a22bbe1c7
5
+ SHA512:
6
+ metadata.gz: d964954609afb5804c4adcb7f0ad25513ebc9329d81de7dab8ed2e62d43b0db96c0b75dd8f6748469b48fb56cd45d5c27f515907b4ac23eb306410ee3165e663
7
+ data.tar.gz: dfea449df14d2779df00890b8bfa60d288149130f00bcc2df34d35da97818ab01b245659ebc5a50c6a4e927118bd05311c4d79bf0e8d05580db9a5634cf6e6c1
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in dispander.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,62 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ dispander.rb (0.1.0)
5
+ discorb (~> 0.2.4)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ async (1.30.1)
11
+ console (~> 1.10)
12
+ nio4r (~> 2.3)
13
+ timers (~> 4.1)
14
+ async-http (0.56.5)
15
+ async (>= 1.25)
16
+ async-io (>= 1.28)
17
+ async-pool (>= 0.2)
18
+ protocol-http (~> 0.22.0)
19
+ protocol-http1 (~> 0.14.0)
20
+ protocol-http2 (~> 0.14.0)
21
+ async-io (1.32.2)
22
+ async
23
+ async-pool (0.3.8)
24
+ async (>= 1.25)
25
+ async-websocket (0.19.0)
26
+ async-http (~> 0.54)
27
+ async-io (~> 1.23)
28
+ protocol-websocket (~> 0.7.0)
29
+ console (1.13.1)
30
+ fiber-local
31
+ discorb (0.2.4)
32
+ async
33
+ async-http
34
+ async-websocket
35
+ mime-types (~> 3.3)
36
+ fiber-local (1.0.0)
37
+ mime-types (3.3.1)
38
+ mime-types-data (~> 3.2015)
39
+ mime-types-data (3.2021.0704)
40
+ nio4r (2.5.8)
41
+ protocol-hpack (1.4.2)
42
+ protocol-http (0.22.5)
43
+ protocol-http1 (0.14.2)
44
+ protocol-http (~> 0.22)
45
+ protocol-http2 (0.14.2)
46
+ protocol-hpack (~> 1.4)
47
+ protocol-http (~> 0.18)
48
+ protocol-websocket (0.7.5)
49
+ protocol-http (~> 0.2)
50
+ protocol-http1 (~> 0.2)
51
+ rake (13.0.6)
52
+ timers (4.3.3)
53
+
54
+ PLATFORMS
55
+ x86-mingw32
56
+
57
+ DEPENDENCIES
58
+ dispander.rb!
59
+ rake (~> 13.0)
60
+
61
+ BUNDLED WITH
62
+ 2.2.22
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 sevenc-nanashi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # dispander.rb
2
+
3
+ discorbで出来たBotにメッセージ展開の機能を追加するExtension。
4
+ ![画像](https://user-images.githubusercontent.com/59691627/131650571-ec50bf35-c971-4aeb-9a58-8fbf9b3e759b.png)
5
+
6
+ [DiscordBotPortalJP/dispander](https://github.com/DiscordBotPortalJP/dispander) のRuby版。
7
+
8
+ ## インストール
9
+
10
+ Gemfileに以下を追記し...
11
+
12
+ ```ruby
13
+ gem 'dispander'
14
+ ```
15
+
16
+ これを実行してください。
17
+
18
+ $ bundle install
19
+
20
+ または...
21
+
22
+ $ gem install dispander
23
+
24
+ ## 使い方
25
+
26
+ ### Extensionとして読み込む
27
+
28
+ ```ruby
29
+ require "discorb"
30
+ require "dispander"
31
+
32
+ client = Discorb::Client.new
33
+
34
+ client.once :ready do
35
+ puts <<~EOS
36
+ ---------
37
+ Logged in as #{client.user}(#{client.user.id})
38
+ ---------
39
+ EOS
40
+ end
41
+
42
+ client.extend(Dispander)
43
+
44
+ client.run ENV["DISCORD_BOT_TOKEN"]
45
+ ```
46
+
47
+ ### 手動で実行する
48
+
49
+ `Dispander.dispand`でメッセージを展開、`Dispander.delete_message`で展開したメッセージを削除できます。
50
+
51
+ ```ruby
52
+ require "discorb"
53
+ require "dispander"
54
+
55
+ client = Discorb::Client.new
56
+
57
+ client.on :message do |message|
58
+ next if message.author.bot?
59
+
60
+ Dispander.dispand(message)
61
+ end
62
+
63
+ client.on :reaction_add do |event|
64
+ Dispander.delete_message(event)
65
+ end
66
+
67
+ client.run ENV["DISCORD_BOT_TOKEN"]
68
+ ```
69
+
70
+ ### 削除の絵文字を変更する
71
+
72
+ `Dispander.delete_emoji`に絵文字を指定してください。
73
+
74
+ ```ruby
75
+ Dispander.delete_emoji = Discorb::UnicodeEmoji.new("x")
76
+
77
+ client.extend(Dispander)
78
+ ```
79
+
80
+
81
+ ## ライセンス
82
+
83
+ [MIT License](https://opensource.org/licenses/MIT)で公開しています。
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "dispander"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/dispander.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/dispander/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "dispander.rb"
7
+ spec.version = Dispander::VERSION
8
+ spec.authors = ["sevenc-nanashi"]
9
+ spec.email = ["sevenc-nanashi@sevenbot.jp"]
10
+
11
+ spec.summary = "discorbのBotにメッセージ展開の機能を追加するExtension。"
12
+ spec.homepage = "https://github.com/DiscordBotPortalJP/dispander.rb"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 3.0.0"
15
+
16
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/DiscordBotPortalJP/dispander.rb"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ # Uncomment to register a new dependency of your gem
31
+ spec.add_dependency "discorb", "~> 0.2.4"
32
+
33
+ # For more information and examples about making a new gem, checkout our
34
+ # guide at: https://bundler.io/guides/creating_gem.html
35
+ end
@@ -0,0 +1,121 @@
1
+ require "discorb"
2
+
3
+ module Dispander
4
+ extend Discorb::Extension
5
+ DISCORD_URL_PATTERN = /(?!<)https:\/\/(ptb.|canary.)?discord(app)?.com\/channels\/(?<guild>[0-9]{18,})\/(?<channel>[0-9]{18,})\/(?<message>[0-9]{18,})(?!>)/
6
+
7
+ event :message do |message|
8
+ next if message.author.bot?
9
+
10
+ Dispander.dispand(message)
11
+ end
12
+
13
+ event :reaction_add do |event|
14
+ Dispander.delete_message(event)
15
+ end
16
+
17
+ class << self
18
+ # @return [Discorb::Emoji] 削除リアクションとして使う絵文字。
19
+ attr_accessor :delete_emoji
20
+
21
+ #
22
+ # メッセージを解析して、埋め込みを送信します。
23
+ #
24
+ # @param [Discorb::Message] base_message 解析するメッセージ。
25
+ #
26
+ # @return [Array<Discorb::Message>] 埋め込みを送信したメッセージ。
27
+ #
28
+ def dispand(base_message)
29
+ all_sent_messages = []
30
+ base_message.content.scan(DISCORD_URL_PATTERN).each do |match|
31
+ guild_id, channel_id, message_id = *match
32
+ next unless base_message.guild.id == guild_id
33
+
34
+ embeds = []
35
+
36
+ sent_messages = []
37
+ begin
38
+ message = @client.guilds[guild_id].channels[channel_id].fetch_message(message_id).wait
39
+ rescue Discorb::NotFoundError
40
+ next
41
+ else
42
+ embed = create_embed_from_message(message)
43
+ embeds << embed
44
+ embeds += message.embeds
45
+ embeds += message.attachments[1..]&.filter(&:image?)&.map { |attachment| create_embed_from_attachment(attachment) }.to_a
46
+
47
+ until (embeds_send = embeds.slice!(..10)).empty?
48
+ sent_messages << message.channel.post(embeds: embeds_send).wait
49
+ end
50
+ embed.url = "http://a.io/#{base_message.author.id}-#{message.author.id}-#{sent_messages.map(&:id).join(",")}"
51
+ first_embeds = sent_messages[0].embeds
52
+ first_embeds[0] = embed
53
+ sent_messages[0].add_reaction(@delete_emoji || Discorb::UnicodeEmoji["wastebasket"])
54
+ sent_messages[0].edit(embeds: first_embeds).wait
55
+ all_sent_messages += sent_messages
56
+ end
57
+ end
58
+ all_sent_messages
59
+ end
60
+
61
+ #
62
+ # メッセージから埋め込みを作成します。
63
+ #
64
+ # @param [Discorb::Message] message 埋め込みを作成するメッセージ。
65
+ #
66
+ # @return [Discorb::Embed] 埋め込み。
67
+ #
68
+ def create_embed_from_message(message)
69
+ embed = Discorb::Embed.new
70
+ embed.description = message.content
71
+ embed.timestamp = message.timestamp
72
+ embed.author = Discorb::Embed::Author.new(
73
+ message.author.to_s,
74
+ icon: message.author.avatar.url,
75
+ )
76
+ embed.footer = Discorb::Embed::Footer.new(
77
+ "#" + message.channel.name
78
+ )
79
+ if (attachment = message.attachments[0]) && attachment.image?
80
+ embed.image = attachment.proxy_url
81
+ end
82
+ embed
83
+ end
84
+
85
+ #
86
+ # 添付ファイルから埋め込みを作成します。
87
+ #
88
+ # @param [Discorb::Attachment] attachment 埋め込みを作成する添付ファイル。
89
+ #
90
+ # @return [Discorb::Embed] 埋め込み。
91
+ #
92
+ def create_embed_from_attachment(attachment)
93
+ embed = Discorb::Embed.new(
94
+ image: attachment.proxy_url,
95
+ )
96
+ embed
97
+ end
98
+
99
+ #
100
+ # 埋め込みを削除します。
101
+ #
102
+ # @param [Discorb::Gateway::ReactionEvent] event リアクションのイベント。
103
+ #
104
+ def delete_message(event)
105
+ return unless event.emoji != (@delete_emoji || Discorb::UnicodeEmoji["wastebasket"])
106
+ return if event.user_id == @client.user.id
107
+
108
+ message = event.fetch_message.wait
109
+
110
+ return if message.embeds.empty?
111
+ return unless message.author == @client.user
112
+
113
+ _, author_id, operator_id, sent_message_ids = *message.embed.url.match(/^http:\/\/a.io\/([0-9]+)-([0-9]+)-([0-9,]+)$/)
114
+ return unless author_id == event.user_id || operator_id == event.user_id
115
+
116
+ sent_message_ids.split(",").each do |sent_message_id|
117
+ event.channel.delete_message!(sent_message_id).wait
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dispander
4
+ VERSION = "0.1.0"
5
+ end
data/lib/dispander.rb ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "dispander/version"
4
+ require_relative "dispander/extension"
5
+
6
+ module Dispander
7
+ class Error < StandardError; end
8
+ end
data/samplebot.rb ADDED
@@ -0,0 +1,16 @@
1
+ require "discorb"
2
+ require "dispander"
3
+
4
+ client = Discorb::Client.new
5
+
6
+ client.once :ready do
7
+ puts <<~EOS
8
+ ---------
9
+ Logged in as #{client.user}(#{client.user.id})
10
+ ---------
11
+ EOS
12
+ end
13
+
14
+ client.extend(Dispander)
15
+
16
+ client.run ENV["DISCORD_BOT_TOKEN"]
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dispander.rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - sevenc-nanashi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-09-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: discorb
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.2.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.2.4
27
+ description:
28
+ email:
29
+ - sevenc-nanashi@sevenbot.jp
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - bin/console
41
+ - bin/setup
42
+ - dispander.gemspec
43
+ - lib/dispander.rb
44
+ - lib/dispander/extension.rb
45
+ - lib/dispander/version.rb
46
+ - samplebot.rb
47
+ homepage: https://github.com/DiscordBotPortalJP/dispander.rb
48
+ licenses:
49
+ - MIT
50
+ metadata:
51
+ allowed_push_host: https://rubygems.org
52
+ homepage_uri: https://github.com/DiscordBotPortalJP/dispander.rb
53
+ source_code_uri: https://github.com/DiscordBotPortalJP/dispander.rb
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 3.0.0
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.2.22
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: discorbのBotにメッセージ展開の機能を追加するExtension。
73
+ test_files: []