无法将附件发送到Discord频道



我无法将文件(由机器人程序创建并由该机器人程序存储在服务器上的目录中(作为附件发送给用户。这是我的代码:

import discord, os
from discord.ext import commands
bot = commands.Bot(command_prefix = '!')
file = 'processed_logs.txt'
some_directory = 'some_directory'
@bot.command()
async def send_file(ctx):
with open(f"{os.getcwd}/Cache/{some_directory}/{file}", 'rb') as attachment: 
await ctx.send(f"{file}", file=discord.File(attachment))
bot.run(bot_token)

我哪里错了?

discord.File((获取文件的路径。您不需要使用open((函数。

我最终发现了哪里出了问题,文件描述在文件路径或文件对象之后,而不是之前!所以答案是:

import discord, os
from discord import File
from discord.ext import commands
bot = commands.Bot(command_prefix = '!')
file = 'processed_logs.txt'
some_directory = 'some_directory'
@bot.command()
async def send_file(ctx):
with open(f"{os.getcwd}/Cache/{some_directory}/{file}", 'rb') as attachment: 
await ctx.send(file=File(attachment, f"{file}"))
bot.run(bot_token)

另一个解决方案(如@KickBull所述(:

import discord, os
from discord import File
from discord.ext import commands
bot = commands.Bot(command_prefix = '!')
file = 'processed_logs.txt'
some_directory = 'some_directory'
@bot.command()
async def send_file(ctx):
await ctx.send(file=File(f"{os.getcwd}/Cache/{some_directory}/{file}", f"{file}"))
bot.run(bot_token)

最新更新