【问题标题】:AttributeError: 'NoneType' object has no attribute 'edit'AttributeError: \'NoneType\' 对象没有属性 \'edit\'
【发布时间】:2022-11-18 23:37:05
【问题描述】:

我使用 python 为我的 discord 机器人创建了一个简单的斜杠命令,以显示机器人延迟。但是,它似乎没有用。你能帮我吗?非常感谢:D。

我的代码:

import time
import discord
from discord import app_commands
from discord.ext import commands


intents = discord.Intents.default()
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)

prefix = "-"
bot = commands.Bot(command_prefix=prefix,intents=intents)
 

@client.event
async def on_ready():
    await tree.sync(guild=discord.Object(id=ID))
    print("Ready!")

 
@tree.command(name = "ping", description = "test command", guild=discord.Object(id=ID)) 
async def ping(Interaction):
    before = time.monotonic()
    message = await Interaction.response.send_message("pong")
    ping = (time.monotonic() - before) * 1000
    await message.edit(content=f"pong `{int(ping)}ms`") <-- here's the line that's causing the problem..  

client.run('TOKEN')

我一直试图在互联网上找到这个问题,但一直找不到。丁: 我试图以某种方式自己修复它,但我是使用 python 编码的新手,但没有成功。

【问题讨论】:

  • 不要自动同步,也不要在 on_ready 中调用 API。为什么你有一个你没有使用的ClientBot?你遇到了什么错误?

标签: python discord discord.py


【解决方案1】:

你不能像message = await Interaction.response.send_message("pong")那样存储交互并更新它。 您可以使用edit_original_message()编辑交互

@tree.command(name = "ping", description = "test command", guild=discord.Object(id=ID)) 
async def ping(Interaction):
    before = time.monotonic()
    await Interaction.response.send_message("pong")
    ping = (time.monotonic() - before) * 1000
    await Interaction.edit_original_message(content=f"pong `{int(ping)}ms`")

【讨论】:

    猜你喜欢
    • 2022-12-15
    • 1970-01-01
    • 2019-01-01
    • 2021-12-26
    • 2019-07-23
    • 2018-05-13
    • 2020-09-07
    • 2017-05-03
    • 2023-03-16
    相关资源
    最近更新 更多