无法在 Discord 机器人的功能中发送图片。

huangapple go评论69阅读模式
英文:

can't use send images in functions in discord bot in cogs

问题

for some reason it seems that i cant send images with this

```python
@commands.Cog.listener()
async def on_message(self, message):
     if str(message.content).lower == "pic" :
         await message.send("text", file=discord.File('picture.png'))

i thought the problem was in the function on_message itself
after i saw the documentation i figured out that i can send it only with

channel.send(file=discord.file('pic.png'))

so i tried this

@commands.Cog.listener()
async def on_message(self, message):
     channel = self.client.get_channel("channel id")
     if str(message.content).lower == "pic" :
         await channel.send("text", file=discord.File('picture.png'))

it didn't work as i thought


<details>
<summary>英文:</summary>

for some reason it seems that i cant send images with this

@commands.Cog.listener()
async def on_message(self, message):
if str(message.content).lower == "pic" :
await message.send("text", file=discord.File('picture.png'))



i thougth the problem was in the function on_message itself
after i saw the documenttion i figured out that i can send it only with

channel.send(file=discord.file('pic.png'))

so i tried this

@commands.Cog.listener()
async def on_message(self, message):
channel = self.client.get_channel("channel id")
if str(message.content).lower == "pic" :
await channel.send("text", file=discord.File('picture.png'))

didn&#39;t work as i thoght


</details>


# 答案1
**得分**: 0

&gt; 首先,当检查消息内容的小写版本是否等于 "pic" 时,你需要调用 lower() 方法将其转换为小写。目前,你缺少了括号,所以比较不会正确工作。修改条件为 str(message.content).lower() == "pic"。

&gt; 其次,当发送带有文件附件的消息时,应该使用 await message.channel.send() 方法,而不是 await message.send()。调整你的代码以使用 await message.channel.send("text", file=discord.File('picture.png'))。

**以下是你的代码的修复版本!:**

@commands.Cog.listener()
async def on_message(self, message):
if str(message.content).lower() == "pic":
await message.channel.send("text", file=discord.File('picture.png'))


尝试一下,然后告诉我结果。

<details>
<summary>英文:</summary>

&gt; First, when checking if the lowercase version of the message content is equal to &quot;pic,&quot; you need to call the lower() method to convert it to lowercase. Currently, you&#39;re missing the parentheses, so the comparison won&#39;t work correctly. Modify the condition to str(message.content).lower() == &quot;pic&quot;.

&gt; Second, when sending a message with a file attachment, you should use the await message.channel.send() method instead of await message.send(). Adjust your code to use await message.channel.send(&quot;text&quot;, file=discord.File(&#39;picture.png&#39;)).

**Here is the Fixed version of your code !:**

@commands.Cog.listener()
async def on_message(self, message):
if str(message.content).lower() == "pic":
await message.channel.send("text", file=discord.File('picture.png'))


try it and tell me.


</details>



huangapple
  • 本文由 发表于 2023年7月17日 23:02:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定