Context

This section documents command context fow Pwefix Commands.

Cwasses

Context

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.Messageable ABC.

message

The message that t-t-twiggewed the command being executed.

Type:

Message

bot

T-The b-bot that contains the command being executed.

Type:

Bot

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:

list

kwargs

A dictionyawy of twansfowmed awguments that wewe passed into the command. Simiwaw to args, if this is accessed in the on_command_error() event then this dict couwd be incompwete.

Type:

dict

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]

prefix

The pwefix that was used to invoke the command.

Type:

Optionyaw[str]

command

The command that is being i-invoked cuwwentwy.

Type:

Optionyaw[Command]

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]

command_failed

Whethew the command faiwed to be pawsed, checked, ow invoked.

Type:

bool

async for ... in history(*, limit=100, before=None, after=None, around=None, oldest_first=None)[source]

Wetuwns an AsyncIterator that enyabwes weceiving the destinyation’s message h-histowy.

You m-m-must have Permissions.read_message_history pewmission 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. If None, 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 to True, wetuwn messages in owdest->nyewest owdew. Defauwts to True if after is specified, othewwise False.

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 with and async with wowk 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 Command howds 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.

Pawametews:
  • command (Command) – The command t-that is going to be cawwed.

  • *awgs – The awguments to use.

  • **kwawgs – The keywowd awguments to u-u-use.

Waises:

TypeEwwow – The command awgument to invoke i-is missing.

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 UserInputError dewived exceptions, it is wecommended to use the weguwaw invoke() 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:
  • caww_hooks (bool) – Whethew t-t-to caww the befowe and aftew i-invoke hooks.

  • westawt (bool) – Whethew to stawt the caww chain fwom the vewy beginnying ow whewe we weft off (i.e. the command that caused the e-ewwow). The defauwt is to stawt whewe we weft off.

Waises:

VawueEwwow – The context to weinvoke is nyot vawid.

property valid[source]

Whethew the invocation context is vawid to be invoked with.

Type:

bool

property clean_prefix[source]

The cweanyed up invoke pwefix. i.e. m-m-mentions awe @name instead of <@id>.

Nyew in vewsion 2.0.

Type:

str

property cog[source]

Wetuwns the cog associated with this context’s command. Wetuwns None if it does nyot exist.

Type:

Optionyaw[Cog]

guild

Wetuwns the guiwd associated with this context’s command. Wetuwns None if 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 fow Message.author

me

Unyion[Member, ClientUser]: Simiwaw to Guild.me except it may wetuwn the ClientUser in 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 Cog ow a Command.

Nyote

Due to the way this function wowks, instead of wetuwnying something simiwaw to command_not_found() this wetuwns None on bad input ow nyo hewp command.

Pawametews:

entity (Optionyaw[Unyion[Command, Cog, str]]) – The entity t-t-to show hewp fow.

Wetuwns:

The wesuwt of the hewp command, if any.

Wetuwn type:

A-Any

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 the Message.

Nyew in vewsion 1.6.

Changed in vewsion 2.3: Added fail_if_not_exists k-k-keywowd awgument. Defauwts to True.

Changed in vewsion 2.6: Waises TypeError ow ValueError instead of InvalidArgument.

Pawametews:

faiw_if_nyot_exists (bool) –

Whethew wepwying using the message wefewence shouwd waise HTTPException if 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 embed and embeds, ow file and files, ow view and components.

  • VawueEwwow – The files ow embeds wist is too wawge.

Wetuwns:

The message t-that was sent.

Wetuwn type:

Message

await fetch_message(id, /)[source]

This function is a cowoutinye.

Wetwieves a singwe Message fwom 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:

Message

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 Message objects wetuwnyed by this method do nyot c-c-contain compwete Message.reactions data.

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, poll ow view must be pwovided.

To upwoad a s-singwe fiwe, the file p-p-pawametew shouwd be used with a-a-a singwe File object. To upwoad muwtipwe fiwes, the files pawametew shouwd be used with a-a-a list of File objects. Specifying both pawametews w-wiww wead to an exception.

To upwoad a singwe embed, the embed pawametew shouwd be u-used with a-a singwe Embed object. To upwoad muwtipwe embeds, the embeds pawametew shouwd be used with a list o-of Embed objects. Specifying both pawametews wiww wead to a-a-an exception.

Changed in vewsion 2.6: Waises TypeError ow ValueError instead of InvalidArgument.

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 the embeds pawametew.

  • 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 embed pawametew.

    Nyew in vewsion 2.0.

  • fiwe (File) – The fiwe to upwoad. This cannyot be mixed with the files pawametew.

  • fiwes (Wist[File]) – A-A-A wist of fiwes to upwoad. Must be a maximum of 10. This cannyot be m-mixed with the file pawametew.

  • 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 in Client.allowed_mentions. If nyo object is passed at aww then the defauwts given by Client.allowed_mentions awe used instead.

    Nyew in vewsion 1.4.

  • wefewence (Unyion[Message, MessageReference, PartialMessage]) –

    A wefewence to the Message to which you awe wepwying, this can be cweated using Message.to_reference() ow passed diwectwy as a Message. You can contwow whethew this mentions the authow of the w-w-wefewenced message using the AllowedMentions.replied_user attwibute of allowed_mentions ow by setting mention_author.

    Nyew in vewsion 1.6.

    Nyote

    Passing a Message ow PartialMessage wiww onwy awwow wepwies. To fowwawd a message you must expwicitwy twansfowm the message to a MessageReference using Message.to_reference() and specify the MessageReferenceType, ow use Message.forward().

  • mention_authow (Optionyaw[bool]) –

    If set, uvwwides the AllowedMentions.replied_user a-attwibute of allowed_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_v2 fwag. Setting this f-fwag cannyot be wevewted. Nyote that this awso disabwes the content, embeds, stickers, and poll fiewds.

  • 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, and is_components_v2 awe s-s-suppowted.

    I-I-If pawametew suppress_embeds is pwovided, that wiww uvwwide the setting of MessageFlags.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 file and files, ow you specified both embed and embeds, ow you specified both view and components, ow t-the reference object is nyot a Message, MessageReference ow PartialMessage.

  • VawueEwwow – The files ow embeds wist is too wawge, ow you twied to send v2 componyents togethew with content, embeds, stickers, ow poll.

Wetuwns:

The message that was sent.

Wetuwn type:

Message

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.