創作內容

1 GP

Discord Bot 解決 Embed 最大 25 條限制 DiscordBot solves the maximum of embed row. (Python)

作者:Tsukasa│2021-06-30 04:41:18│巴幣:1,000│人氣:803


這幾天在寫 Discord 的機器人,
遇到了一個小問題,網路上也沒有中文的解決方式,外文的方法也十分稀少。
因此就決定來獻醜給後人乘涼~~

狀況:
Discord 的 Embed 元件雖然標榜有 6000 bytes 的大小可以使用,卻只能容納 25 行的訊息,
簡單來說,就是只能 embed.add_field() 25 次,超過的行數就會被自動省略。

解決方式:
原程式碼:
embed = discord.Embed( title = "Title" )
embed.add_field( name = "Name", value = "Value", inline = False )
await message.channel.send( embed = embed )

新程式碼:
#embed = discord.Embed( title = "Title" )
o_embed = [ None, None, None, None, None, None, None, None ,None ,None ]
embed_now = 0
embed_count = 0
o_embed[ embed_now ] = discord.Embed( title = "Title" )

#embed.add_field( name = "Name", value = "Value", inline = False)
o_embed[ embed_now ].add_field( name = "Name", value = "Value", inline = False )
if embed_count == 25:
    embed_now += 1
    o_embed[ embed_now ] = discord.Embed( title = '*' )
    embed_count = 0

#await message.channel.send( embed = embed )
for i in o_embed:
    if i == None:
        break
    else:
        await message.channel.send( embed = i )


成果:
Before:

After:

其實就是把 Embed 用 List 來表示,
若 .add_field 到了最大 25 次後,接下來就寫入到下個 Embed 裡。

當然還有更多方式可以寫得更好,
例如:使用 Link List 並 Overwrite class,可以讓程式碼更加漂亮,往後要用時只要呼叫 class 就可以。
看之後我有沒有多餘的時間完成。

歡迎轉載,但請附上出處。
有問題歡迎留言詢問。

引用網址:https://home.gamer.com.tw/TrackBack.php?sn=5193491
All rights reserved. 版權所有,保留一切權利

相關創作

同標籤作品搜尋:discord_bot|discord|Embed|limit|25 rows|python|discord embed limit

留言共 0 篇留言

我要留言提醒:您尚未登入,請先登入再留言

1喜歡★chenteddy 可決定是否刪除您的留言,請勿發表違反站規文字。

前一篇:黑櫻!!... 後一篇:近況分享...

追蹤私訊切換新版閱覽

作品資料夾

xzp83502在線巴哈們
果果日記小屋更新中 歡迎進來參觀 謝謝^^看更多我要大聲說昨天20:43


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】