diff options
| author | 2021-02-06 11:37:34 +0200 | |
|---|---|---|
| committer | 2021-02-06 11:37:34 +0200 | |
| commit | bbce0a8cb17d0771a4823e67675cf4dc26f72b2a (patch) | |
| tree | 5185e68249386905246384a195639fd0741c1e78 | |
| parent | Merge pull request #1397 from python-discord/feat/1280/deleted-jumplink (diff) | |
Create local-file tag about sending local files to Discord
| -rw-r--r-- | bot/resources/tags/local-file.md | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/bot/resources/tags/local-file.md b/bot/resources/tags/local-file.md new file mode 100644 index 000000000..309ca4820 --- /dev/null +++ b/bot/resources/tags/local-file.md @@ -0,0 +1,24 @@ +Thanks to discord.py, sending local files as embed images is simple. You have to create an instance of `discord.File` class: +```py +# When you know the file exact path, you can pass it. +file = discord.File("/this/is/path/to/my/file.png", filename="file.png") + +# When you have the file-like object, then you can pass this instead path. +with open("/this/is/path/to/my/file.png", "rb") as f: + file = discord.File(f) +``` +When using the file-like object, you have to open it in `rb` mode. Also, in this case, passing filename to it is not necessary. +Please note that `filename` can't contain underscores. This is Discord limitation. + +`discord.Embed` instance has method `set_image` what can be used to set attachment as image: +```py +embed = discord.Embed() +# Set other fields +embed.set_image("attachment://file.png") # Filename here must be exactly same as attachment filename. +``` +After this, you can send embed and attachment to Discord: +```py +await channel.send(file=file, embed=embed) +``` +This example uses `discord.Channel` for sending, but any `discord.Messageable` can be used for sending. + |