英文:
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't work as i thoght
</details>
# 答案1
**得分**: 0
> 首先,当检查消息内容的小写版本是否等于 "pic" 时,你需要调用 lower() 方法将其转换为小写。目前,你缺少了括号,所以比较不会正确工作。修改条件为 str(message.content).lower() == "pic"。
> 其次,当发送带有文件附件的消息时,应该使用 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>
> First, when checking if the lowercase version of the message content is equal to "pic," you need to call the lower() method to convert it to lowercase. Currently, you're missing the parentheses, so the comparison won't work correctly. Modify the condition to str(message.content).lower() == "pic".
> 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("text", file=discord.File('picture.png')).
**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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论