Daniel Tam
Daniel Tam

Reputation: 926

Discord.py links with box brackets around them

elif message.content.find("{memes") != -1:
    a = "https://imgur.com/Jlhfk4F"
    b = "https://i.imgur.com/seC2lyP.jpg?play"
    c = "https://imgur.com/CZyB2Zz"
    d = "https://imgur.com/N4KQcJ5"
    e = "https://imgur.com/kuDuY0b"
    f = "https://imgur.com/z6uMw0q"
    g = "https://imgur.com/gAa9Poq"
    h = "https://imgur.com/7dh4NFR"
    i = "https://imgur.com/ox8BQqv"
    j = "https://imgur.com/iLMRDZY"
    k = "https://imgur.com/UG0sLCd"
    l = "https://imgur.com/peCVHpF"
    m = "https://imgur.com/XgBE3Pu"
    n = "https://i.imgur.com/zoBayy9.jpg?play"
    o = "https://imgur.com/2wDk09q"
    p = "https://imgur.com/mz62oay"
    secret = random.choices([a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p])
    await message.channel.send(secret)

This is the current code I'm using for a random meme command for my bot. For some reason, the links being sent by the bot comes with box brackets on each side, just like this

['https://imgur.com/7dh4NFR']

Can someone help me?

Upvotes: 0

Views: 233

Answers (3)

TheSinisterShadow
TheSinisterShadow

Reputation: 196

It’s because you’re using random.choices instead of random.choice. random.choices returns a list of k choices, default 1, while random.choice returns just one every time.

Upvotes: 2

duckboycool
duckboycool

Reputation: 2455

The method random.choices returns a list of results since it is designed to be able to return multiple results. You should instead use random.choice.

Upvotes: 2

jkr
jkr

Reputation: 19300

You are using random.choices, which returns a list. Use random.choice instead. That returns one element.

secret = random.choice([a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p])

Here are relevant snippets from the Python documentation:

random.choice(seq)

Return a random element from the non-empty sequence seq. If seq is empty, raises IndexError.

random.choices(population, weights=None, *, cum_weights=None, k=1)

Return a k sized list of elements chosen from the population with replacement. If the population is empty, raises IndexError.

Upvotes: 1

Related Questions