简体   繁体   English

如何编辑discord.py中的消息

[英]How to edit a message in discord.py

I would like to have my bot edit a message if it detects a keyword, i'm not sure how to edit the message though.我想让我的机器人在检测到关键字时编辑消息,但我不确定如何编辑消息。

I've looked through the documentation but can't seem to figure it out.我查看了文档,但似乎无法弄清楚。 I'm using discord.py with python 3.6.我正在使用 discord.py 和 python 3.6。

This is the code:这是代码:

@bot.event
async def on_message(message):
    if 'test' in message.content:
        await edit(message, "testtest")

This is the error:这是错误:

  File "testthing.py", line 67, in on_message
    await edit(message, "test")
 NameError: name 'edit' is not defined

I would like the bot to edit a message to "testtest" if the message contains the word test, but i just get an error.如果消息包含单词测试,我希望机器人将消息编辑为“testtest”,但我只是收到一个错误。

You can use the Message.edit coroutine.您可以使用Message.edit协程。 The arguments must be passed as keyword arguments content , embed , or delete_after .参数必须作为关键字参数contentembeddelete_after传递。 You may only edit messages that you have sent.您只能编辑已发送的消息。

await message.edit(content="newcontent")

Here's a solution that worked for me.这是一个对我有用的解决方案。

@client.command()
async def test(ctx):
  message = await ctx.send("hello")
  await asyncio.sleep(1)
  await message.edit(content="newcontent")

Please try to add def to your code like this:请尝试将def添加到您的代码中,如下所示:

@bot.event
async def on_message(message):
    if 'test' in message.content:
        await edit(message, "edited !")

Did you do this:你做了这个了吗:

from discord import edit

or this:或这个:

from discord import *

before using message.edit function?在使用 message.edit 功能之前?

If you did it, maybe the problem is with your discord.py version.如果你这样做了,那么问题可能出在你的 discord.py 版本上。 Try this:尝试这个:

print(discord.__version__)

If you are wanting to update responses in discord.py you have to use:如果您想更新 discord.py 中的回复,您必须使用:

@tree.command(name = 'foobar', description = 'Send the word foo and update it to say bar')
async def self(interaction: discord.Interaction):
    await interaction.response.send_message(f'foo', ephemeral = True)
    time.sleep(1)
    await interaction.edit_original_response(content=f'bar')

Assign the original message to a variable.将原始消息分配给一个变量。 Reference the variable with .edit(content='content') .使用.edit(content='content')引用变量。

(You need " content= " in there). (你需要“ content= ”)。

@bot.command()
async def test(ctx):
    msg = await ctx.send('test')
    await msg.edit(content='this message has been edited')
@bot.event
async def on_message(message):
  if message.content == 'test':
    messages=await message.channel.send("CONTENT")
    await asyncio.sleep(INT)
    await messages.edit(content="NEW CONTENT")

This is what I did:这就是我所做的:

@bot.event
async def on_message(message):
  if message.content == 'test':
    await message.channel.send('Hello World!')
    await message.edit(content='testtest')

I don't know if this will work for you, but try and see.我不知道这是否适合你,但试试看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM