【问题标题】:Discord.py attach image from botDiscord.py 附加来自机器人的图像
【发布时间】:2019-12-18 21:38:45
【问题描述】:

我制作了一个机器人,它从我的下载文件夹中名为willy 的文件夹中随机发送一张威尔史密斯的照片。我没有实现随机部分,但我试图让机器人在名为0aPw0J4.pngwilly 文件夹中发送图像。我只希望脚本能够找到图像并上传它。目前的错误是即使我尝试列出从我的C: 驱动器开始的目录,python 也找不到该目录,但这没有用,所以我删除了它。

import discord
from discord.ext import commands



client = commands.Bot(command_prefix = "!_")


@client.event
async def on_ready():
    print('bots online')

@client.command()
async def will(ctx):
    await ctx.send('will')



@client.event
async def on_message(message):
    if message.content.startswith('!_will'):
        embed = discord.Embed(title="A photo of Will Smith", description="Here he is", color=0x00ff00)
        embed.add_field(name="Field2", value="hi2", inline=False)
        embed.set_image(url="https://discordapp.com/assets/e4923594e694a21542a489471ecffa50.svg")
        await message.channel.send(embed=embed)
        await client.send_file(channel, "0aPw0J4.png", content="...", filename="...")

@client.event
async def on_message(message):
    if message.content.startswith('!willy'):
        file = discord.File("0aPw0J4.png", filename="0aPw0J4.png")
        await message.channel.send("0aPw0J4.png", file=file)


client.run('my bot token hidden')

我尝试更改我的默认目录,但也没有用。

【问题讨论】:

  • 请分享完整的错误信息。
  • 你定义了两个同名的函数
  • @AlexanderCécile FileNotFoundError: [Errno 2] 没有这样的文件或目录:'0aPw0J4.png'
  • @aws_apprentice 是底部的两个client.events 吗?
  • 是的,您是否尝试过提供文件的完整路径?

标签: python discord.py


【解决方案1】:

可能是工作目录问题,在这种情况下,请始终尝试使用绝对路径引用文件。使用os模块查看是否可以找到包含图片的文件夹:

import os
# it will print all files in that folder
print(os.listdir('folder path of the images'))

# it will print True of False if the file exists with this path
print(os.path.isfile('absolute path of the image'))

【讨论】:

  • AttributeError: module 'ntpath' has no attribute 'listdir' 是我运行它时得到的
猜你喜欢
  • 2021-10-07
  • 2019-02-09
  • 2020-03-07
  • 2021-08-07
  • 2018-09-27
  • 2021-04-20
  • 2021-05-03
  • 2021-05-02
  • 1970-01-01
相关资源
最近更新 更多