@@ -173,116 +173,6 @@ async def on_command_error(self, context: Context, exception: errors.CommandErro
173
173
print (f'Ignoring exception in command { context .command } :' , file = sys .stderr )
174
174
traceback .print_exception (type (exception ), exception , exception .__traceback__ , file = sys .stderr )
175
175
176
- # global check registration
177
-
178
- def check (self , func : T ) -> T :
179
- r"""A decorator that adds a global check to the bot.
180
-
181
- A global check is similar to a :func:`.check` that is applied
182
- on a per command basis except it is run before any command checks
183
- have been verified and applies to every command the bot has.
184
-
185
- .. note::
186
-
187
- This function can either be a regular function or a coroutine.
188
-
189
- Similar to a command :func:`.check`\, this takes a single parameter
190
- of type :class:`.Context` and can only raise exceptions inherited from
191
- :exc:`.CommandError`.
192
-
193
- Example
194
- ---------
195
-
196
- .. code-block:: python3
197
-
198
- @bot.check
199
- def check_commands(ctx):
200
- return ctx.command.qualified_name in allowed_commands
201
-
202
- """
203
- # T was used instead of Check to ensure the type matches on return
204
- self .add_check (func ) # type: ignore
205
- return func
206
-
207
- def add_check (self , func : Check , * , call_once : bool = False ) -> None :
208
- """Adds a global check to the bot.
209
-
210
- This is the non-decorator interface to :meth:`.check`
211
- and :meth:`.check_once`.
212
-
213
- Parameters
214
- -----------
215
- func
216
- The function that was used as a global check.
217
- call_once: :class:`bool`
218
- If the function should only be called once per
219
- :meth:`.invoke` call.
220
- """
221
-
222
- if call_once :
223
- self ._check_once .append (func )
224
- else :
225
- self ._checks .append (func )
226
-
227
- def remove_check (self , func : Check , * , call_once : bool = False ) -> None :
228
- """Removes a global check from the bot.
229
-
230
- This function is idempotent and will not raise an exception
231
- if the function is not in the global checks.
232
-
233
- Parameters
234
- -----------
235
- func
236
- The function to remove from the global checks.
237
- call_once: :class:`bool`
238
- If the function was added with ``call_once=True`` in
239
- the :meth:`.Bot.add_check` call or using :meth:`.check_once`.
240
- """
241
- l = self ._check_once if call_once else self ._checks
242
-
243
- try :
244
- l .remove (func )
245
- except ValueError :
246
- pass
247
-
248
- def check_once (self , func : CFT ) -> CFT :
249
- r"""A decorator that adds a "call once" global check to the bot.
250
-
251
- Unlike regular global checks, this one is called only once
252
- per :meth:`.invoke` call.
253
-
254
- Regular global checks are called whenever a command is called
255
- or :meth:`.Command.can_run` is called. This type of check
256
- bypasses that and ensures that it's called only once, even inside
257
- the default help command.
258
-
259
- .. note::
260
-
261
- When using this function the :class:`.Context` sent to a group subcommand
262
- may only parse the parent command and not the subcommands due to it
263
- being invoked once per :meth:`.Bot.invoke` call.
264
-
265
- .. note::
266
-
267
- This function can either be a regular function or a coroutine.
268
-
269
- Similar to a command :func:`.check`\, this takes a single parameter
270
- of type :class:`.Context` and can only raise exceptions inherited from
271
- :exc:`.CommandError`.
272
-
273
- Example
274
- ---------
275
-
276
- .. code-block:: python3
277
-
278
- @bot.check_once
279
- def whitelist(ctx):
280
- return ctx.message.author.id in my_whitelist
281
-
282
- """
283
- self .add_check (func , call_once = True )
284
- return func
285
-
286
176
async def can_run (self , ctx : Context , * , call_once : bool = False ) -> bool :
287
177
data = self ._check_once if call_once else self ._checks
288
178
@@ -292,109 +182,6 @@ async def can_run(self, ctx: Context, *, call_once: bool = False) -> bool:
292
182
# type-checker doesn't distinguish between functions and methods
293
183
return await discord .utils .async_all (f (ctx ) for f in data ) # type: ignore
294
184
295
- async def is_owner (self , user : discord .User ) -> bool :
296
- """|coro|
297
-
298
- Checks if a :class:`~discord.User` or :class:`~discord.Member` is the owner of
299
- this bot.
300
-
301
- If an :attr:`owner_id` is not set, it is fetched automatically
302
- through the use of :meth:`~.Bot.application_info`.
303
-
304
- .. versionchanged:: 1.3
305
- The function also checks if the application is team-owned if
306
- :attr:`owner_ids` is not set.
307
-
308
- Parameters
309
- -----------
310
- user: :class:`.abc.User`
311
- The user to check for.
312
-
313
- Returns
314
- --------
315
- :class:`bool`
316
- Whether the user is the owner.
317
- """
318
-
319
- if self .owner_id :
320
- return user .id == self .owner_id
321
- elif self .owner_ids :
322
- return user .id in self .owner_ids
323
- else :
324
-
325
- app = await self .application_info () # type: ignore
326
- if app .team :
327
- self .owner_ids = ids = {m .id for m in app .team .members }
328
- return user .id in ids
329
- else :
330
- self .owner_id = owner_id = app .owner .id
331
- return user .id == owner_id
332
-
333
- def before_invoke (self , coro : CFT ) -> CFT :
334
- """A decorator that registers a coroutine as a pre-invoke hook.
335
-
336
- A pre-invoke hook is called directly before the command is
337
- called. This makes it a useful function to set up database
338
- connections or any type of set up required.
339
-
340
- This pre-invoke hook takes a sole parameter, a :class:`.Context`.
341
-
342
- .. note::
343
-
344
- The :meth:`~.Bot.before_invoke` and :meth:`~.Bot.after_invoke` hooks are
345
- only called if all checks and argument parsing procedures pass
346
- without error. If any check or argument parsing procedures fail
347
- then the hooks are not called.
348
-
349
- Parameters
350
- -----------
351
- coro: :ref:`coroutine <coroutine>`
352
- The coroutine to register as the pre-invoke hook.
353
-
354
- Raises
355
- -------
356
- TypeError
357
- The coroutine passed is not actually a coroutine.
358
- """
359
- if not asyncio .iscoroutinefunction (coro ):
360
- raise TypeError ('The pre-invoke hook must be a coroutine.' )
361
-
362
- self ._before_invoke = coro
363
- return coro
364
-
365
- def after_invoke (self , coro : CFT ) -> CFT :
366
- r"""A decorator that registers a coroutine as a post-invoke hook.
367
-
368
- A post-invoke hook is called directly after the command is
369
- called. This makes it a useful function to clean-up database
370
- connections or any type of clean up required.
371
-
372
- This post-invoke hook takes a sole parameter, a :class:`.Context`.
373
-
374
- .. note::
375
-
376
- Similar to :meth:`~.Bot.before_invoke`\, this is not called unless
377
- checks and argument parsing procedures succeed. This hook is,
378
- however, **always** called regardless of the internal command
379
- callback raising an error (i.e. :exc:`.CommandInvokeError`\).
380
- This makes it ideal for clean-up scenarios.
381
-
382
- Parameters
383
- -----------
384
- coro: :ref:`coroutine <coroutine>`
385
- The coroutine to register as the post-invoke hook.
386
-
387
- Raises
388
- -------
389
- TypeError
390
- The coroutine passed is not actually a coroutine.
391
- """
392
- if not asyncio .iscoroutinefunction (coro ):
393
- raise TypeError ('The post-invoke hook must be a coroutine.' )
394
-
395
- self ._after_invoke = coro
396
- return coro
397
-
398
185
399
186
# cogs
400
187
0 commit comments