【发布时间】:2021-02-28 03:43:53
【问题描述】:
我正在尝试制作一个不和谐的机器人,当用户键入!goat 时,它会发送山羊的随机图像(位于文件夹中)。但我无法到达那里,也找不到像我这样的案例。
这是我的代码:
import discord
import random
import os
class MyClient(discord.Client):
async def on_ready(self):
print('Logged in as')
print(self.user.name)
print(self.user.id)
print('------')
async def on_message(self, message):
# we do not want the bot to reply to itself
if message.author.id == self.user.id:
return
if message.content.startswith('!goat'):
with open(random.choice(os.listdir("/home/leo/Bureau/bot-discord/image")), 'rb') as f:
picture = discord.File(f)
await channel.send(channel, picture)
感谢您的回答。
【问题讨论】:
-
listdir返回文件名,而不是完整路径。 -
哦,好吧,这是个问题,哈哈。你有什么我可以使用的方法吗?
-
@LéoCarrez 为什么不将路径存储在变量中,然后将路径和文件名连接起来?
-
看看
glob.glob。我认为这正是您正在寻找的。span> -
因为我的代码有效,他在我的文件夹中随机拍摄了一张图片,但他没有发送不和谐的图片,他返回如下错误:FileNotFoundError: [Errno 2] No such file or directory: '图片的名称。
标签: python python-3.x discord bots discord.py