discordrb 1.6.6 → 1.7.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 +4 -4
- data/.travis.yml +3 -0
- data/CHANGELOG.md +26 -0
- data/README.md +1 -1
- data/discordrb.gemspec +2 -0
- data/lib/discordrb.rb +1 -1
- data/lib/discordrb/api.rb +36 -5
- data/lib/discordrb/await.rb +3 -1
- data/lib/discordrb/bot.rb +70 -275
- data/lib/discordrb/commands/command_bot.rb +81 -28
- data/lib/discordrb/commands/container.rb +78 -0
- data/lib/discordrb/commands/events.rb +2 -12
- data/lib/discordrb/commands/parser.rb +40 -4
- data/lib/discordrb/commands/rate_limiter.rb +141 -0
- data/lib/discordrb/container.rb +379 -0
- data/lib/discordrb/data.rb +52 -4
- data/lib/discordrb/errors.rb +24 -0
- data/lib/discordrb/events/bans.rb +10 -8
- data/lib/discordrb/events/generic.rb +38 -2
- data/lib/discordrb/events/guilds.rb +11 -1
- data/lib/discordrb/events/lifetime.rb +6 -0
- data/lib/discordrb/events/members.rb +9 -1
- data/lib/discordrb/events/message.rb +33 -1
- data/lib/discordrb/logger.rb +7 -1
- data/lib/discordrb/permissions.rb +3 -0
- data/lib/discordrb/token_cache.rb +38 -4
- data/lib/discordrb/version.rb +1 -1
- data/lib/discordrb/voice/encoder.rb +29 -19
- data/lib/discordrb/voice/network.rb +6 -11
- data/lib/discordrb/voice/voice_bot.rb +112 -43
- metadata +34 -3
- data/lib/discordrb/exceptions.rb +0 -12
@@ -0,0 +1,379 @@
|
|
1
|
+
require 'discordrb/events/message'
|
2
|
+
require 'discordrb/events/typing'
|
3
|
+
require 'discordrb/events/lifetime'
|
4
|
+
require 'discordrb/events/presence'
|
5
|
+
require 'discordrb/events/voice_state_update'
|
6
|
+
require 'discordrb/events/channel_create'
|
7
|
+
require 'discordrb/events/channel_update'
|
8
|
+
require 'discordrb/events/channel_delete'
|
9
|
+
require 'discordrb/events/members'
|
10
|
+
require 'discordrb/events/guild_role_create'
|
11
|
+
require 'discordrb/events/guild_role_delete'
|
12
|
+
require 'discordrb/events/guild_role_update'
|
13
|
+
require 'discordrb/events/guilds'
|
14
|
+
require 'discordrb/events/await'
|
15
|
+
require 'discordrb/events/bans'
|
16
|
+
|
17
|
+
require 'discordrb/await'
|
18
|
+
|
19
|
+
module Discordrb
|
20
|
+
# This module provides the functionality required for events and awaits. It is separated
|
21
|
+
# from the {Bot} class so users can make their own container modules and include them.
|
22
|
+
module EventContainer
|
23
|
+
# This **event** is raised when a message is sent to a text channel the bot is currently in.
|
24
|
+
# @param attributes [Hash] The event's attributes.
|
25
|
+
# @option attributes [String, Regexp] :start_with Matches the string the message starts with.
|
26
|
+
# @option attributes [String, Regexp] :end_with Matches the string the message ends with.
|
27
|
+
# @option attributes [String, Regexp] :contains Matches a string the message contains.
|
28
|
+
# @option attributes [String, Integer, Channel] :in Matches the channel the message was sent in.
|
29
|
+
# @option attributes [String, Integer, User] :from Matches the user that sent the message.
|
30
|
+
# @option attributes [String] :content Exactly matches the entire content of the message.
|
31
|
+
# @option attributes [String] :content Exactly matches the entire content of the message.
|
32
|
+
# @option attributes [Time] :after Matches a time after the time the message was sent at.
|
33
|
+
# @option attributes [Time] :before Matches a time before the time the message was sent at.
|
34
|
+
# @option attributes [Boolean] :private Matches whether or not the channel is private.
|
35
|
+
# @yield The block is executed when the event is raised.
|
36
|
+
# @yieldparam event [MessageEvent] The event that was raised.
|
37
|
+
# @return [MessageEventHandler] The event handler that was registered.
|
38
|
+
def message(attributes = {}, &block)
|
39
|
+
register_event(MessageEvent, attributes, block)
|
40
|
+
end
|
41
|
+
|
42
|
+
# This **event** is raised when the READY packet is received, i. e. servers and channels have finished
|
43
|
+
# initialization. It's the recommended way to do things when the bot has finished starting up.
|
44
|
+
# @param attributes [Hash] Event attributes, none in this particular case
|
45
|
+
# @yield The block is executed when the event is raised.
|
46
|
+
# @yieldparam event [ReadyEvent] The event that was raised.
|
47
|
+
# @return [ReadyEventHandler] The event handler that was registered.
|
48
|
+
def ready(attributes = {}, &block)
|
49
|
+
register_event(ReadyEvent, attributes, block)
|
50
|
+
end
|
51
|
+
|
52
|
+
# This **event** is raised when the bot has disconnected from the WebSocket, due to the {Bot#stop} method or
|
53
|
+
# external causes. It's the recommended way to do clean-up tasks.
|
54
|
+
# @param attributes [Hash] Event attributes, none in this particular case
|
55
|
+
# @yield The block is executed when the event is raised.
|
56
|
+
# @yieldparam event [DisconnectEvent] The event that was raised.
|
57
|
+
# @return [DisconnectEventHandler] The event handler that was registered.
|
58
|
+
def disconnected(attributes = {}, &block)
|
59
|
+
register_event(DisconnectEvent, attributes, block)
|
60
|
+
end
|
61
|
+
|
62
|
+
# This **event** is raised when somebody starts typing in a channel the bot is also in. The official Discord
|
63
|
+
# client would display the typing indicator for five seconds after receiving this event. If the user continues
|
64
|
+
# typing after five seconds, the event will be re-raised.
|
65
|
+
# @param attributes [Hash] The event's attributes.
|
66
|
+
# @option attributes [String, Integer, Channel] :in Matches the channel where typing was started.
|
67
|
+
# @option attributes [String, Integer, User] :from Matches the user that started typing.
|
68
|
+
# @option attributes [Time] :after Matches a time after the time the typing started.
|
69
|
+
# @option attributes [Time] :before Matches a time before the time the typing started.
|
70
|
+
# @yield The block is executed when the event is raised.
|
71
|
+
# @yieldparam event [TypingEvent] The event that was raised.
|
72
|
+
# @return [TypingEventHandler] The event handler that was registered.
|
73
|
+
def typing(attributes = {}, &block)
|
74
|
+
register_event(TypingEvent, attributes, block)
|
75
|
+
end
|
76
|
+
|
77
|
+
# This **event** is raised when a message is edited in a channel.
|
78
|
+
# @param attributes [Hash] The event's attributes.
|
79
|
+
# @option attributes [#resolve_id] :id Matches the ID of the message that was edited.
|
80
|
+
# @option attributes [String, Integer, Channel] :in Matches the channel the message was edited in.
|
81
|
+
# @yield The block is executed when the event is raised.
|
82
|
+
# @yieldparam event [MessageEditEvent] The event that was raised.
|
83
|
+
# @return [MessageEditEventHandler] The event handler that was registered.
|
84
|
+
def message_edit(attributes = {}, &block)
|
85
|
+
register_event(MessageEditEvent, attributes, block)
|
86
|
+
end
|
87
|
+
|
88
|
+
# This **event** is raised when a message is deleted in a channel.
|
89
|
+
# @param attributes [Hash] The event's attributes.
|
90
|
+
# @option attributes [#resolve_id] :id Matches the ID of the message that was deleted.
|
91
|
+
# @option attributes [String, Integer, Channel] :in Matches the channel the message was deleted in.
|
92
|
+
# @yield The block is executed when the event is raised.
|
93
|
+
# @yieldparam event [MessageDeleteEvent] The event that was raised.
|
94
|
+
# @return [MessageDeleteEventHandler] The event handler that was registered.
|
95
|
+
def message_delete(attributes = {}, &block)
|
96
|
+
register_event(MessageDeleteEvent, attributes, block)
|
97
|
+
end
|
98
|
+
|
99
|
+
# This **event** is raised when a user's status (online/offline/idle) changes.
|
100
|
+
# @param attributes [Hash] The event's attributes.
|
101
|
+
# @option attributes [String, Integer, User] :from Matches the user whose status changed.
|
102
|
+
# @option attributes [:offline, :idle, :online] :status Matches the status the user has now.
|
103
|
+
# @yield The block is executed when the event is raised.
|
104
|
+
# @yieldparam event [PresenceEvent] The event that was raised.
|
105
|
+
# @return [PresenceEventHandler] The event handler that was registered.
|
106
|
+
def presence(attributes = {}, &block)
|
107
|
+
register_event(PresenceEvent, attributes, block)
|
108
|
+
end
|
109
|
+
|
110
|
+
# This **event** is raised when the game a user is playing changes.
|
111
|
+
# @param attributes [Hash] The event's attributes.
|
112
|
+
# @option attributes [String, Integer, User] :from Matches the user whose playing game changes.
|
113
|
+
# @option attributes [String] :game Matches the game the user is now playing.
|
114
|
+
# @yield The block is executed when the event is raised.
|
115
|
+
# @yieldparam event [PlayingEvent] The event that was raised.
|
116
|
+
# @return [PlayingEventHandler] The event handler that was registered.
|
117
|
+
def playing(attributes = {}, &block)
|
118
|
+
register_event(PlayingEvent, attributes, block)
|
119
|
+
end
|
120
|
+
|
121
|
+
# This **event** is raised when the bot is mentioned in a message.
|
122
|
+
# @param attributes [Hash] The event's attributes.
|
123
|
+
# @option attributes [String, Regexp] :start_with Matches the string the message starts with.
|
124
|
+
# @option attributes [String, Regexp] :end_with Matches the string the message ends with.
|
125
|
+
# @option attributes [String, Regexp] :contains Matches a string the message contains.
|
126
|
+
# @option attributes [String, Integer, Channel] :in Matches the channel the message was sent in.
|
127
|
+
# @option attributes [String, Integer, User] :from Matches the user that sent the message.
|
128
|
+
# @option attributes [String] :content Exactly matches the entire content of the message.
|
129
|
+
# @option attributes [String] :content Exactly matches the entire content of the message.
|
130
|
+
# @option attributes [Time] :after Matches a time after the time the message was sent at.
|
131
|
+
# @option attributes [Time] :before Matches a time before the time the message was sent at.
|
132
|
+
# @option attributes [Boolean] :private Matches whether or not the channel is private.
|
133
|
+
# @yield The block is executed when the event is raised.
|
134
|
+
# @yieldparam event [MentionEvent] The event that was raised.
|
135
|
+
# @return [MentionEventHandler] The event handler that was registered.
|
136
|
+
def mention(attributes = {}, &block)
|
137
|
+
register_event(MentionEvent, attributes, block)
|
138
|
+
end
|
139
|
+
|
140
|
+
# This **event** is raised when a channel is created.
|
141
|
+
# @param attributes [Hash] The event's attributes.
|
142
|
+
# @option attributes [String] :type Matches the type of channel that is being created (text or voice)
|
143
|
+
# @option attributes [String] :name Matches the name of the created channel.
|
144
|
+
# @yield The block is executed when the event is raised.
|
145
|
+
# @yieldparam event [ChannelCreateEvent] The event that was raised.
|
146
|
+
# @return [ChannelCreateEventHandler] The event handler that was registered.
|
147
|
+
def channel_create(attributes = {}, &block)
|
148
|
+
register_event(ChannelCreateEvent, attributes, block)
|
149
|
+
end
|
150
|
+
|
151
|
+
# This **event** is raised when a channel is updated.
|
152
|
+
# @param attributes [Hash] The event's attributes.
|
153
|
+
# @option attributes [String] :type Matches the type of channel that is being updated (text or voice)
|
154
|
+
# @option attributes [String] :name Matches the new name of the channel.
|
155
|
+
# @yield The block is executed when the event is raised.
|
156
|
+
# @yieldparam event [ChannelUpdateEvent] The event that was raised.
|
157
|
+
# @return [ChannelUpdateEventHandler] The event handler that was registered.
|
158
|
+
def channel_update(attributes = {}, &block)
|
159
|
+
register_event(ChannelUpdateEvent, attributes, block)
|
160
|
+
end
|
161
|
+
|
162
|
+
# This **event** is raised when a channel is updated.
|
163
|
+
# @param attributes [Hash] The event's attributes.
|
164
|
+
# @option attributes [String] :type Matches the type of channel that is being deleted (text or voice)
|
165
|
+
# @option attributes [String] :name Matches the name of the deleted channel.
|
166
|
+
# @yield The block is executed when the event is raised.
|
167
|
+
# @yieldparam event [ChannelDeleteEvent] The event that was raised.
|
168
|
+
# @return [ChannelDeleteEventHandler] The event handler that was registered.
|
169
|
+
def channel_delete(attributes = {}, &block)
|
170
|
+
register_event(ChannelDeleteEvent, attributes, block)
|
171
|
+
end
|
172
|
+
|
173
|
+
# This **event** is raised when a user's voice state changes.
|
174
|
+
# @param attributes [Hash] The event's attributes.
|
175
|
+
# @option attributes [String, Integer, User] :from Matches the user that sent the message.
|
176
|
+
# @option attributes [String, Integer, Channel] :channel Matches the voice channel the user has joined.
|
177
|
+
# @option attributes [true, false] :mute Matches whether or not the user is muted server-wide.
|
178
|
+
# @option attributes [true, false] :deaf Matches whether or not the user is deafened server-wide.
|
179
|
+
# @option attributes [true, false] :self_mute Matches whether or not the user is muted by the bot.
|
180
|
+
# @option attributes [true, false] :self_deaf Matches whether or not the user is deafened by the bot.
|
181
|
+
# @yield The block is executed when the event is raised.
|
182
|
+
# @yieldparam event [VoiceStateUpdateEvent] The event that was raised.
|
183
|
+
# @return [VoiceStateUpdateEventHandler] The event handler that was registered.
|
184
|
+
def voice_state_update(attributes = {}, &block)
|
185
|
+
register_event(VoiceStateUpdateEvent, attributes, block)
|
186
|
+
end
|
187
|
+
|
188
|
+
# This **event** is raised when a new user joins a server.
|
189
|
+
# @param attributes [Hash] The event's attributes.
|
190
|
+
# @option attributes [String] :username Matches the username of the joined user.
|
191
|
+
# @yield The block is executed when the event is raised.
|
192
|
+
# @yieldparam event [GuildMemberAddEvent] The event that was raised.
|
193
|
+
# @return [GuildMemberAddEventHandler] The event handler that was registered.
|
194
|
+
def member_join(attributes = {}, &block)
|
195
|
+
register_event(GuildMemberAddEvent, attributes, block)
|
196
|
+
end
|
197
|
+
|
198
|
+
# This **event** is raised when a member update happens.
|
199
|
+
# @param attributes [Hash] The event's attributes.
|
200
|
+
# @option attributes [String] :username Matches the username of the updated user.
|
201
|
+
# @yield The block is executed when the event is raised.
|
202
|
+
# @yieldparam event [GuildMemberUpdateEvent] The event that was raised.
|
203
|
+
# @return [GuildMemberUpdateEventHandler] The event handler that was registered.
|
204
|
+
def member_update(attributes = {}, &block)
|
205
|
+
register_event(GuildMemberUpdateEvent, attributes, block)
|
206
|
+
end
|
207
|
+
|
208
|
+
# This **event** is raised when a member leaves a server.
|
209
|
+
# @param attributes [Hash] The event's attributes.
|
210
|
+
# @option attributes [String] :username Matches the username of the member.
|
211
|
+
# @yield The block is executed when the event is raised.
|
212
|
+
# @yieldparam event [GuildMemberDeleteEvent] The event that was raised.
|
213
|
+
# @return [GuildMemberDeleteEventHandler] The event handler that was registered.
|
214
|
+
def member_leave(attributes = {}, &block)
|
215
|
+
register_event(GuildMemberDeleteEvent, attributes, block)
|
216
|
+
end
|
217
|
+
|
218
|
+
# This **event** is raised when a user is banned from a server.
|
219
|
+
# @param attributes [Hash] The event's attributes.
|
220
|
+
# @option attributes [String, Integer, User] :user Matches the user that was banned.
|
221
|
+
# @option attributes [String, Integer, Server] :server Matches the server from which the user was banned.
|
222
|
+
# @yield The block is executed when the event is raised.
|
223
|
+
# @yieldparam event [UserBanEvent] The event that was raised.
|
224
|
+
# @return [UserBanEventHandler] The event handler that was registered.
|
225
|
+
def user_ban(attributes = {}, &block)
|
226
|
+
register_event(UserBanEvent, attributes, block)
|
227
|
+
end
|
228
|
+
|
229
|
+
# This **event** is raised when a user is unbanned from a server.
|
230
|
+
# @param attributes [Hash] The event's attributes.
|
231
|
+
# @option attributes [String, Integer, User] :user Matches the user that was unbanned.
|
232
|
+
# @option attributes [String, Integer, Server] :server Matches the server from which the user was unbanned.
|
233
|
+
# @yield The block is executed when the event is raised.
|
234
|
+
# @yieldparam event [UserUnbanEvent] The event that was raised.
|
235
|
+
# @return [UserUnbanEventHandler] The event handler that was registered.
|
236
|
+
def user_unban(attributes = {}, &block)
|
237
|
+
register_event(UserUnbanEvent, attributes, block)
|
238
|
+
end
|
239
|
+
|
240
|
+
# This **event** is raised when a server is created respective to the bot, i. e. the bot joins a server or creates
|
241
|
+
# a new one itself. It should never be necessary to listen to this event as it will only ever be triggered by
|
242
|
+
# things the bot itself does, but one can never know.
|
243
|
+
# @param attributes [Hash] The event's attributes.
|
244
|
+
# @option attributes [String, Integer, Server] :server Matches the server that was created.
|
245
|
+
# @yield The block is executed when the event is raised.
|
246
|
+
# @yieldparam event [GuildCreateEvent] The event that was raised.
|
247
|
+
# @return [GuildCreateEventHandler] The event handler that was registered.
|
248
|
+
def server_create(attributes = {}, &block)
|
249
|
+
register_event(GuildCreateEvent, attributes, block)
|
250
|
+
end
|
251
|
+
|
252
|
+
# This **event** is raised when a server is updated, for example if the name or region has changed.
|
253
|
+
# @param attributes [Hash] The event's attributes.
|
254
|
+
# @option attributes [String, Integer, Server] :server Matches the server that was updated.
|
255
|
+
# @yield The block is executed when the event is raised.
|
256
|
+
# @yieldparam event [GuildUpdateEvent] The event that was raised.
|
257
|
+
# @return [GuildUpdateEventHandler] The event handler that was registered.
|
258
|
+
def server_update(attributes = {}, &block)
|
259
|
+
register_event(GuildUpdateEvent, attributes, block)
|
260
|
+
end
|
261
|
+
|
262
|
+
# This **event** is raised when a server is deleted, or when the bot leaves a server. (These two cases are identical
|
263
|
+
# to Discord.)
|
264
|
+
# @param attributes [Hash] The event's attributes.
|
265
|
+
# @option attributes [String, Integer, Server] :server Matches the server that was deleted.
|
266
|
+
# @yield The block is executed when the event is raised.
|
267
|
+
# @yieldparam event [GuildDeleteEvent] The event that was raised.
|
268
|
+
# @return [GuildDeleteEventHandler] The event handler that was registered.
|
269
|
+
def server_delete(attributes = {}, &block)
|
270
|
+
register_event(GuildDeleteEvent, attributes, block)
|
271
|
+
end
|
272
|
+
|
273
|
+
# This **event** is raised when an {Await} is triggered. It provides an easy way to execute code
|
274
|
+
# on an await without having to rely on the await's block.
|
275
|
+
# @param attributes [Hash] The event's attributes.
|
276
|
+
# @option attributes [Symbol] :key Exactly matches the await's key.
|
277
|
+
# @option attributes [Class] :type Exactly matches the event's type.
|
278
|
+
# @yield The block is executed when the event is raised.
|
279
|
+
# @yieldparam event [AwaitEvent] The event that was raised.
|
280
|
+
# @return [AwaitEventHandler] The event handler that was registered.
|
281
|
+
def await(attributes = {}, &block)
|
282
|
+
register_event(AwaitEvent, attributes, block)
|
283
|
+
end
|
284
|
+
|
285
|
+
# This **event** is raised when a private message is sent to the bot.
|
286
|
+
# @param attributes [Hash] The event's attributes.
|
287
|
+
# @option attributes [String, Regexp] :start_with Matches the string the message starts with.
|
288
|
+
# @option attributes [String, Regexp] :end_with Matches the string the message ends with.
|
289
|
+
# @option attributes [String, Regexp] :contains Matches a string the message contains.
|
290
|
+
# @option attributes [String, Integer, Channel] :in Matches the channel the message was sent in.
|
291
|
+
# @option attributes [String, Integer, User] :from Matches the user that sent the message.
|
292
|
+
# @option attributes [String] :content Exactly matches the entire content of the message.
|
293
|
+
# @option attributes [String] :content Exactly matches the entire content of the message.
|
294
|
+
# @option attributes [Time] :after Matches a time after the time the message was sent at.
|
295
|
+
# @option attributes [Time] :before Matches a time before the time the message was sent at.
|
296
|
+
# @option attributes [Boolean] :private Matches whether or not the channel is private.
|
297
|
+
# @yield The block is executed when the event is raised.
|
298
|
+
# @yieldparam event [PrivateMessageEvent] The event that was raised.
|
299
|
+
# @return [PrivateMessageEventHandler] The event handler that was registered.
|
300
|
+
def pm(attributes = {}, &block)
|
301
|
+
register_event(PrivateMessageEvent, attributes, block)
|
302
|
+
end
|
303
|
+
|
304
|
+
alias_method :private_message, :pm
|
305
|
+
|
306
|
+
# Removes an event handler from this container. If you're looking for a way to do temporary events, I recommend
|
307
|
+
# {Await}s instead of this.
|
308
|
+
# @param handler [Discordrb::Events::EventHandler] The handler to remove.
|
309
|
+
def remove_handler(handler)
|
310
|
+
clazz = EventContainer.event_class(handler.class)
|
311
|
+
@event_handlers ||= {}
|
312
|
+
@event_handlers[clazz].delete(handler)
|
313
|
+
end
|
314
|
+
|
315
|
+
# Adds an event handler to this container. Usually, it's more expressive to just use one of the shorthand adder
|
316
|
+
# methods like {#message}, but if you want to create one manually you can use this.
|
317
|
+
# @param handler [Discordrb::Events::EventHandler] The handler to add.
|
318
|
+
def add_handler(handler)
|
319
|
+
clazz = EventContainer.event_class(handler.class)
|
320
|
+
@event_handlers ||= {}
|
321
|
+
@event_handlers[clazz] << handler
|
322
|
+
end
|
323
|
+
|
324
|
+
# Adds all event handlers from another container into this one. Existing event handlers will be overwritten.
|
325
|
+
# @param container [Module] A module that `extend`s {EventContainer} from which the handlers will be added.
|
326
|
+
def include_events(container)
|
327
|
+
handlers = container.instance_variable_get '@event_handlers'
|
328
|
+
fail "Couldn't include the container #{container} as it doesn't have any event handlers - have you tried to include a commands container into an event-only bot?" unless handlers
|
329
|
+
@event_handlers ||= {}
|
330
|
+
@event_handlers.merge! handlers
|
331
|
+
end
|
332
|
+
|
333
|
+
alias_method :include!, :include_events
|
334
|
+
alias_method :<<, :add_handler
|
335
|
+
|
336
|
+
# Returns the handler class for an event class type
|
337
|
+
# @see #event_class
|
338
|
+
# @param event_class [Class] The event type
|
339
|
+
# @return [Class] the handler type
|
340
|
+
def self.handler_class(event_class)
|
341
|
+
class_from_string(event_class.to_s + 'Handler')
|
342
|
+
end
|
343
|
+
|
344
|
+
# Returns the event class for a handler class type
|
345
|
+
# @see #handler_class
|
346
|
+
# @param handler_class [Class] The handler type
|
347
|
+
# @return [Class, nil] the event type, or nil if the handler_class isn't a handler class (i. e. ends with Handler)
|
348
|
+
def self.event_class(handler_class)
|
349
|
+
class_name = handler_class.to_s
|
350
|
+
return nil unless class_name.end_with? 'Handler'
|
351
|
+
|
352
|
+
EventContainer.class_from_string(class_name[0..-8])
|
353
|
+
end
|
354
|
+
|
355
|
+
# Utility method to return a class object from a string of its name. Mostly useful for internal stuff
|
356
|
+
# @param str [String] The name of the class
|
357
|
+
# @return [Class] the class
|
358
|
+
def self.class_from_string(str)
|
359
|
+
str.split('::').inject(Object) do |mod, class_name|
|
360
|
+
mod.const_get(class_name)
|
361
|
+
end
|
362
|
+
end
|
363
|
+
|
364
|
+
private
|
365
|
+
|
366
|
+
include Discordrb::Events
|
367
|
+
|
368
|
+
def register_event(clazz, attributes, block)
|
369
|
+
handler = EventContainer.handler_class(clazz).new(attributes, block)
|
370
|
+
|
371
|
+
@event_handlers ||= {}
|
372
|
+
@event_handlers[clazz] ||= []
|
373
|
+
@event_handlers[clazz] << handler
|
374
|
+
|
375
|
+
# Return the handler so it can be removed later
|
376
|
+
handler
|
377
|
+
end
|
378
|
+
end
|
379
|
+
end
|
data/lib/discordrb/data.rb
CHANGED
@@ -147,6 +147,13 @@ module Discordrb
|
|
147
147
|
@roles[server.id] = roles
|
148
148
|
end
|
149
149
|
|
150
|
+
# Set the user's name
|
151
|
+
# @note for internal use only
|
152
|
+
# @!visibility private
|
153
|
+
def update_username(username)
|
154
|
+
@username = username
|
155
|
+
end
|
156
|
+
|
150
157
|
# Merge this user's roles with the roles from another instance of this user (from another server)
|
151
158
|
# @note For internal use only
|
152
159
|
# @!visibility private
|
@@ -624,6 +631,26 @@ module Discordrb
|
|
624
631
|
JSON.parse(logs).map { |message| Message.new(message, @bot) }
|
625
632
|
end
|
626
633
|
|
634
|
+
# Deletes the last N messages on this channel.
|
635
|
+
# @note Each delete request is performed in a separate thread for performance reasons, so if a large number of
|
636
|
+
# messages are pruned, many threads will be created.
|
637
|
+
# @param amount [Integer] How many messages to delete. Must be 100 or less (Discord limitation)
|
638
|
+
# @raise [ArgumentError] if more than 100 messages are requested.
|
639
|
+
def prune(amount)
|
640
|
+
fail ArgumentError, "Can't prune more than 100 messages!" if amount > 100
|
641
|
+
|
642
|
+
threads = []
|
643
|
+
history(amount).each do |message|
|
644
|
+
threads << Thread.new { message.delete }
|
645
|
+
end
|
646
|
+
|
647
|
+
# Make sure all requests have finished
|
648
|
+
threads.each(&:join)
|
649
|
+
|
650
|
+
# Delete the threads
|
651
|
+
threads.map! { nil }
|
652
|
+
end
|
653
|
+
|
627
654
|
# Updates the cached permission overwrites
|
628
655
|
# @note For internal use only
|
629
656
|
# @!visibility private
|
@@ -638,6 +665,12 @@ module Discordrb
|
|
638
665
|
@bot.add_await(key, Discordrb::Events::MessageEvent, { in: @id }.merge(attributes), &block)
|
639
666
|
end
|
640
667
|
|
668
|
+
# Creates a new invite to this channel.
|
669
|
+
# @param max_age [Integer] How many seconds this invite should last.
|
670
|
+
# @param max_uses [Integer] How many times this invite should be able to be used.
|
671
|
+
# @param temporary [true, false] Whether membership should be temporary (kicked after going offline).
|
672
|
+
# @param xkcd [true, false] Whether or not the invite should be human-readable.
|
673
|
+
# @return [Invite] the created invite.
|
641
674
|
def make_invite(max_age = 0, max_uses = 0, temporary = false, xkcd = false)
|
642
675
|
response = API.create_invite(@bot.token, @id, max_age, max_uses, temporary, xkcd)
|
643
676
|
Invite.new(JSON.parse(response), @bot)
|
@@ -756,6 +789,7 @@ module Discordrb
|
|
756
789
|
|
757
790
|
# @return [Array<User>] an array of all the users on this server.
|
758
791
|
attr_reader :members
|
792
|
+
alias_method :users, :members
|
759
793
|
|
760
794
|
# @return [Array<Channel>] an array of all the channels (text and voice) on this server.
|
761
795
|
attr_reader :channels
|
@@ -763,6 +797,14 @@ module Discordrb
|
|
763
797
|
# @return [Array<Role>] an array of all the roles created on this server.
|
764
798
|
attr_reader :roles
|
765
799
|
|
800
|
+
# @return [true, false] whether or not this server is large (members > 100). If it is,
|
801
|
+
# it means the members list may be inaccurate for a couple seconds after starting up the bot.
|
802
|
+
attr_reader :large
|
803
|
+
alias_method :large?, :large
|
804
|
+
|
805
|
+
# @return [Integer] the absolute number of members on this server, offline or not.
|
806
|
+
attr_reader :member_count
|
807
|
+
|
766
808
|
# @todo Make this behave like user.avatar where a URL is available as well.
|
767
809
|
# @return [String] the hexadecimal ID used to identify this server's icon.
|
768
810
|
attr_reader :icon
|
@@ -784,6 +826,9 @@ module Discordrb
|
|
784
826
|
@id = data['id'].to_i
|
785
827
|
update_data(data)
|
786
828
|
|
829
|
+
@large = data['large']
|
830
|
+
@member_count = data['member_count']
|
831
|
+
|
787
832
|
process_roles(data['roles'])
|
788
833
|
process_members(data['members'])
|
789
834
|
process_presences(data['presences'])
|
@@ -836,13 +881,15 @@ module Discordrb
|
|
836
881
|
# @!visibility private
|
837
882
|
def add_user(user)
|
838
883
|
@members << user unless @members.include? user
|
884
|
+
@member_count += 1
|
839
885
|
end
|
840
886
|
|
841
887
|
# Removes a user from the user cache.
|
842
888
|
# @note For internal use only
|
843
889
|
# @!visibility private
|
844
890
|
def delete_user(user_id)
|
845
|
-
@members.reject! { |member| member.id == user_id }
|
891
|
+
@members.reject! { |member| member.id == user_id }.length
|
892
|
+
@member_count -= 1
|
846
893
|
end
|
847
894
|
|
848
895
|
# Creates a channel on this server with the given name.
|
@@ -894,9 +941,10 @@ module Discordrb
|
|
894
941
|
API.delete_server(@bot.token, @id)
|
895
942
|
end
|
896
943
|
|
897
|
-
# Leave the server
|
898
|
-
|
899
|
-
|
944
|
+
# Leave the server
|
945
|
+
def leave
|
946
|
+
API.leave_server(@bot.token, @id)
|
947
|
+
end
|
900
948
|
|
901
949
|
# Transfers server ownership to another user.
|
902
950
|
# @param user [User] The user who should become the new owner.
|