开发者社区> 问答> 正文

py从bot附加图像

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

进口不和谐 不和。ext导入命令

客户=命令。机器人(command_prefix =”! _”)

@client.event 异步def on_ready (): print(“僵尸网络”)

@client.command () 异步def (ctx): 等待ctx.send(将)

@client.event 异步def on_message(信息): 如果message.content.startswith (“! _will”): 嵌入=不和。(title="一张Will Smith的照片",description="Here he is", color=0x00ff00) 嵌入。add_field (name = " Field2”,价值=“hi2内联= False) embed.set_image (url = " https://discordapp.com/assets/e4923594e694a21542a489471ecffa50.svg ") 等待message.channel.send(嵌入=嵌入) 等待客户端。0 apw0j4 send_file(通道。”png”,内容= "…",文件名= "…")

@client.event 异步def on_message(信息): 如果message.content.startswith(“!威利”): 文件= discord.File (“0 apw0j4。png”,文件名= " 0 apw0j4.png”) 等待message.channel.send apw0j4 (“0。png”,文件=文件)

client.run('my bot token hidden') 我试着改变我的默认目录,但那也不工作。 问题来源StackOverflow 地址:/questions/59384296/discord-py-attach-image-from-bot

展开
收起
kun坤 2019-12-26 14:42:49 446 0
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'))
    
    2019-12-26 14:42:55
    赞同 1 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载