我制作了一个机器人,从我的下载文件夹中的一个名为willy
的文件夹中随机发送一张威尔·史密斯的照片。我没有实现随机部分,但我正在尝试让机器人发送一个名为0aPw0J4.png
的willy
文件夹中的图像。我只想让脚本能够找到图像并上传它。目前的错误是python无法找到目录,即使我试图列出目录从我的C:
驱动器开始,但没有工作,所以我删除了它。
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')
我试着更改我的默认目录,但也不起作用。
发布于 2019-12-18 01:48:39
这可能是工作目录的问题,在这种情况下,请始终尝试使用绝对路径引用文件。使用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'))
https://stackoverflow.com/questions/59384296
复制相似问题