Search code examples
vb.netdiscorddiscord.net

Created a new embed field automatically once one is full? [VB]


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

This is my command to get every word from an array and put it on a field. What I want to know is how do I make it so once the field gets full it'll automatically add a new one and continue with the list. This might be a little far-fetched but just don't know how to go about this. Sorry if I can't understand any of the answers. I'm still a little new to coding on Discord.net and well vb in general.


Solution

  • This is a modification of you hastebin code

    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())
    

    While it does not change any of the logic, you could consider using a StringBuilder