diff options
author | 2021-08-27 07:39:26 +0530 | |
---|---|---|
committer | 2021-08-27 02:09:26 +0000 | |
commit | b0ea616add8f6122ba5b98b348a4fa5d41774d21 (patch) | |
tree | 481209d67ffe5c5b31abbe324a95ea76a20125b9 | |
parent | Merge pull request #1778 from python-discord/update-remind-help (diff) |
Added bot variables tag (#1784)
* Added a new tag with the name bot_var
Co-authored-by: Bluenix <[email protected]>
-rw-r--r-- | bot/resources/tags/bot_var.md | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/bot/resources/tags/bot_var.md b/bot/resources/tags/bot_var.md new file mode 100644 index 000000000..6833b3cd8 --- /dev/null +++ b/bot/resources/tags/bot_var.md @@ -0,0 +1,23 @@ +Python allows you to set custom attributes to class instances, like your bot! By adding variables as attributes to your bot you can access them anywhere you access your bot. In the discord.py library, these custom attributes are commonly known as "bot variables" and can be a lifesaver if your bot is divided into many different files. An example on how to use custom attributes on your bot is shown below: + +```py +bot = commands.Bot(command_prefix="!") +# Set an attribute on our bot +bot.test = "I am accessible everywhere!" + +async def get(ctx: commands.Context): + """A command to get the current value of `test`.""" + # Send what the test attribute is currently set to + await ctx.send(ctx.bot.test) + +async def setval(ctx: commands.Context, *, new_text: str): + """A command to set a new value of `test`.""" + # Here we change the attribute to what was specified in new_text + bot.test = new_text +``` + +This all applies to cogs as well! You can set attributes to `self` as you wish. + +*Be sure **not** to overwrite attributes discord.py uses, like `cogs` or `users`. Name your attributes carefully!* |