【问题标题】:Created a new embed field automatically once one is full? [VB]一旦一个已满,自动创建一个新的嵌入字段? [VB]
【发布时间】:2021-04-03 07:13:04
【问题描述】:
Private Async Function cmdList() As Task
    Dim m = Context.Message
    Dim u = Context.User
    Dim g = Context.Guild
    Dim c = Context.Client

    Dim words As String = ""

    Dim embed As New EmbedBuilder With {
        .Title = $"Wallpaper keyword list",
        .ImageUrl = "https://i.imgur.com/vc241Ku.jpeg",
        .Description = "The full list of keywords in our random wallpaper list",
        .Color = New Color(masterClass.randomEmbedColor),
        .ThumbnailUrl = g.IconUrl,
        .Timestamp = Context.Message.Timestamp,
        .Footer = New EmbedFooterBuilder With {
                .Text = "Keyword Data",
                .IconUrl = g.IconUrl
            }
        }

    For Each keyword As String In wall.keywords

        words = words + keyword + " **|** "
    Next
    embed.AddField("Full list", words)

    Await m.Channel.SendMessageAsync("", False, embed.Build())
End Function

这是我从数组中获取每个单词并将其放在字段中的命令。我想知道的是如何制作它,以便一旦字段填满,它将自动添加一个新字段并继续列表。这可能有点牵强,但只是不知道如何去做。对不起,如果我不能理解任何答案。我对 Discord.net 和 vb 上的编码还是有点陌生​​。

【问题讨论】:

  • 在构建你的words字符串时,检查要添加的新词的长度是否会导致字符串超过字段的最大长度。如果超过,不要将其添加到字符串中,而是将字符串添加到字段中...使用新单词重置words 继续相同的逻辑,直到完成。
  • 这会很有趣。不知道该怎么做。
  • hastebin.com/ixaqubelat.php 来自我的尝试。不知道我到底需要做什么。

标签: vb.net discord discord.net


【解决方案1】:

这是你hastebin代码的修改

Dim row As Integer = 0
Dim words As String = String.Empty

For Each keyword As String In wall.keywords
    'If appending the keyword to the list of words exceeds 256
    'don't append, but instead add the existing words to a field.
    If words.Length + keyword.length + 7 > 256 Then
        row += 1
        embed.AddField($"List #{row}", words) 'Add words to field
        
        'reset words
        words = String.Empty
    End If

    words = words + keyword + " **|** "
Next

'The add condition within the for loop is only entered when we are
'about to exceed to field length. Anything string under the max 
'length would exit the loop without being added. Add it here
embed.AddField($"List #{row + 1}", words)

Await m.Channel.SendMessageAsync("", False, embed.Build())

虽然它不会改变任何逻辑,但您可以考虑使用StringBuilder

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 2019-06-26
    • 2021-07-07
    相关资源
    最近更新 更多