iT邦幫忙

0

Discord.py 沒辦法編輯回復,是少了什麼嗎?(版本2.3.2)

  • 分享至 

  • xImage

事情如題
代碼是長這樣的

commandname = (f'{prefix}name')
@bot.tree.command(name = commandname, description = 'description')
async def name(interaction:discord.Integration):  
    await interaction.response.send_message("123")
    print("1")
    await asyncio.sleep(1)
    print("2")
    await interaction.response.edit_message(content="1234")
    print("3")

實際執行的話會先在DC回復123
像這樣
https://ithelp.ithome.com.tw/upload/images/20240426/20145811wZj9EOIxxC.png
然後終端機打印"1"
等待1秒
終端機打印"2"
在然後就不會執行了


雖然也是可以用底下代碼的方式

commandname = (f'{prefix}name')
@bot.tree.command(name = commandname, description = 'description')
async def name(interaction:discord.Integration):  
    msg = await interaction.channel.send("123")
    print("1")
    await asyncio.sleep(1)
    print("2")
    await msg.edit(content="1234")
    print("3")

而實際執行起來是先在DC回復123
跟剛剛一樣就不放圖了
然後一樣終端機打印"1"
等待1秒
終端機打印"2"
再然後也確實會把內容改成1234
像這樣
https://ithelp.ithome.com.tw/upload/images/20240426/20145811vz6BcUM55i.png
最後終端機打印"3"
結束

可是有個很大的問題
因為沒有用到交互功能所以DC還會有失敗的消息
像這樣
https://ithelp.ithome.com.tw/upload/images/20240426/20145811IO2LNMoYoL.png

那請問是哪裡寫的不對嗎?
可我去參考其他機器人代碼時也看起來是跟我的一樣
官方API也是這寫法
甚至就連GPT也是

實在是有點奇怪
那完整代碼我放底下了
希望有哪個大什可以糾正糾正我
謝謝

import discord
from discord.ext import commands
intents = discord.Intents.all()
intents.typing = False
intents.presences = False
import json
with open('setting.json','r',encoding='utf8') as setting_file:
    setting = json.load(setting_file)
bot = commands.Bot(command_prefix =[f'{setting["prefix"]}-','/'],intents = intents)

import asyncio,keep_alive,os

prefix = 'a-'
#機器人登陸通知
@bot.event
async def on_ready():
    print('>>','嗨嗨嗨!! {0.user}已經成功登陸嘍!!!'.format(bot),'<<')
    try:
        synced = await bot.tree.sync()
        print(f'已為您同步{len(synced)}條命令')
    except Exception as e:
        print('命令同步時發生錯誤: ', e)


commandname = (f'{prefix}name')
@bot.tree.command(name = commandname, description = 'description')
async def name(interaction:discord.Integration):  
    await interaction.response.send_message("123")
    print("1")
    await asyncio.sleep(1)
    print("2")
    await interaction.response.edit_message(content="1234")
    print("3")





async def main():
    for Filename in os.listdir('./cmds'):
        if Filename.endswith('py'):
            await bot.load_extension(f'cmds.{Filename[:-3]}')
    await bot.start(setting['TOKEN'])
if __name__ == '__main__':
    if setting['keep_alive'] == 'True':
        keep_alive.keep_alive()
        asyncio.run(main())
    elif setting['keep_alive'] == 'False':
        asyncio.run(main())
    else:
        print('ERROR')
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

1
xinshou
iT邦見習生 ‧ 2024-04-27 07:36:03
最佳解答

參考這份 Docs 可以看到 Interaction 下有 edit_original_response 方法可以用。把指令函數修改成以下功能即正常執行。
順帶一提,你的參數類型好像打錯了,雖然不影響執行,但IDE可能會看不懂。"async def name(interaction:discord.Integration): "。有更多問題可以找我 DC: xs._.b

@bot.tree.command(name='test-cmd', description='description')
async def name(interaction: discord.Interaction):
    await interaction.response.send_message("123")
    await asyncio.sleep(1)
    await interaction.edit_original_response(content="456")
    print('done')

謝謝你的回覆
確實是參數類型打錯了,也許是我太著急了沒有檢查到
真的非常感謝


我...我想找藉口ww
怎麼錯的和對的都是綠字!!
https://ithelp.ithome.com.tw/upload/images/20240428/20145811d9APuALCdb.png

0
meebox
iT邦新手 5 級 ‧ 2024-04-28 13:34:46
@bot.tree.command(name = commandname, description = 'description')
async def name(interaction:discord.Interaction):  
    msg = await interaction.response.send_message("123")
    print("1")
    await asyncio.sleep(1)
    print("2")
    await msg.edit(content="1234")
    print("3")

這個曾經試過了W
但一樣沒有成功
不過還是非常感謝你的回答
你讓我知道我的思考方向、邏輯至少不是錯得離譜

非常感謝


那上面有個回覆,有成功的執行我想像中的反應
這問題就算是解決了

我要發表回答

立即登入回答