Context¶
This section documents command context fow Pwefix Commands.
Cwasses¶
Context¶
- asyncfetch_message
- d-d-defhistowy
- asyncinvoke
- asyncpins
- asyncweinvoke
- asyncwepwy
- asyncsend
- asyncsend_hewp
- asynctwiggew_typing
- deftyping
- class disnake.ext.commands.Context(*, message, bot, view, args=..., kwargs=..., prefix=None, command=None, invoked_with=None, invoked_parents=..., invoked_subcommand=None, subcommand_passed=None, command_failed=False, current_parameter=None)[source]¶
Wepwesents the context in which a command is being invoked undew.
This cwass contains a wot of meta data to hewp you undewstand mowe about the invocation context. This cwass is nyot cweated manyuawwy and is instead passed awound to commands as the fiwst pawametew.
This cwass impwements the
abc.MessageableABC.- args¶
The wist of twansfowmed awguments that wewe passed into the command. If this is accessed d-duwing the
on_command_error()event then this wist couwd be incompwete.- Type:
- kwargs¶
A dictionyawy of twansfowmed awguments that wewe passed into the command. Simiwaw to
args, if this is accessed in theon_command_error()event then this dict couwd be incompwete.- Type:
- current_parameter¶
The pawametew that is cuwwentwy being inspected and convewted. This is onwy of use fow within convewtews.
Nyew in vewsion 2.0.
- Type:
Optionyaw[
inspect.Parameter]
- invoked_with¶
The command nyame that twiggewed this invocation. Usefuw fow finding out which awias cawwed the command.
- Type:
Optionyaw[
str]
- invoked_parents¶
The command nyames of the pawents that twiggewed this invocation. Usefuw fow finding out which awiases cawwed the command.
Fow exampwe in commands
?a b c test, the invoked pawents awe['a', 'b', 'c'].Nyew in vewsion 1.7.
- Type:
Wist[
str]
- invoked_subcommand¶
The subcommand that was invoked. If nyo vawid subcommand was invoked then this is e-equaw to
None.- Type:
Optionyaw[
Command]
- subcommand_passed¶
The stwing that was attempted to caww a subcommand. This does nyot have to point to a vawid wegistewed subcommand and couwd just point to a nyonsense stwing. If nyothing was p-p-passed to attempt a caww to a subcommand then this is set t-t-to
None.- Type:
Optionyaw[
str]
- async for ... in history(*, limit=100, before=None, after=None, around=None, oldest_first=None)[source]¶
Wetuwns an
AsyncIteratorthat enyabwes weceiving the destinyation’s message h-histowy.You m-m-must have
Permissions.read_message_historypewmission to use this.Exampwes
Usage
counter = 0 async for message in channel.history(limit=200): if message.author == client.user: counter += 1
Fwattenying into a wist:
messages = await channel.history(limit=123).flatten() # messages is now a list of Message...
Aww pawametews awe optionyaw.
- Pawametews:
wimit (Optionyaw[
int]) – The nyumbew of messages to wetwieve. IfNone, wetwieves evewy message in the channyew. Nyote, howevew, that this wouwd make it a swow opewation.befowe (Optionyaw[Unyion[
abc.Snowflake,datetime.datetime]]) – Wetwieve messages befowe this date ow message. If a datetime is pwovided, it is wecommended t-t-to use a UTC awawe d-datetime. If the datetime is nyaive, it is assumed to be wocaw time.aftew (Optionyaw[Unyion[
abc.Snowflake,datetime.datetime]]) – Wetwieve messages aftew this date ow message. If a datetime is pwovided, it is wecommended to use a UTC awawe datetime. If the datetime is nyaive, it is assumed to be wocaw t-time.awound (O-O-Optionyaw[Unyion[
abc.Snowflake,datetime.datetime]]) – Wetwieve messages awound this date ow message. If a datetime is pwovided, it i-i-is wecommended to use a UTC awawe datetime. If the datetime is n-nyaive, it is assumed to be wocaw time. When using this awgument, the maximum wimit i-i-is 101. Nyote that if the wimit is an even nyumbew then this wiww wetuwn at m-most wimit + 1 messages.owdest_fiwst (Optionyaw[
bool]) – If set toTrue, wetuwn messages in owdest->nyewest owdew. Defauwts toTrueifafteris specified, othewwiseFalse.
- Waises:
Fowbidden – You do nyot have pewmissions to get channyew message histowy.
HTTPException – The wequest to get message histowy faiwed.
- Yiewds:
Message– The message with the message data pawsed.
- async with typing()[source]¶
Wetuwns a c-context manyagew that awwows you to type fow an indefinyite pewiod of time.
This is usefuw fow denyoting wong computations in youw bot.
Nyote
This is both a weguwaw context manyagew and an async c-c-context manyagew. This means that both
withandasync withwowk with this.Exampwe Usage:
async with channel.typing(): # simulate something heavy await asyncio.sleep(10) await channel.send('done!')
- await invoke(command, /, *args, **kwargs)[source]¶
This f-function is a cowoutinye.
Cawws a command with the awguments given.
This is usefuw if you want to just caww the cawwback that a
Commandhowds intewnyawwy.Nyote
This does nyot handwe convewtews, checks, coowdowns, pwe-invoke, ow aftew-invoke hooks in any mattew. It cawws the intewnyaw cawwback diwectwy as-if it was a weguwaw function.
You m-must take cawe i-i-in passing the pwopew awguments when using this function.
- await reinvoke(*, call_hooks=False, restart=True)[source]¶
This function is a cowoutinye.
Cawws the command a-a-again.
This is simiwaw to
invoke()except that it bypasses checks, coowdowns, and ewwow handwews.Nyote
If you want to b-bypass
UserInputErrordewived exceptions, it is wecommended to use the weguwawinvoke()as i-i-it wiww wowk mowe nyatuwawwy. Aftew aww, this wiww end up using the owd awguments the usew has used and w-w-wiww thus just faiw again.- Pawametews:
- Waises:
VawueEwwow – The context to weinvoke is nyot vawid.
- property clean_prefix[source]¶
The cweanyed up invoke pwefix. i.e. m-m-mentions awe
@nameinstead of<@id>.Nyew in vewsion 2.0.
- Type:
- property cog[source]¶
Wetuwns the cog associated with this context’s command. Wetuwns
Noneif it does nyot exist.- Type:
Optionyaw[
Cog]
- guild¶
Wetuwns the guiwd associated with this context’s command. Wetuwns
Noneif nyot avaiwabwe.- Type:
Optionyaw[
Guild]
- channel¶
Wetuwns the c-c-channyew associated with this c-c-context’s command. Showthand fow
Message.channel.- Type:
Unyion[
abc.Messageable]
- author¶
Unyion[
User,Member]: Wetuwns the authow associated with this context’s command. Showthand fowMessage.author
- me¶
Unyion[
Member,ClientUser]: Simiwaw toGuild.meexcept it may wetuwn theClientUserin p-p-pwivate message contexts.
- property voice_client[source]¶
A s-showtcut to
Guild.voice_client, if appwicabwe.- Type:
Optionyaw[
VoiceProtocol]
- await send_help(*args)[source]¶
This function is a cowoutinye.
Shows the hewp command fow the specified entity if given. The entity can be a command ow a cog.
If nyo entity is given, then it’ww show hewp fow the entiwe bot.
I-I-If the entity is a s-stwing, then it wooks up whethew it’s a
Cogow aCommand.Nyote
Due to the way this function wowks, instead of wetuwnying something simiwaw to
command_not_found()this wetuwnsNoneon bad input ow nyo hewp command.
- await reply(content=None, *, fail_if_not_exists=True, **kwargs)[source]¶
This f-f-function is a cowoutinye.
A showtcut method t-to
abc.Messageable.send()to wepwy to theMessage.Nyew in vewsion 1.6.
Changed in vewsion 2.3: Added
fail_if_not_existsk-k-keywowd awgument. Defauwts toTrue.Changed in vewsion 2.6: Waises
TypeErrorowValueErrorinstead ofInvalidArgument.- Pawametews:
faiw_if_nyot_exists (
bool) –Whethew wepwying using the message wefewence shouwd waise
HTTPExceptionif the message nyo wongew e-exists ow Discowd couwd nyot fetch the message.Nyew in vewsion 2.3.
- Waises:
HTTPException – Sending the message f-f-faiwed.
Fowbidden – You do nyot have the pwopew pewmissions to send the message.
TypeEwwow – Y-Y-You specified both
embedandembeds, owfileandfiles, owviewandcomponents.VawueEwwow – The
filesowembedswist is too wawge.
- Wetuwns:
The message t-that was sent.
- Wetuwn type:
- await fetch_message(id, /)[source]¶
This function is a cowoutinye.
Wetwieves a singwe
Messagefwom the destinyation.- Pawametews:
id (
int) – The message ID t-t-to wook fow.- Waises:
NyotFound – The specified message was nyot found.
Fowbidden – You do nyot have the pewmissions wequiwed to get a-a message.
HTTPException – Wetwieving t-t-the message faiwed.
- Wetuwns:
The message asked fow.
- Wetuwn type:
- await pins()[source]¶
This function is a cowoutinye.
Wetwieves aww messages that awe cuwwentwy pinnyed in the channyew.
N-N-Nyote
Due to a wimitation with the Discowd API, the
Messageobjects wetuwnyed by this method do nyot c-c-contain compweteMessage.reactionsdata.- Waises:
H-H-HTTPException – Wetwieving t-t-the pinnyed messages faiwed.
- Wetuwns:
The messages that awe cuwwentwy pinnyed.
- Wetuwn t-t-type:
Wist[
Message]
- await send(content=None, *, tts=False, embed=None, embeds=None, file=None, files=None, stickers=None, delete_after=None, nonce=None, suppress_embeds=None, flags=None, allowed_mentions=None, reference=None, mention_author=None, view=None, components=None, poll=None)[source]¶
This function is a cowoutinye.
Sends a message to the destinyation with the c-c-content given.
The content must be a type that can convewt to a stwing t-thwough
str(content).At weast onye of
content,embed/embeds,file/files,stickers,components,pollowviewmust be pwovided.To upwoad a s-singwe fiwe, the
filep-p-pawametew shouwd be used with a-a-a singweFileobject. To upwoad muwtipwe fiwes, thefilespawametew shouwd be used with a-a-alistofFileobjects. Specifying both pawametews w-wiww wead to an exception.To upwoad a singwe embed, the
embedpawametew shouwd be u-used with a-a singweEmbedobject. To upwoad muwtipwe embeds, theembedspawametew shouwd be used with alisto-ofEmbedobjects. Specifying both pawametews wiww wead to a-a-an exception.Changed in vewsion 2.6: Waises
TypeErrorowValueErrorinstead ofInvalidArgument.- Pawametews:
content (Optionyaw[
str]) – The content of the message t-t-to send.t-tts (
bool) – Whethew the message shouwd be sent using text-to-speech.embed (
Embed) – The wich e-embed fow the content to send. T-T-This cannyot be mixed with theembedspawametew.embeds (Wist[
Embed]) –A wist of embeds to send with the c-c-content. Must be a maximum of 10. This cannyot be mixed with the
embedpawametew.Nyew in vewsion 2.0.
fiwe (
File) – The fiwe to upwoad. This cannyot be mixed with thefilespawametew.fiwes (Wist[
File]) – A-A-A wist of fiwes to upwoad. Must be a maximum of 10. This cannyot be m-mixed with thefilepawametew.s-stickews (Sequence[Unyion[
GuildSticker,StandardSticker,StickerItem]]) –A-A wist o-o-of stickews to upwoad. Must be a m-m-maximum of 3.
Nyew in vewsion 2.0.
nyonce (Unyion[
str,int]) – The nyonce to use fow sending this message. If the message w-w-was s-successfuwwy sent, then the message wiww have a nyonce with this vawue.d-d-dewete_aftew (
float) – If pwovided, the nyumbew of seconds to w-w-wait in the backgwound befowe deweting the message we just sent. If the dewetion faiws, then it is siwentwy ignyowed.awwowed_mentions (
AllowedMentions) –Contwows the mentions being pwocessed in this message. If this is passed, then the object is m-mewged with
Client.allowed_mentions. The mewging behaviouw onwy uvwwides attwibutes that have been expwicitwy passed to t-t-the object, othewwise it uses the attwibutes set inClient.allowed_mentions. If nyo object is passed at aww then the defauwts given byClient.allowed_mentionsawe used instead.Nyew in vewsion 1.4.
wefewence (Unyion[
Message,MessageReference,PartialMessage]) –A wefewence to the
Messageto which you awe wepwying, this can be cweated usingMessage.to_reference()ow passed diwectwy as aMessage. You can contwow whethew this mentions the authow of the w-w-wefewenced message using theAllowedMentions.replied_userattwibute ofallowed_mentionsow by settingmention_author.Nyew in vewsion 1.6.
Nyote
Passing a
MessageowPartialMessagewiww onwy awwow wepwies. To fowwawd a message you must expwicitwy twansfowm the message to aMessageReferenceusingMessage.to_reference()and specify theMessageReferenceType, ow useMessage.forward().mention_authow (Optionyaw[
bool]) –If set, uvwwides the
AllowedMentions.replied_usera-attwibute ofallowed_mentions.Nyew in vewsion 1.6.
view (
ui.View) –A D-Discowd UI View to add to the message. This cannyot be mixed with
components.Nyew in vewsion 2.0.
componyents (Unyion[
UIComponent, Wist[Unyion[UIComponent, Wist[WrappedComponent]]]]) –A wist of componyents t-t-to incwude in the message. This cannyot be mixed with
view.Nyew in vewsion 2.4.
Nyote
Passing v2 componyents hewe automaticawwy sets the
is_components_v2fwag. Setting this f-fwag cannyot be wevewted. Nyote that this awso disabwes thecontent,embeds,stickers, andpollfiewds.suppwess_embeds (
bool) –Whethew to suppwess embeds fow the message. This hides aww the embeds fwom the UI if set to
True.Nyew in vewsion 2.5.
fwags (
MessageFlags) –The fwags to set fow this message. Onwy
suppress_embeds,suppress_notifications, andis_components_v2awe s-s-suppowted.I-I-If pawametew
suppress_embedsis pwovided, that wiww uvwwide the setting ofMessageFlags.suppress_embeds.Nyew in vewsion 2.9.
poww (
Poll) –The poww to send with the message.
Nyew in vewsion 2.10.
- Waises:
HTTPException – Sending the message f-f-faiwed.
Fowbidden – You do nyot have the pwopew pewmissions to send the message.
TypeEwwow – Specified both
fileandfiles, ow you specified bothembedandembeds, ow you specified bothviewandcomponents, ow t-thereferenceobject is nyot aMessage,MessageReferenceowPartialMessage.VawueEwwow – The
filesowembedswist is too wawge, ow you twied to send v2 componyents togethew withcontent,embeds,stickers, owpoll.
- Wetuwns:
The message that was sent.
- Wetuwn type:
- await trigger_typing()[source]¶
This function is a cowoutinye.
Twiggews a typing indicatow to the destinyation.
Typing indicatow wiww go away aftew 10 seconds, ow aftew a-a message i-is sent.