英文:
How can i send an ephemeral message in discord.py?
问题
我想要一个机器人在discord.py中对名为/help
的自定义命令作出瞬时响应,这样当我在我的discord服务器中发送/help
时,用户发送的消息会被删除,机器人会用一条瞬时消息做出响应。
我已经尝试过使用ctx和discord.InteractionResponse来代替discord.Interaction,但会出现错误,如下所示:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Context' object has no attribute 'response'
以下是代码:
import discord
from discord.ext import commands
TOKEN = 'my_token'
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='/', intents=intents)
help_text = 'This is a help text'
@bot.event
async def on_ready():
print(f"{bot.user} is now up and running.")
bot.remove_command('help')
@bot.command()
async def help(ctx, interaction: discord.Interaction):
await ctx.channel.purge(limit=1) # 移除用户的消息
# await ctx.send(help_text)
# interaction = discord.InteractionResponse
await interaction.response.send_message(content="Hello", ephemeral=True) # 应该发送一条瞬时消息
bot.run(TOKEN)
英文:
I want a bot to respond with an ephemeral message to a custom command called /help
in discord.py. So that when i send /help
in my discord server, the message that the user sent gets removed and the bot responds with an ephemeral message.
I have tried to use ctx and discord.InteractionResponse instead of discord.Interaction but it will just return with an error, like:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Context' object has no attribute 'response'
.
And here is the code:
import discord
from discord.ext import commands
TOKEN = 'my_token'
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='/', intents=intents)
help_text = 'This is a help text'
@bot.event
async def on_ready():
print(f"{bot.user} is now up and running.")
bot.remove_command('help')
@bot.command()
async def help(ctx, interaction: discord.Interaction):
await ctx.channel.purge(limit=1) # Remove the user message
# await ctx.send(help_text)
# interaction = discord.InteractionResponse
await interaction.response.send_message(content="Hello", ephemeral=True) # Should send a ephemeral message
bot.run(TOKEN)
答案1
得分: 0
你正在使用普通命令而不是交互命令。你可以将基于交互的东西视为预定义命令,你要通知 Discord。每当创建新命令时,你都必须进行同步。
在频道上调用清除命令以删除一条消息并不是一个好主意。如果有人在命令和响应之间发送消息,它可能会误删正常的消息。
import discord
from discord import app_commands
TOKEN = 'TOKEN'
intents = discord.Intents.default()
intents.message_content = True
bot = discord.Client(intents=intents)
tree = app_commands.CommandTree(bot)
@tree.command(name="help", description="A basic help command.")
async def help(interaction):
# 由于现在有交互,我们可以将消息发送为临时消息。
# 我们也不必处理删除命令消息的问题。
await interaction.response.send_message(content="Help command.", ephemeral=True)
@bot.event
async def on_ready():
# 将命令树上的命令与 Discord 同步。
await tree.sync()
print(f"{bot.user} is now up and running.")
bot.run(TOKEN)
[1]: https://i.stack.imgur.com/D0rmP.png
英文:
You are using a normal commands rather than an interaction. You can think of interaction based stuff as predefined commands you notify discord about. Whenever a new command is created, you have to sync it.
Calling purge on a channel to remove one message isn't a good idea. If anyone sends a message between the command and response, it might end up deleting the normal message instead.
import discord
from discord import app_commands
TOKEN = 'TOKEN'
intents = discord.Intents.default()
intents.message_content = True
bot = discord.Client(intents=intents)
tree=app_commands.CommandTree(bot)
@tree.command(name="help", description="A basic help command.")
async def help(interaction):
# Since now we have interaction we can send the message as an ephemeral message.
# We also dont have to deal with deleting the command message.
await interaction.response.send_message(content="Help command.", ephemeral=True)
@bot.event
async def on_ready():
# Sync commands on command tree with discord.
await tree.sync()
print(f"{bot.user} is now up and running.")
bot.run(TOKEN)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论