vendredi 30 octobre 2020

How to create automatic new field if character reaches x amount of characters

For a while I have been trying to work on a automatic adding field whenever it reaches a specific characters total which in my case would be the Discord embed (Discord-embed). For each field there is a limitation of 1020 characters and after that you need to add a new field to be able to post to discord. Else it will return an error saying that it has reached its number and it will not be posted to discord.

The code I have written right now is:

from discord_webhook import DiscordEmbed, DiscordWebhook

data = {
    '1': ' - (https://www.python.org/)',
    '2': ' - (https://en.wikipedia.org/wiki/Python_(programming_language))',
    '3': None,
    '4': ' - (https://sv.wikipedia.org/wiki/Python_(programspr%C3%A5k))',
    '5': None,
    '6': ' - (https://www.learnpython.org/)',
    '7': ' - (https://www.w3schools.com/python/python_intro.asp)',
    '8': None,
    '9': ' - (https://www.codecademy.com/learn/learn-python)',
    '10': ' - (https://www.programiz.com/python-programming)',
    '11': None
}

embed = DiscordEmbed(title="Test", description="test", color=10881109)

dict_keys = list(data.keys())
characterCount, i = 0, 0
for index, key in enumerate(dict_keys):

    if data[key]:
        if len(dict_keys[index] + data[key]) + characterCount > 20: #I put 20 just for test, should be 1020
            embed.add_embed_field(name='Info', value='\n'.join([dict_keys[index] + data[key]]))
            characterCount, i = len(dict_keys[index] + data[key]), index
        else:
            characterCount += len(dict_keys[index] + data[key])

    else:
        if len(dict_keys[index]) + characterCount > 20: # I put 20 just for test, should be 1020
            embed.add_embed_field(name='Info', value='\n'.join([dict_keys[index]]))
            characterCount, i = len(dict_keys[index]), index
        else:
            characterCount += len(dict_keys[index])

webhook = DiscordWebhook(url="https://discordapp.com/api/webhooks/633369105713332254/Rx_3-AjWajxIM8uKButDV6mp3ymO4B4Ja7dyuRkzSQlTgd8l-UA_mdiM9evExb8e9klF")
webhook.add_embed(embed)
response = webhook.execute()

And here is two things I had struggles first with, the first one would be the key-value where value doesn't contain any values. Which means we just want to print out the key.

But my main problem is that I have got stuck where Im at this point that I dont know how I can split the code so that every time it reaches 1020 total of characters, it should create a new field which is: embed.add_embed_field(name='Info', value='\n'.join([dict_keys[index] + data[key]])) <-- embed.add_embed_field is how you create a new embed.

What I would like it to show in the discord webhook is whenever it reaches over 1020 characters (And that can be tested if we add a long url instead of the numbers inside the ():

Info                                                                Info
1 - (https://www.python.org/)                                       7 - (https://www.w3schools.com/python/python_intro.asp)
2 - (https://en.wikipedia.org/wiki/Python_(programming_language))   8
3                                                                   9 - (https://www.codecademy.com/learn/learn-python)
4 - (https://sv.wikipedia.org/wiki/Python_(programspr%C3%A5k)       10 - (https://www.programiz.com/python-programming)
5                                                                   11
6 - (https://www.learnpython.org/)

but thats the outlook it should look once we reach over 1020 characters in one embed.

How am I able to do that?

Aucun commentaire:

Enregistrer un commentaire