如何使用 discord.py 发送文件?

huangapple go评论87阅读模式
英文:

How to send files using discord.py?

问题

我如何使用discord.py发送文件?这是我的代码:

import discord
from discord.ext import commands
import random

TOKEN = "" # 我的令牌

bot = commands.Bot(command_prefix = "/", intents = discord.Intents.all())

@bot.event
async def on_ready():
    print("机器人已启动并准备就绪!")
    try:
        synced = await bot.tree.sync()
        print(f"同步了 {len(synced)} 条命令!")
    except Exception as e:
        print(e)

@bot.tree.command(name = "roll", description = "掷骰子!")
async def roll(interaction: discord.Interaction):
    await interaction.response.send_message(f"`{random.randint(1, 6)}`")

到目前为止,我只做了一个简单的掷骰子命令。例如,如果我在根目录中有一个名为song.mp3的文件,并且我想在斜杠命令中上传(发送)它,我该如何做?我想要类似这样的效果:

@bot.tree.command(name = "song", description = "发送歌曲")
async def song(interaction: discord.Interaction):
    await interaction.response.send_message(f"{the_song_file}")

我希望实现的效果类似于你将文件拖放到discord聊天中发送,但我希望我的机器人能够完成这个任务。

英文:

How do I send files using discord.py? Here is my code:

import discord
from discord import app_commands
from discord.ext import commands
import random

TOKEN = "" # my token

bot = commands.Bot(command_prefix = "/", intents = discord.Intents.all())

@bot.event
async def on_ready():
    print("Bot is Up and Ready!")
    try:
        synced = await bot.tree.sync()
        print(f"Synced {len(synced)} command(s)!")
    except Exception as e:
        print(e)

@bot.tree.command(name = "roll", description = "roll a dice!")
async def roll(interaction: discord.Interaction):
    await interaction.response.send_message(f"`{random.randint(1, 6)}`")

so far I only made a simple command that rolls a dice. If for example, I have a file named song.mp3 in my root directory, and I want to upload (send it) in a slash command, how do I do that? I want something like this:


@bot.tree.command(name = "song", description = "send the song")
async def song(interaction: discord.Interaction):
    await interaction.response.send_message(f"{the_song_file}")

I want to like how when you drag and drop a file into discord chat to send it, but I want my bot to do it.

答案1

得分: 1

你使用 send_message 方法是正确的轨迹。相同的方法在给定文件时可以发送文件:查看文档,我们看到有一个 file 参数,我们可以使用它:

@bot.tree.command(name="song", description="send the song")
async def song(interaction: discord.Interaction):
    await interaction.response.send_message(file=discord.File(file_to_send))

请注意,在使用 discord.File 发送文件之前,你必须打开文件,而不是发送文件的路径。

英文:

You're on the right track using the send_message method.
The same method can send a file when given one:
Looking through the documentation, we see a file parameter, which we can use:

@bot.tree.command(name = "song", description = "send the song")
async def song(interaction: discord.Interaction):
    await interaction.response.send_message(file=discord.File(file_to_send))

Note that you have to open the file before sending it using discord.File, instead of sending the file's path.

huangapple
  • 本文由 发表于 2023年5月7日 11:37:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定