gruubY 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +21 -21
  3. data/README.md +11 -6
  4. data/Rakefile +51 -51
  5. data/config/config.rb +1 -1
  6. data/example/bot.rb +122 -2
  7. data/example/generate_user_session.rb +81 -81
  8. data/example/tdlib.rb +77 -77
  9. data/example/userbot.rb +133 -133
  10. data/lib/gruubY/api.rb +540 -363
  11. data/lib/gruubY/async.rb +13 -13
  12. data/lib/gruubY/bot.rb +104 -90
  13. data/lib/gruubY/bound.rb +14 -14
  14. data/lib/gruubY/client.rb +310 -296
  15. data/lib/gruubY/context.rb +296 -242
  16. data/lib/gruubY/dispatcher.rb +60 -60
  17. data/lib/gruubY/enums.rb +57 -62
  18. data/lib/gruubY/exception.rb +5 -5
  19. data/lib/gruubY/file_stream.rb +17 -17
  20. data/lib/gruubY/filters.rb +292 -292
  21. data/lib/gruubY/group_manager.rb +213 -213
  22. data/lib/gruubY/handlers.rb +225 -170
  23. data/lib/gruubY/keyboard.rb +49 -49
  24. data/lib/gruubY/media.rb +38 -38
  25. data/lib/gruubY/middleware.rb +18 -18
  26. data/lib/gruubY/plugin.rb +27 -14
  27. data/lib/gruubY/raw.rb +25 -25
  28. data/lib/gruubY/retry.rb +14 -14
  29. data/lib/gruubY/server.rb +18 -18
  30. data/lib/gruubY/session.rb +30 -30
  31. data/lib/gruubY/tdlib/client.rb +565 -565
  32. data/lib/gruubY/tdlib/client_manager.rb +37 -37
  33. data/lib/gruubY/tdlib/decorators.rb +17 -17
  34. data/lib/gruubY/tdlib/errors.rb +6 -6
  35. data/lib/gruubY/tdlib/group_manager.rb +58 -58
  36. data/lib/gruubY/tdlib/native.rb +72 -72
  37. data/lib/gruubY/tdlib/schema_builder.rb +237 -237
  38. data/lib/gruubY/tdlib/tdjson.rb +49 -49
  39. data/lib/gruubY/tdlib/user_session.rb +69 -69
  40. data/lib/gruubY/types/base_object.rb +72 -72
  41. data/lib/gruubY/types/bound_entities.rb +289 -245
  42. data/lib/gruubY/types/chat.rb +93 -93
  43. data/lib/gruubY/types/chat_info.rb +32 -31
  44. data/lib/gruubY/types/extra.rb +289 -251
  45. data/lib/gruubY/types/message.rb +301 -295
  46. data/lib/gruubY/types/message_entity.rb +14 -14
  47. data/lib/gruubY/types/registry.rb +86 -67
  48. data/lib/gruubY/types/user.rb +47 -31
  49. data/lib/gruubY/webapp.rb +22 -22
  50. data/lib/gruubY.rb +39 -41
  51. data/plugins/logger.rb +7 -7
  52. metadata +3 -9
  53. data/example/ntgcalls.rb +0 -80
  54. data/lib/gruubY/ntgcalls/native.rb +0 -100
  55. data/lib/gruubY/ntgcalls.rb +0 -478
  56. data/lib/gruubY/raw_types.rb +0 -57
  57. data/plugins/media_tools.rb +0 -13
@@ -1,213 +1,213 @@
1
- module GrubY
2
- class GroupManager
3
- DEFAULT_WARN_LIMIT = 3
4
-
5
- def initialize(api, session: nil, warn_limit: DEFAULT_WARN_LIMIT)
6
- @api = api
7
- @session = session
8
- @warn_limit = warn_limit
9
- end
10
-
11
- def ban(chat_id, user_id, **opts)
12
- @api.ban_chat_member(chat_id: chat_id, user_id: user_id, **opts)
13
- end
14
-
15
- def unban(chat_id, user_id, **opts)
16
- @api.unban_chat_member(chat_id: chat_id, user_id: user_id, **opts)
17
- end
18
-
19
- def kick(chat_id, user_id, **opts)
20
- ban(chat_id, user_id, **opts)
21
- unban(chat_id, user_id)
22
- end
23
-
24
- def restrict(chat_id, user_id, permissions:, **opts)
25
- @api.restrict_chat_member(
26
- chat_id: chat_id,
27
- user_id: user_id,
28
- permissions: permissions,
29
- **opts
30
- )
31
- end
32
-
33
- def mute(chat_id, user_id, until_date: nil)
34
- restrict(
35
- chat_id,
36
- user_id,
37
- permissions: { can_send_messages: false },
38
- until_date: until_date
39
- )
40
- end
41
-
42
- def unmute(chat_id, user_id)
43
- permissions = {
44
- can_send_messages: true,
45
- can_send_audios: true,
46
- can_send_documents: true,
47
- can_send_photos: true,
48
- can_send_videos: true,
49
- can_send_video_notes: true,
50
- can_send_voice_notes: true,
51
- can_send_polls: true,
52
- can_send_other_messages: true,
53
- can_add_web_page_previews: true
54
- }
55
- restrict(chat_id, user_id, permissions: permissions)
56
- end
57
-
58
- def promote(chat_id, user_id, **opts)
59
- @api.promote_chat_member(chat_id: chat_id, user_id: user_id, **opts)
60
- end
61
-
62
- def demote(chat_id, user_id)
63
- promote(
64
- chat_id,
65
- user_id,
66
- can_manage_chat: false,
67
- can_delete_messages: false,
68
- can_manage_video_chats: false,
69
- can_restrict_members: false,
70
- can_promote_members: false,
71
- can_change_info: false,
72
- can_invite_users: false,
73
- can_post_stories: false,
74
- can_edit_stories: false,
75
- can_delete_stories: false,
76
- can_post_messages: false,
77
- can_edit_messages: false,
78
- can_pin_messages: false,
79
- can_manage_topics: false
80
- )
81
- end
82
-
83
- def set_member_tag(chat_id, user_id, tag = nil)
84
- @api.set_chat_member_tag(chat_id: chat_id, user_id: user_id, tag: tag)
85
- end
86
-
87
- def set_admin_title(chat_id, user_id, title)
88
- @api.set_chat_administrator_custom_title(
89
- chat_id: chat_id,
90
- user_id: user_id,
91
- custom_title: title
92
- )
93
- end
94
-
95
- def set_administrator_title(chat_id, user_id, title)
96
- set_admin_title(chat_id, user_id, title)
97
- end
98
-
99
- def set_chat_title(chat_id, title)
100
- @api.set_chat_title(chat_id: chat_id, title: title)
101
- end
102
-
103
- def set_chat_description(chat_id, description)
104
- @api.set_chat_description(chat_id: chat_id, description: description)
105
- end
106
-
107
- def set_chat_photo(chat_id, photo)
108
- @api.set_chat_photo(chat_id: chat_id, photo: photo)
109
- end
110
-
111
- def delete_chat_photo(chat_id)
112
- @api.delete_chat_photo(chat_id: chat_id)
113
- end
114
-
115
- def leave_chat(chat_id)
116
- @api.leave_chat(chat_id: chat_id)
117
- end
118
-
119
- def set_permissions(chat_id, permissions, **opts)
120
- @api.set_chat_permissions(chat_id: chat_id, permissions: permissions, **opts)
121
- end
122
-
123
- def lock(chat_id)
124
- set_permissions(chat_id, { can_send_messages: false })
125
- end
126
-
127
- def unlock(chat_id)
128
- set_permissions(chat_id, { can_send_messages: true })
129
- end
130
-
131
- def set_slow_mode(chat_id, seconds)
132
- @api.set_slow_mode(chat_id: chat_id, seconds: seconds)
133
- end
134
-
135
- def pin(chat_id, message_id, **opts)
136
- @api.pin_chat_message(chat_id: chat_id, message_id: message_id, **opts)
137
- end
138
-
139
- def unpin(chat_id, message_id = nil, **opts)
140
- params = { chat_id: chat_id }.merge(opts)
141
- params[:message_id] = message_id if message_id
142
- @api.unpin_chat_message(**params)
143
- end
144
-
145
- def unpin_all(chat_id)
146
- @api.unpin_all_chat_messages(chat_id: chat_id)
147
- end
148
-
149
- def approve_join_request(chat_id, user_id)
150
- @api.approve_chat_join_request(chat_id: chat_id, user_id: user_id)
151
- end
152
-
153
- def decline_join_request(chat_id, user_id)
154
- @api.decline_chat_join_request(chat_id: chat_id, user_id: user_id)
155
- end
156
-
157
- def get_chat(chat_id)
158
- @api.get_chat(chat_id: chat_id)
159
- end
160
-
161
- def get_member(chat_id, user_id)
162
- @api.get_chat_member(chat_id: chat_id, user_id: user_id)
163
- end
164
-
165
- def warn(chat_id, user_id, reason: nil)
166
- key = warn_key(chat_id, user_id)
167
- current = load_warns(key)
168
- current += 1
169
- store_warns(key, current)
170
-
171
- {
172
- user_id: user_id,
173
- warns: current,
174
- limit: @warn_limit,
175
- reason: reason,
176
- action: (current >= @warn_limit ? :kick : :none)
177
- }
178
- end
179
-
180
- def reset_warns(chat_id, user_id)
181
- key = warn_key(chat_id, user_id)
182
- store_warns(key, 0)
183
- end
184
-
185
- def enforce_warns(chat_id, user_id, reason: nil)
186
- result = warn(chat_id, user_id, reason: reason)
187
- return result unless result[:action] == :kick
188
-
189
- kick(chat_id, user_id)
190
- reset_warns(chat_id, user_id)
191
- result
192
- end
193
-
194
- private
195
-
196
- def warn_key(chat_id, user_id)
197
- "warns:#{chat_id}:#{user_id}"
198
- end
199
-
200
- def load_warns(key)
201
- return 0 unless @session
202
-
203
- value = @session.get(key)
204
- value.is_a?(Hash) ? value["count"].to_i : value.to_i
205
- end
206
-
207
- def store_warns(key, count)
208
- return unless @session
209
-
210
- @session.set(key, { "count" => count })
211
- end
212
- end
213
- end
1
+ module GrubY
2
+ class GroupManager
3
+ DEFAULT_WARN_LIMIT = 3
4
+
5
+ def initialize(api, session: nil, warn_limit: DEFAULT_WARN_LIMIT)
6
+ @api = api
7
+ @session = session
8
+ @warn_limit = warn_limit
9
+ end
10
+
11
+ def ban(chat_id, user_id, **opts)
12
+ @api.ban_chat_member(chat_id: chat_id, user_id: user_id, **opts)
13
+ end
14
+
15
+ def unban(chat_id, user_id, **opts)
16
+ @api.unban_chat_member(chat_id: chat_id, user_id: user_id, **opts)
17
+ end
18
+
19
+ def kick(chat_id, user_id, **opts)
20
+ ban(chat_id, user_id, **opts)
21
+ unban(chat_id, user_id)
22
+ end
23
+
24
+ def restrict(chat_id, user_id, permissions:, **opts)
25
+ @api.restrict_chat_member(
26
+ chat_id: chat_id,
27
+ user_id: user_id,
28
+ permissions: permissions,
29
+ **opts
30
+ )
31
+ end
32
+
33
+ def mute(chat_id, user_id, until_date: nil)
34
+ restrict(
35
+ chat_id,
36
+ user_id,
37
+ permissions: { can_send_messages: false },
38
+ until_date: until_date
39
+ )
40
+ end
41
+
42
+ def unmute(chat_id, user_id)
43
+ permissions = {
44
+ can_send_messages: true,
45
+ can_send_audios: true,
46
+ can_send_documents: true,
47
+ can_send_photos: true,
48
+ can_send_videos: true,
49
+ can_send_video_notes: true,
50
+ can_send_voice_notes: true,
51
+ can_send_polls: true,
52
+ can_send_other_messages: true,
53
+ can_add_web_page_previews: true
54
+ }
55
+ restrict(chat_id, user_id, permissions: permissions)
56
+ end
57
+
58
+ def promote(chat_id, user_id, **opts)
59
+ @api.promote_chat_member(chat_id: chat_id, user_id: user_id, **opts)
60
+ end
61
+
62
+ def demote(chat_id, user_id)
63
+ promote(
64
+ chat_id,
65
+ user_id,
66
+ can_manage_chat: false,
67
+ can_delete_messages: false,
68
+ can_manage_video_chats: false,
69
+ can_restrict_members: false,
70
+ can_promote_members: false,
71
+ can_change_info: false,
72
+ can_invite_users: false,
73
+ can_post_stories: false,
74
+ can_edit_stories: false,
75
+ can_delete_stories: false,
76
+ can_post_messages: false,
77
+ can_edit_messages: false,
78
+ can_pin_messages: false,
79
+ can_manage_topics: false
80
+ )
81
+ end
82
+
83
+ def set_member_tag(chat_id, user_id, tag = nil)
84
+ @api.set_chat_member_tag(chat_id: chat_id, user_id: user_id, tag: tag)
85
+ end
86
+
87
+ def set_admin_title(chat_id, user_id, title)
88
+ @api.set_chat_administrator_custom_title(
89
+ chat_id: chat_id,
90
+ user_id: user_id,
91
+ custom_title: title
92
+ )
93
+ end
94
+
95
+ def set_administrator_title(chat_id, user_id, title)
96
+ set_admin_title(chat_id, user_id, title)
97
+ end
98
+
99
+ def set_chat_title(chat_id, title)
100
+ @api.set_chat_title(chat_id: chat_id, title: title)
101
+ end
102
+
103
+ def set_chat_description(chat_id, description)
104
+ @api.set_chat_description(chat_id: chat_id, description: description)
105
+ end
106
+
107
+ def set_chat_photo(chat_id, photo)
108
+ @api.set_chat_photo(chat_id: chat_id, photo: photo)
109
+ end
110
+
111
+ def delete_chat_photo(chat_id)
112
+ @api.delete_chat_photo(chat_id: chat_id)
113
+ end
114
+
115
+ def leave_chat(chat_id)
116
+ @api.leave_chat(chat_id: chat_id)
117
+ end
118
+
119
+ def set_permissions(chat_id, permissions, **opts)
120
+ @api.set_chat_permissions(chat_id: chat_id, permissions: permissions, **opts)
121
+ end
122
+
123
+ def lock(chat_id)
124
+ set_permissions(chat_id, { can_send_messages: false })
125
+ end
126
+
127
+ def unlock(chat_id)
128
+ set_permissions(chat_id, { can_send_messages: true })
129
+ end
130
+
131
+ def set_slow_mode(chat_id, seconds)
132
+ @api.set_slow_mode(chat_id: chat_id, seconds: seconds)
133
+ end
134
+
135
+ def pin(chat_id, message_id, **opts)
136
+ @api.pin_chat_message(chat_id: chat_id, message_id: message_id, **opts)
137
+ end
138
+
139
+ def unpin(chat_id, message_id = nil, **opts)
140
+ params = { chat_id: chat_id }.merge(opts)
141
+ params[:message_id] = message_id if message_id
142
+ @api.unpin_chat_message(**params)
143
+ end
144
+
145
+ def unpin_all(chat_id)
146
+ @api.unpin_all_chat_messages(chat_id: chat_id)
147
+ end
148
+
149
+ def approve_join_request(chat_id, user_id)
150
+ @api.approve_chat_join_request(chat_id: chat_id, user_id: user_id)
151
+ end
152
+
153
+ def decline_join_request(chat_id, user_id)
154
+ @api.decline_chat_join_request(chat_id: chat_id, user_id: user_id)
155
+ end
156
+
157
+ def get_chat(chat_id)
158
+ @api.get_chat(chat_id: chat_id)
159
+ end
160
+
161
+ def get_member(chat_id, user_id)
162
+ @api.get_chat_member(chat_id: chat_id, user_id: user_id)
163
+ end
164
+
165
+ def warn(chat_id, user_id, reason: nil)
166
+ key = warn_key(chat_id, user_id)
167
+ current = load_warns(key)
168
+ current += 1
169
+ store_warns(key, current)
170
+
171
+ {
172
+ user_id: user_id,
173
+ warns: current,
174
+ limit: @warn_limit,
175
+ reason: reason,
176
+ action: (current >= @warn_limit ? :kick : :none)
177
+ }
178
+ end
179
+
180
+ def reset_warns(chat_id, user_id)
181
+ key = warn_key(chat_id, user_id)
182
+ store_warns(key, 0)
183
+ end
184
+
185
+ def enforce_warns(chat_id, user_id, reason: nil)
186
+ result = warn(chat_id, user_id, reason: reason)
187
+ return result unless result[:action] == :kick
188
+
189
+ kick(chat_id, user_id)
190
+ reset_warns(chat_id, user_id)
191
+ result
192
+ end
193
+
194
+ private
195
+
196
+ def warn_key(chat_id, user_id)
197
+ "warns:#{chat_id}:#{user_id}"
198
+ end
199
+
200
+ def load_warns(key)
201
+ return 0 unless @session
202
+
203
+ value = @session.get(key)
204
+ value.is_a?(Hash) ? value["count"].to_i : value.to_i
205
+ end
206
+
207
+ def store_warns(key, count)
208
+ return unless @session
209
+
210
+ @session.set(key, { "count" => count })
211
+ end
212
+ end
213
+ end