在 Discord.py 中获取消息内容

问题描述 投票:0回答:1

我一直在尝试在 Discord.py 中获取消息的内容,以便我可以将其显示在我的机器人发回的消息的底部。我在网上看到的每个例子都是检查某些短语来触发命令,但不打印回消息。

import discord
from discord.ext import commands

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

@bot.event
async def on_ready():
  print("The bot is now running.")

@bot.command()
async def copycat(ctx):
  embed = discord.Embed(title="Copy Cat", description=message.content, color=0x3cc13c)
  await ctx.send(embed=embed)

bot.run(TOKEN)
discord.py
1个回答
0
投票

要获取消息内容,您可以使用

ctx.message.content
,然后将其放在您需要的任何地方。将嵌入代码中的
description=message.content
替换为
description=ctx.message.content

另一种解决方案是使用 @tree.command 与斜杠命令一起使用

Interactions
(这涉及使用客户端而不是机器人):

import discord
from discord import app_commands

# Define client and tree
client = discord.Client(intents=discord.Intents.all())
tree = app_commands.CommandTree(client)

# Define your command
@tree.command(
    name="commandname", # Enter your command name here, it will turn out to be /<name>
    description="My first application Command", # What shows up in the dialog
)
async def first_command(interaction):
    await interaction.response.send_message("Hello!") # You can use f-strings here
    # And to get the message content...
    await interaction.followup.send_message(f"{interaction.message.content}") # Use followup to send other messages on top

# And then sync it in on_ready:
@client.event
async def on_ready():
    await tree.sync()
    # other code here

要创建更多命令,只需将

@tree.command
与这些参数一起使用,然后使用具有相同结构的
async def

希望这有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.