簡體   English   中英

編輯消息嵌入的一部分 (Discord.JS)

[英]Edit part of a Message Embed (Discord.JS)

我有一個包含 10 條嵌入消息的通道(每條消息嵌入 1 條)。 每個嵌入都是 Track 提供的人們最佳單圈時間的排行榜。

每個嵌入的布局是

const trackName = new MessageEmbed
.setTitle(trackName) 
.addField(user1, lapTime)
.addField(user2, lapTime) 
.addField(user3, lapTime)

例如,假設第三個嵌入看起來像這樣:

| 軌道 3 名稱

| 約翰 37 秒

| 克里斯 39 秒

| 傑夫 40 秒

除了簡單地編輯嵌入並發送手動更新的所有信息之外,我怎么能只更新一個特定的插槽? 例如,假設克拉克以 38 秒的成績進入,我將如何將 Chris 移至第 3 位,移除 Jeff,並將克拉克添加至第 2 位,以便嵌入看起來如此

| 軌道 3 名稱

| 約翰 37 秒

| 克拉克 38 秒

| 克里斯 39 秒

不更改頻道中的任何其他嵌入

您不一定需要創建一個全新的嵌入並將所有信息輸入其中。 您可以從消息中獲取當前嵌入,並編輯您需要編輯的特定字段。 以下是您可以適應與您的系統一起使用的示例:

function editTrack(msg, newUser, newTime) {
    //Assume `msg` is the message in the channel containing the embed you want to edit
    //currentEmbed is now the embed you want to edit
    let currentEmbed = msg.embeds[0];

    //Add `newUser` and its `newTime` to the embed as a new field, in the last position
    currentEmbed.addField(newUser, newTime);

    //Sorts the embed's fields by the lap times of each user, from lowest to highest
    //This example numerically sorts the fields by the number in their value
    //This does most of the work for you; the laps are now in the correct order
    currentEmbed.fields.sort((a, b) => Number(a.value.split(" second")[0]) - Number(b.value.split(" second")[0]));

    //If you want to display only the top 3, remove the 4th field of the embed (if any)
    if (currentEmbed.fields.length == 4) currentEmbed.fields.splice(3, 1);

    //Now, we need to edit the message with our updated embed (djs 13.x syntax)
    return msg.edit({embeds: [currentEmbed]});
}

我使用我的機器人的 eval 命令之一測試了這個editTrack方法:

前 -
首先嵌入

后 -
編輯嵌入

原始嵌入已使用新信息成功編輯。 並且它只需要包含嵌入的Message對象、新圈的用戶和新圈的時間。


根據 OP 的評論進行編輯

為了在編輯帶有新單圈時間的現有用戶嵌入的軌道時工作的答案,需要稍作修改。 在刪除嵌入的第 4 個字段之前,您必須執行以下操作:

const names = new Set();
currentEmbed.fields = currentEmbed.fields.filter(field =>
    !names.has(field.name) && names.add(field.name)
);

這是該代碼正在執行的操作。 首先,我們創建一個Set 集合是類似於數組的可迭代結構,但它們只能包含唯一值。 因此,我們可以使用集合來確保沒有重復的用戶名。 接下來,我們過濾字段; 只有用戶尚未包含在names的字段才會保留。 如果用戶名尚未在names ,則會通過.add()將其添加到集合中。 並且因為這些字段已經按照最快單圈時間進行排序,所以只會保留給定用戶的最快單圈; 同一用戶的任何更長的單圈時間將被過濾掉。

請注意,我只是簡單地測試了這個編輯,讓我知道是否有任何邏輯錯誤或由它引起的其他問題(或者它是否完全無法正常工作)。

感謝Cannicide 的有用回答,它讓我走上了正軌。

為了實現排序時間和覆蓋現有時間的最終結果,我最終使用了以下函數。 我要求所有時間都以以下格式提交:MINUTES:SECONDS (0:35 = 35s | 4:24 = 4m24s | 62:08 = 1h2m8s)。

function editLb(theMessage, newUser, newTime) {
    //get the embed you want to edit
    let currentEmbed = theMessage.embeds[0];

    //Check all existing fields for the same newUser, if the same newUser
    //is found, replace that entire field with the name: "Placeholder"
    //and the value: "999:99". This will also remove any existing duplicates.
    currentEmbed.fields.forEach(field => {
        if (field.name == newUser) {
            field.name = `Placeholder`;
            field.value = `999:99`;
        }
    })

    //add the newUser and the newTime to a new field
    currentEmbed.addField(`${newUser}`, `${newTime}`);

    //sort all available fields effectively by seconds, by taking 
    // (minutes*60) + (seconds)
    currentEmbed.fields.sort((a, b) => Number((a.value.split(":")[0])*60 + (a.value.split(":")[1])) - Number((b.value.split(":")[0])*60 + (b.value.split(":")[1])));
    
    //If there are now 4 fields, remove the slowest(last) one.
    if (currentEmbed.fields.length == 4) currentEmbed.fields.splice(3, 1);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM