-
Notifications
You must be signed in to change notification settings - Fork 8
/
ruboty-channel-gacha.rb
139 lines (113 loc) · 2.92 KB
/
ruboty-channel-gacha.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
require 'slack'
require 'active_support'
require 'active_support/core_ext'
module Ruboty
module Handlers
class ChannelGacha < Base
on(
%r!((?<option>\S+) )?((channel_gacha)|(チャンネルガチャ))!,
name: 'channel_gacha',
description: 'returns randomly channel information',
)
def channel_gacha(message)
option = Options::Channel.new(message.match_data['option'])
message.reply RubyJP::Channel.all(reload: option.reload?)
.then { |channels| Replies::ChannelGacha.create(channels, option.pre_message) }
end
end
end
end
module Replies
module ChannelGacha
class << self
def create(channels, pre_message)
messages(channels, pre_message).join("\n")
end
private
def messages(channels, pre_message)
[pre_message, channels.sample.channel_information].compact
end
end
end
end
module Options
class Channel
def initialize(option)
@option = option
end
def reload?
@option == '-r'
end
def pre_message
@option.split('=', 2)[1] if with_pre_message?
end
def with_pre_message?
@option =~ /\A-pre/
end
end
end
module RubyJP
class Cache
@store ||= ActiveSupport::Cache::MemoryStore.new
class << self
def store
@store
end
def fetch(name, reload: false, &block)
store.tap { _1.delete(name) if reload }.fetch(name, &block)
end
end
end
class Channel
attr_writer :id, :topic, :purpose
def initialize(channel)
@id = channel['id']
@topic = channel['topic']['value']
@purpose = channel['purpose']['value']
end
class << self
def all(reload: false)
cache(reload: reload) { SlackApi::Channel.new.fetch_public_channels.map(&method(:new)) }
end
private
def cache(reload: false, &block)
Cache.fetch('channels', reload: reload, &block)
end
end
def channel_information
[channel_name, topic, purpose].compact.join("\n")
end
def channel_name
"チャンネル名: <##{@id}>"
end
def topic
"トピック: #{@topic}" if @topic.present?
end
def purpose
"説明: #{@purpose}" if @purpose.present?
end
end
end
module SlackApi
class Channel
def initialize
@client = client
end
def fetch_public_channels
channels, next_cursor = [], nil
until next_cursor&.empty?
response = @client.conversations_list request_params(next_cursor)
next_cursor = response['response_metadata']['next_cursor']
channels.concat(response['channels'])
end
channels
end
private
def client
Slack::Client.new(token: ENV.fetch('SLACK_TOKEN'))
end
def request_params(next_cursor)
{ exclude_archived: true, limit: 200 }.tap { _1.merge!({ cursor: next_cursor }) if next_cursor.present? }
end
end
end