首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Discord.py从机器人附加图像

Discord.py从机器人附加图像
EN

Stack Overflow用户
提问于 2019-12-18 01:24:30
回答 1查看 3.9K关注 0票数 2

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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')

我试着更改我的默认目录,但也不起作用。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-18 01:48:39

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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'))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59384296

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文