如何在discord.py中发送一条临时消息?

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

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)

如何在discord.py中发送一条临时消息?


[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.

如何在discord.py中发送一条临时消息?

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)

huangapple
  • 本文由 发表于 2023年6月15日 18:21:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481529.html
匿名

发表评论

匿名网友

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

确定