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

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

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

问题

  1. for some reason it seems that i cant send images with this
  2. ```python
  3. @commands.Cog.listener()
  4. async def on_message(self, message):
  5. if str(message.content).lower == "pic" :
  6. 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

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

so i tried this

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

it didn't work as i thought

  1. <details>
  2. <summary>英文:</summary>
  3. 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'))

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

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

  1. 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'))

  1. didn&#39;t work as i thoght
  2. </details>
  3. # 答案1
  4. **得分**: 0
  5. &gt; 首先,当检查消息内容的小写版本是否等于 "pic" 时,你需要调用 lower() 方法将其转换为小写。目前,你缺少了括号,所以比较不会正确工作。修改条件为 str(message.content).lower() == "pic"
  6. &gt; 其次,当发送带有文件附件的消息时,应该使用 await message.channel.send() 方法,而不是 await message.send()。调整你的代码以使用 await message.channel.send("text", file=discord.File('picture.png'))。
  7. **以下是你的代码的修复版本!:**

@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'))

  1. 尝试一下,然后告诉我结果。
  2. <details>
  3. <summary>英文:</summary>
  4. &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;.
  5. &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;)).
  6. **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'))

  1. try it and tell me.
  2. </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:

确定