我刚刚弄清楚如何使用我的 Discord 机器人运行斜杠命令。但是,我希望机器人以只有用户可以看到消息的方式回复用户(如下示例)。
我进行了广泛的搜索,但找不到任何有关此的文档。有人可以帮我吗?我的代码附在下面:
import discord
from discord.ext import commands
from datetime import datetime
# Define the ping command
@commands.hybrid_command(name='ping', description='Fetch the bots ping')
async def ping(ctx):
# Send "Calculating ping..." as part of the embed
embed = discord.Embed(title="Calculating ping...", color=discord.Color.blue())
msg = await ctx.send(embed=embed)
# Simulate a delay (you can adjust the delay time as needed)
await asyncio.sleep(1) # Wait for 1 second
# Calculate latency
latency = round(ctx.bot.latency * 1000) # Convert latency to milliseconds
# Update the embed to display the latency
embed.title = "Pong!"
embed.description = f'Latency: {latency}ms'
embed.color = discord.Color.green()
# Add a footer with the text "Internal Audio" and a timestamp with the current system time
embed.set_footer(text="Internal Audio")
embed.timestamp = datetime.now() # Use datetime.now() to get the current system time
# Edit the message to display the updated embed
await msg.edit(embed=embed)
您可以通过添加参数来“忽略”消息:
ephemeral = True
因此,当您发送消息时,您的代码应该如下所示:
msg = await ctx.send(embed=embed, ephemeral = True)
遗憾的是,我不能 100% 确定这是否适用于混合命令,但它确实适用于 app_commands 库中的交互。
希望能帮到你!如果它不起作用,那么遗憾的是,混合命令是不可能的,您需要使用 app_command 库(我更喜欢)来创建斜杠命令。