slack-smart-bot 1.13.2 → 1.14.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +100 -4
  3. data/lib/slack/smart-bot/ai/open_ai/connect.rb +61 -0
  4. data/lib/slack/smart-bot/ai/open_ai/models.rb +21 -0
  5. data/lib/slack/smart-bot/ai/open_ai/send_gpt_chat.rb +24 -0
  6. data/lib/slack/smart-bot/ai/open_ai/send_image_edit.rb +23 -0
  7. data/lib/slack/smart-bot/ai/open_ai/send_image_generation.rb +18 -0
  8. data/lib/slack/smart-bot/ai/open_ai/send_image_variation.rb +22 -0
  9. data/lib/slack/smart-bot/ai/open_ai/whisper_transcribe.rb +21 -0
  10. data/lib/slack/smart-bot/ai.rb +8 -0
  11. data/lib/slack/smart-bot/comm/get_channel_members.rb +15 -13
  12. data/lib/slack/smart-bot/comm/get_channels.rb +31 -29
  13. data/lib/slack/smart-bot/comm/respond_thread.rb +2 -2
  14. data/lib/slack/smart-bot/commands/general/ai/open_ai/open_ai_chat.rb +40 -0
  15. data/lib/slack/smart-bot/commands/general/ai/open_ai/open_ai_edit_image.rb +66 -0
  16. data/lib/slack/smart-bot/commands/general/ai/open_ai/open_ai_generate_image.rb +65 -0
  17. data/lib/slack/smart-bot/commands/general/ai/open_ai/open_ai_models.rb +37 -0
  18. data/lib/slack/smart-bot/commands/general/ai/open_ai/open_ai_variations_image.rb +84 -0
  19. data/lib/slack/smart-bot/commands/general/ai/open_ai/open_ai_whisper.rb +51 -0
  20. data/lib/slack/smart-bot/commands/general/bot_help.rb +1 -0
  21. data/lib/slack/smart-bot/commands/general/personal_settings.rb +38 -0
  22. data/lib/slack/smart-bot/commands/general/poster.rb +107 -104
  23. data/lib/slack/smart-bot/commands/general/public_holidays.rb +116 -114
  24. data/lib/slack/smart-bot/commands/general/set_public_holidays.rb +6 -2
  25. data/lib/slack/smart-bot/commands/general/teams/add_team.rb +87 -0
  26. data/lib/slack/smart-bot/commands/general/teams/delete_team.rb +69 -0
  27. data/lib/slack/smart-bot/commands/general/teams/memos/add_memo_team.rb +136 -0
  28. data/lib/slack/smart-bot/commands/general/teams/memos/add_memo_team_comment.rb +37 -0
  29. data/lib/slack/smart-bot/commands/general/teams/memos/delete_memo_team.rb +83 -0
  30. data/lib/slack/smart-bot/commands/general/teams/memos/see_memo_team.rb +97 -0
  31. data/lib/slack/smart-bot/commands/general/teams/memos/see_memos_team.rb +304 -0
  32. data/lib/slack/smart-bot/commands/general/teams/memos/set_memo_status.rb +66 -0
  33. data/lib/slack/smart-bot/commands/general/teams/ping_team.rb +104 -0
  34. data/lib/slack/smart-bot/commands/general/teams/see_teams.rb +236 -0
  35. data/lib/slack/smart-bot/commands/general/teams/see_vacations_team.rb +183 -0
  36. data/lib/slack/smart-bot/commands/general/teams/update_team.rb +137 -0
  37. data/lib/slack/smart-bot/commands/general_bot_commands.rb +905 -741
  38. data/lib/slack/smart-bot/commands/on_bot/general/bot_stats.rb +379 -353
  39. data/lib/slack/smart-bot/commands/on_bot/repl.rb +6 -107
  40. data/lib/slack/smart-bot/commands/on_bot/repl_client.rb +233 -0
  41. data/lib/slack/smart-bot/commands/on_master/admin_master/exit_bot.rb +17 -4
  42. data/lib/slack/smart-bot/commands.rb +26 -10
  43. data/lib/slack/smart-bot/process.rb +14 -3
  44. data/lib/slack/smart-bot/process_first.rb +36 -2
  45. data/lib/slack/smart-bot/treat_message.rb +28 -0
  46. data/lib/slack/smart-bot/utils/check_vacations.rb +1 -0
  47. data/lib/slack/smart-bot/utils/create_routine_thread.rb +1 -1
  48. data/lib/slack/smart-bot/utils/display_calendar.rb +17 -10
  49. data/lib/slack/smart-bot/utils/encryption/decrypt.rb +23 -0
  50. data/lib/slack/smart-bot/utils/encryption/encrypt.rb +27 -0
  51. data/lib/slack/smart-bot/utils/{encryption_get_key_iv.rb → encryption/encryption_get_key_iv.rb} +12 -8
  52. data/lib/slack/smart-bot/utils/get_help.rb +3 -1
  53. data/lib/slack/smart-bot/utils/get_personal_settings.rb +14 -0
  54. data/lib/slack/smart-bot/utils/get_teams.rb +2 -2
  55. data/lib/slack/smart-bot/utils/get_vacations.rb +2 -2
  56. data/lib/slack/smart-bot/utils/save_stats.rb +3 -1
  57. data/lib/slack/smart-bot/utils/update_personal_settings.rb +18 -0
  58. data/lib/slack/smart-bot/utils/update_teams.rb +1 -1
  59. data/lib/slack/smart-bot/utils/update_vacations.rb +1 -1
  60. data/lib/slack/smart-bot/utils.rb +5 -3
  61. data/lib/slack-smart-bot.rb +12 -0
  62. data/whats_new.txt +13 -14
  63. metadata +63 -15
  64. data/lib/slack/smart-bot/commands/general/add_memo_team.rb +0 -117
  65. data/lib/slack/smart-bot/commands/general/add_team.rb +0 -81
  66. data/lib/slack/smart-bot/commands/general/delete_memo_team.rb +0 -69
  67. data/lib/slack/smart-bot/commands/general/delete_team.rb +0 -55
  68. data/lib/slack/smart-bot/commands/general/ping_team.rb +0 -100
  69. data/lib/slack/smart-bot/commands/general/see_memos_team.rb +0 -202
  70. data/lib/slack/smart-bot/commands/general/see_teams.rb +0 -230
  71. data/lib/slack/smart-bot/commands/general/see_vacations_team.rb +0 -136
  72. data/lib/slack/smart-bot/commands/general/set_memo_status.rb +0 -58
  73. data/lib/slack/smart-bot/commands/general/update_team.rb +0 -131
  74. data/lib/slack/smart-bot/utils/decrypt.rb +0 -15
  75. data/lib/slack/smart-bot/utils/encrypt.rb +0 -15
@@ -0,0 +1,236 @@
1
+ class SlackSmartBot
2
+ module Commands
3
+ module General
4
+ module Teams
5
+ def see_teams(user, team_name, search = "", add_stats: true)
6
+ save_stats(__method__) if add_stats
7
+
8
+ get_teams()
9
+ teams = @teams.deep_copy
10
+ if teams.empty?
11
+ respond "There are no teams added yet. Use `add team` command to add a team."
12
+ elsif team_name.to_s != "" and !teams.key?(team_name.to_sym) and (teams.keys.select { |t| (t.to_s.gsub("-", "").gsub("_", "") == team_name.to_s) }).empty?
13
+ respond "It seems like the team *#{team_name}* doesn't exist.\nRelated commands `add team TEAM_NAME PROPERTIES`, `see team TEAM_NAME`, `see teams`"
14
+ else
15
+ users_link = (Thread.current[:dest][0] == "D")
16
+ filter = (search != "")
17
+ react :runner
18
+ @users = get_users() if add_stats
19
+
20
+ messages = []
21
+ search_members = []
22
+ search_channels = []
23
+ search_info = []
24
+ if filter
25
+ search.split(" ").each do |s|
26
+ if s.match(/<@(\w+)>/i)
27
+ m = $1
28
+ user_info = @users.select { |u| u.id.downcase == m.downcase or (u.key?(:enterprise_user) and u.enterprise_user.id.downcase == m.downcase) }[-1]
29
+ search_members << user_info.name unless user_info.nil?
30
+ elsif s.match(/<#(\w+)\|[^>]*>/i)
31
+ c = $1.upcase
32
+ search_channels << @channels_name[c] if @channels_name.key?(c)
33
+ else
34
+ search_info << s
35
+ end
36
+ end
37
+ end
38
+ if team_name.to_s == "" and search.to_s == ""
39
+ dest = :on_thread
40
+ messages.unshift("Since there are many lines returned the results are returned on a thread by default.")
41
+ else
42
+ dest = Thread.current[:dest]
43
+ end
44
+
45
+ teams.each do |name, team|
46
+ filter ? add = false : add = true
47
+ if team_name.to_s == "" or (team_name.to_s == name.to_s) or (name.to_s.gsub("-", "").gsub("_", "") == team_name.to_s)
48
+ message = []
49
+ message << "*#{name.capitalize}*"
50
+
51
+ if filter and search_info.size > 0
52
+ all_info = true
53
+ search_info.each do |s|
54
+ if (team.members.keys.find { |e| /#{s}/i =~ e })
55
+ add = true
56
+ break
57
+ end
58
+ if !name.match?(/#{s}/i)
59
+ all_info = false
60
+ break
61
+ end
62
+ end
63
+ add = true if all_info
64
+ end
65
+
66
+ message << " > *_members_*"
67
+
68
+ assigned_members, unassigned_members, not_on_team_channel, channels_members, all_team_members = get_team_members(team)
69
+
70
+ unless unassigned_members.empty?
71
+ um = unassigned_members.dup
72
+ um.each do |m|
73
+ user_info = @users.select { |u| u.name == m or (u.key?(:enterprise_user) and u.enterprise_user.name == m) }[-1]
74
+ unless user_info.nil? or user_info.profile.title.to_s == ""
75
+ team.members[user_info.profile.title.to_snake_case] ||= []
76
+ team.members[user_info.profile.title.to_snake_case] << m
77
+ unassigned_members.delete(m)
78
+ end
79
+ end
80
+ unless unassigned_members.empty?
81
+ team.members["unassigned"] ||= []
82
+ team.members["unassigned"] += unassigned_members
83
+ team.members["unassigned"].sort!
84
+ end
85
+ end
86
+ unless not_on_team_channel.empty?
87
+ team.members["not on members channel"] = not_on_team_channel
88
+ team.members["not on members channel"].sort!
89
+ end
90
+ add = true if (team.members.values.flatten & search_members).size > 0
91
+ add = true if (team.channels.values.flatten & search_channels).size > 0
92
+ if filter and search_info.size > 0
93
+ all_info = true
94
+ search_info.each do |s|
95
+ if (team.members.keys.find { |e| /#{s}/i =~ e })
96
+ add = true
97
+ break
98
+ end
99
+ if !team.info.match?(/#{s}/i)
100
+ all_info = false
101
+ break
102
+ end
103
+ end
104
+ add = true if all_info
105
+ end
106
+
107
+ if add
108
+ if team_name.to_s != ""
109
+ team.members.each do |type, members|
110
+ message << " _`#{type}`_: "
111
+ members.each do |member|
112
+ types = [":palm_tree:", ":spiral_calendar_pad:", ":face_with_thermometer:", ":baby:"]
113
+ member_info = @users.select { |u| u.name == member }[-1]
114
+ if !member_info.nil? and !member_info.deleted
115
+ member_id = member_info.id
116
+ info = get_user_info(member_id)
117
+ emoji = info.user.profile.status_emoji
118
+ if types.include?(emoji)
119
+ status = emoji
120
+ else
121
+ active = (get_presence(member_id).presence.to_s == "active")
122
+ if active
123
+ user_info = @users.select { |u| u.id == member_id or (u.key?(:enterprise_user) and u.enterprise_user.name == member_id) }[-1]
124
+ if (user_info.tz_offset - user.tz_offset).abs <= (4 * 3600)
125
+ status = ":large_green_circle:"
126
+ else
127
+ status = ":large_yellow_circle:"
128
+ end
129
+ else
130
+ status = ":white_circle:"
131
+ end
132
+ end
133
+ else
134
+ status = ":exclamation:"
135
+ end
136
+ unless status == ":exclamation:"
137
+ if users_link
138
+ message[-1] += " #{status}<@#{member}>, "
139
+ else
140
+ user_info = @users.select { |u| u.name == member or (u.key?(:enterprise_user) and u.enterprise_user.name == member) }[-1]
141
+ unless user_info.nil?
142
+ if user_info.profile.display_name == ""
143
+ name = user_info.name
144
+ else
145
+ name = user_info.profile.display_name
146
+ end
147
+ message[-1] += " #{status} #{name}, "
148
+ end
149
+ end
150
+ end
151
+ end
152
+ message[-1].chop!
153
+ message[-1].chop!
154
+ end
155
+ else
156
+ team.members.each do |type, members|
157
+ if users_link
158
+ message << " _`#{type}`_: <@#{members.join(">, <@")}>"
159
+ else
160
+ membersn = []
161
+ members.each do |m|
162
+ user_info = @users.select { |u| u.name == m or (u.key?(:enterprise_user) and u.enterprise_user.name == m) }[-1]
163
+ unless user_info.nil? or user_info.deleted
164
+ if user_info.profile.display_name == ""
165
+ name = user_info.name
166
+ else
167
+ name = user_info.profile.display_name
168
+ end
169
+ membersn << name
170
+ end
171
+ end
172
+ message << " _`#{type}`_: #{membersn.join(" / ")}"
173
+ end
174
+ end
175
+ end
176
+ end
177
+
178
+ if add
179
+ message << " > *_channels_*"
180
+ team.channels.each do |type, channels|
181
+ channel_ids = []
182
+ channels.each do |ch|
183
+ channel_info = @channels_list.select { |c| c.name.to_s.downcase == ch.to_s.downcase }[-1]
184
+ if @channels_id.key?(ch) and (!channel_info.is_private or (channel_info.is_private and (team.members.values + [team.creator]).flatten.include?(user.name)))
185
+ channel_ids << @channels_id[ch]
186
+ end
187
+ end
188
+ message << " _`#{type}`_: <##{channel_ids.join("> <#")}>" unless channel_ids.empty?
189
+ end
190
+
191
+ unless !team.key?(:memos) or team.memos.empty? or (team_name.to_s == "" and search.to_s == "")
192
+ message += see_memos_team(user, type: "all", add_stats: false, team: team)
193
+ end
194
+
195
+ unless team.info.empty?
196
+ team.info.split("\n").each do |m|
197
+ message << ">#{m}"
198
+ end
199
+ message << "> "
200
+ message << "> "
201
+ end
202
+ messages << message.join("\n")
203
+ end
204
+ end
205
+ end
206
+ unreact :runner
207
+ if messages.empty?
208
+ if filter
209
+ respond "It seems like we didn't find any team with the criteria supplied. Call `see teams` for a full list of teams."
210
+ else
211
+ respond "It seems like there are no teams added.\nUse `add team TEAM_NAME PROPERTIES` to add one. Call `bot help add team` for extended info."
212
+ end
213
+ else
214
+ if team_name.to_s != ""
215
+ message = "\n\n:palm_tree: On vacation / "
216
+ message += ":spiral_calendar_pad: In a meeting / "
217
+ message += ":face_with_thermometer: :baby: Sick leave / "
218
+ message += ":white_circle: Away / "
219
+ message += ":large_yellow_circle: Available in remote timezone / "
220
+ message += ":large_green_circle: Available"
221
+ messages[-1] << message
222
+ messages[-1] << "\n:information_source: Remote Time zone is >4h away from your current (#{user.tz_label})"
223
+ end
224
+ messages.each do |msg|
225
+ respond msg, dest, unfurl_links: false, unfurl_media: false
226
+ end
227
+ unless team_name.to_s.empty?
228
+ see_vacations_team(user, team_name, Date.today.strftime("%Y/%m/%d"), add_stats: false)
229
+ end
230
+ end
231
+ end
232
+ end
233
+ end
234
+ end
235
+ end
236
+ end
@@ -0,0 +1,183 @@
1
+ class SlackSmartBot
2
+ module Commands
3
+ module General
4
+ module Teams
5
+ def see_vacations_team(user, team_name, date, add_stats: true)
6
+ save_stats(__method__) if add_stats
7
+
8
+ get_teams()
9
+ teams = @teams.deep_copy
10
+ if teams.empty?
11
+ respond "There are no teams added yet. Use `add team` command to add a team."
12
+ elsif team_name.to_s != "" and !teams.key?(team_name.to_sym) and (teams.keys.select { |t| (t.to_s.gsub("-", "").gsub("_", "") == team_name.to_s) }).empty?
13
+ respond "It seems like the team *#{team_name}* doesn't exist.\nRelated commands `add team TEAM_NAME PROPERTIES`, `see team TEAM_NAME`, `see teams`"
14
+ else
15
+ teams.each do |name, team|
16
+ if team_name == name.to_s or (name.to_s.gsub("-", "").gsub("_", "") == team_name.to_s)
17
+ team_name = name.to_s
18
+ break
19
+ end
20
+ end
21
+ date.gsub!("-", "/")
22
+ get_vacations()
23
+ team = teams[team_name.to_sym]
24
+ assigned_members = team.members.values.flatten
25
+ assigned_members.uniq!
26
+ assigned_members.dup.each do |m|
27
+ user_info = @users.select { |u| u.id == m or (u.key?(:enterprise_user) and u.enterprise_user.id == m) or u.name == m or (u.key?(:enterprise_user) and u.enterprise_user.name == m) }[-1]
28
+ assigned_members.delete(m) if user_info.nil? or user_info.deleted
29
+ end
30
+
31
+ channels_members = []
32
+ all_team_members = assigned_members.dup
33
+ if team.channels.key?("members")
34
+ team_members = []
35
+ team.channels["members"].each do |ch|
36
+ get_channels_name_and_id() unless @channels_id.key?(ch)
37
+ tm = get_channel_members(@channels_id[ch])
38
+ if tm.nil?
39
+ respond ":exclamation: Add the Smart Bot to *##{ch}* channel to be able to get the list of members.", dest
40
+ else
41
+ channels_members << @channels_id[ch]
42
+ tm.each do |m|
43
+ user_info = @users.select { |u| u.id == m or (u.key?(:enterprise_user) and u.enterprise_user.id == m) }[-1]
44
+ team_members << user_info.name unless user_info.is_app_user or user_info.is_bot
45
+ end
46
+ end
47
+ end
48
+ team_members.flatten!
49
+ all_team_members += team_members
50
+ all_team_members.uniq!
51
+ end
52
+ unless all_team_members.empty?
53
+ blocks_header =
54
+ {
55
+ "type": "context",
56
+ elements: [
57
+ {
58
+ type: "mrkdwn",
59
+ text: "*Time Off #{team_name} team* from #{date} ",
60
+ },
61
+ ],
62
+ }
63
+
64
+ from = Date.parse(date, "%Y/%m/%d")
65
+ blocks = []
66
+ if config[:public_holidays].key?(:default_calendar)
67
+ defaulted_country_region = config[:public_holidays][:default_calendar].downcase
68
+ else
69
+ defaulted_country_region = ""
70
+ end
71
+ all_team_members.each do |m|
72
+ @users = get_users() if @users.empty?
73
+ info = @users.select { |u| u.id == m or (u.key?(:enterprise_user) and u.enterprise_user.id == m) or u.name == m or (u.key?(:enterprise_user) and u.enterprise_user.name == m) }[-1]
74
+ unless info.nil?
75
+ country_region = ""
76
+ if @vacations.key?(m) and @vacations[m][:public_holidays].to_s != ""
77
+ country_region = @vacations[m][:public_holidays].downcase
78
+ elsif config[:public_holidays].key?(:default_calendar) and country_region.empty?
79
+ country_region = defaulted_country_region
80
+ end
81
+
82
+ info = get_user_info(info.id)
83
+ if @vacations.key?(m)
84
+ v = ""
85
+ (from..(from + 20)).each do |d|
86
+ v += "#{d.strftime("%d")} " if d.wday == 1 or d == from
87
+ on_vacation = false
88
+ @vacations[m].periods ||= []
89
+ @vacations[m].periods.each do |p|
90
+ if p.from <= d.strftime("%Y/%m/%d") and p.to >= d.strftime("%Y/%m/%d")
91
+ if d.wday == 0 or d.wday == 6
92
+ v += ":large_orange_square: "
93
+ else
94
+ v += ":large_red_square: "
95
+ end
96
+ on_vacation = true
97
+ break
98
+ end
99
+ end
100
+ unless on_vacation
101
+ if country_region != "" and (!@public_holidays.key?(country_region) or !@public_holidays[country_region].key?(d.year.to_s))
102
+ country, location = country_region.split("/")
103
+ public_holidays(country.to_s, location.to_s, d.year.to_s, "", "", add_stats: false, publish_results: false)
104
+ end
105
+ if @public_holidays.key?(country_region) and @public_holidays[country_region].key?(d.year.to_s)
106
+ phd = @public_holidays[country_region][d.year.to_s].date.iso
107
+ else
108
+ phd = []
109
+ end
110
+ date_text = d.strftime("%Y-%m-%d")
111
+ if phd.include?(date_text)
112
+ v += ":large_red_square: "
113
+ elsif d.wday == 0 or d.wday == 6
114
+ v += ":large_yellow_square: "
115
+ else
116
+ v += ":white_square: "
117
+ end
118
+ end
119
+ end
120
+ else
121
+ v = ""
122
+ (from..(from + 20)).each do |d|
123
+ if country_region != "" and (!@public_holidays.key?(country_region) or !@public_holidays[country_region].key?(d.year.to_s))
124
+ country, location = country_region.split("/")
125
+ public_holidays(country.to_s, location.to_s, d.year.to_s, "", "", add_stats: false, publish_results: false)
126
+ end
127
+ if @public_holidays.key?(country_region) and @public_holidays[country_region].key?(d.year.to_s)
128
+ phd = @public_holidays[country_region][d.year.to_s].date.iso
129
+ else
130
+ phd = []
131
+ end
132
+ if d.wday == 1 or d == from
133
+ v += "#{d.strftime("%d")} "
134
+ end
135
+ date_text = d.strftime("%Y-%m-%d")
136
+ if phd.include?(date_text)
137
+ v += ":large_red_square: "
138
+ elsif d.wday == 0 or d.wday == 6
139
+ v += ":large_yellow_square: "
140
+ else
141
+ v += ":white_square: "
142
+ end
143
+ end
144
+ end
145
+
146
+ blocks << {
147
+ type: "context",
148
+ elements: [
149
+ {
150
+ type: "image",
151
+ image_url: info.user.profile.image_24,
152
+ alt_text: info.user.name,
153
+ },
154
+ {
155
+ type: "plain_text",
156
+ text: v,
157
+ },
158
+ ],
159
+ }
160
+ end
161
+ end
162
+ first = true
163
+ blocks.each_slice(10).each do |b|
164
+ if first
165
+ b.unshift(blocks_header)
166
+ first = false
167
+ end
168
+ respond blocks: b
169
+ end
170
+ message = ""
171
+ if !defaulted_country_region.empty?
172
+ message = "Defaulted public holidays calendar: #{defaulted_country_region}\n"
173
+ end
174
+ message += "To change your public holidays calendar, use the command `set public holidays to COUNTRY/STATE`. "
175
+ message += "\nExamples: `set public holidays to Iceland`, `set public holidays to Spain/Madrid`"
176
+ respond message
177
+ end
178
+ end
179
+ end
180
+ end
181
+ end
182
+ end
183
+ end
@@ -0,0 +1,137 @@
1
+ class SlackSmartBot
2
+ module Commands
3
+ module General
4
+ module Teams
5
+ def update_team(user, team_name, new_name: "", new_info: "", delete_opts: "", add_opts: "")
6
+ save_stats(__method__)
7
+ get_teams()
8
+ if @teams.key?(team_name.to_sym)
9
+ assigned_members = @teams[team_name.to_sym].members.values.flatten
10
+ assigned_members.uniq!
11
+ all_team_members = assigned_members.dup
12
+ team_members = []
13
+ if @teams[team_name.to_sym].channels.key?("members")
14
+ @teams[team_name.to_sym].channels["members"].each do |ch|
15
+ get_channels_name_and_id() unless @channels_id.key?(ch)
16
+ tm = get_channel_members(@channels_id[ch])
17
+ tm.each do |m|
18
+ user_info = @users.select { |u| u.id == m or (u.key?(:enterprise_user) and u.enterprise_user.id == m) }[-1]
19
+ team_members << user_info.name unless user_info.is_app_user or user_info.is_bot
20
+ end
21
+ end
22
+ end
23
+ team_members.flatten!
24
+ team_members.uniq!
25
+ all_team_members += team_members
26
+ all_team_members.uniq!
27
+ end
28
+ if !@teams.key?(team_name.to_sym)
29
+ respond "It seems like the team *#{team_name}* doesn't exist.\nRelated commands `add team TEAM_NAME PROPERTIES`, `see team TEAM_NAME`, `see teams`"
30
+ elsif !(all_team_members + [@teams[team_name.to_sym].creator] + config.masters).flatten.include?(user.name)
31
+ respond "You have to be a member of the team, the creator or a Master admin to be able to update this team."
32
+ else
33
+ wrong = false
34
+ if new_name != ""
35
+ team = @teams[team_name.to_sym].deep_copy
36
+ @teams[new_name.to_sym] = team
37
+ @teams.delete(team_name.to_sym)
38
+ File.delete(File.join(config.path, "teams", "t_#{team_name}.yaml"))
39
+ message = "The *#{team_name}* team has been renamed #{new_name}."
40
+ team_name = new_name
41
+ elsif new_info != ""
42
+ @teams[team_name.to_sym].info = new_info
43
+ elsif delete_opts != ""
44
+ last_type = nil
45
+ delete_opts.split(" ").each do |opt|
46
+ if opt.match?(/^\s*$/)
47
+ #blank
48
+ elsif opt.match?(/^[\w\-]+$/i)
49
+ last_type = opt
50
+ elsif opt.match(/<?@(\w+)>?/i) #accepts also @username for the case the user has been deactivated
51
+ member_id = $1
52
+ member_info = @users.select { |u| u.id == member_id or u.name == member_id or (u.key?(:enterprise_user) and u.enterprise_user.id == member_id) }[-1]
53
+ if last_type.nil?
54
+ @teams[team_name.to_sym].members.each do |type, members|
55
+ @teams[team_name.to_sym].members[type].delete(member_info.name)
56
+ end
57
+ else
58
+ @teams[team_name.to_sym].members[last_type] ||= []
59
+ @teams[team_name.to_sym].members[last_type].delete(member_info.name)
60
+ end
61
+ elsif opt.match(/<#(\w+)\|[^>]*>/i)
62
+ channel_id = $1
63
+ get_channels_name_and_id() unless @channels_name.keys.include?(channel_id)
64
+ channel = @channels_name[channel_id]
65
+ if last_type.nil?
66
+ @teams[team_name.to_sym].channels.each do |type, channels|
67
+ @teams[team_name.to_sym].channels[type].delete(channel)
68
+ end
69
+ else
70
+ @teams[team_name.to_sym].channels[last_type] ||= []
71
+ @teams[team_name.to_sym].channels[last_type].delete(channel)
72
+ end
73
+ else
74
+ respond "It seems like the members or channel list is not correct. Please double check."
75
+ wrong = true
76
+ break
77
+ end
78
+ end
79
+ tmembers = @teams[team_name.to_sym].members.deep_copy
80
+ tmembers.each do |type, members|
81
+ @teams[team_name.to_sym].members.delete(type) if members.empty?
82
+ end
83
+ tchannels = @teams[team_name.to_sym].channels.deep_copy
84
+ tchannels.each do |type, channels|
85
+ @teams[team_name.to_sym].channels.delete(type) if channels.empty?
86
+ end
87
+ elsif add_opts != ""
88
+ last_type = nil
89
+ add_opts.split(" ").each do |opt|
90
+ if opt.match?(/^\s*$/)
91
+ #blank
92
+ elsif opt.match?(/^[\w\-]+$/i)
93
+ last_type = opt
94
+ elsif opt.match(/<@(\w+)>/i)
95
+ member_id = $1
96
+ last_type = "no_type" if last_type.nil?
97
+ member_info = @users.select { |u| u.id == member_id or (u.key?(:enterprise_user) and u.enterprise_user.id == member_id) }[-1]
98
+ @teams[team_name.to_sym].members[last_type] ||= []
99
+ @teams[team_name.to_sym].members[last_type] << member_info.name
100
+ @teams[team_name.to_sym].members[last_type].uniq!
101
+ elsif opt.match(/<#(\w+)\|[^>]*>/i)
102
+ channel_id = $1
103
+ get_channels_name_and_id() unless @channels_name.keys.include?(channel_id)
104
+ channel = @channels_name[channel_id]
105
+ @teams[team_name.to_sym].channels[last_type] ||= []
106
+ @teams[team_name.to_sym].channels[last_type] << channel
107
+ @teams[team_name.to_sym].channels[last_type].uniq!
108
+ else
109
+ respond "It seems like the members or channel list is not correct. Please double check."
110
+ wrong = true
111
+ break
112
+ end
113
+ end
114
+ tmembers = @teams[team_name.to_sym].members.deep_copy
115
+ tmembers.each do |type, members|
116
+ @teams[team_name.to_sym].members.delete(type) if members.empty?
117
+ end
118
+ tchannels = @teams[team_name.to_sym].channels.deep_copy
119
+ tchannels.each do |type, channels|
120
+ @teams[team_name.to_sym].channels.delete(type) if channels.empty?
121
+ end
122
+ end
123
+ unless wrong
124
+ message ||= "The *#{team_name}* team has been updated."
125
+ @teams[team_name.to_sym].status = :updated
126
+ @teams[team_name.to_sym].user = user.name
127
+ @teams[team_name.to_sym].date = Time.now.strftime("%Y-%m-%dT%H:%M:%S.000Z")[0..18]
128
+ update_teams()
129
+ end
130
+ respond message
131
+ see_teams(user, team_name, add_stats: false) unless wrong
132
+ end
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end