Death_Blows
Death_Blows

Reputation: 3

Trying to sub inside a embed with swears, doesn't work for strings in the last list

I'm attempting to make an urban dictionary command, I am substituting swear words with the word '[NSFW]' though it only works when I place the swear in the last part of the list.

My Code:

nsfwdefine = ["swear1", "swear2","swear3"]

// there are headers I just didn't add them because it has my apikey.

async with ClientSession() as session:
    async with session.get(url, headers=headers, params=querystring) as response:
        r = await response.json()
        embed = discord.Embed(title=f"All result for {term}", description=None,color=0xff0000)
        embed.set_author(name=ctx.author.display_name, url="https://ass.com/", icon_url=ctx.author.avatar_url)
        definition = r['list'][0]['definition']
        embed.add_field(name=term, value=definition, inline=False)
        txt = str(definition)
        for y in nsfwdefine:
          x = re.sub(y, '[NSFW]', txt)
        print(x)

embed2=discord.Embed(title=f"All results for {term}", description=x)
embed2.add_field(name="Info",value=":warning: If your message contains the word [NSFW] then you need to rerun the defintion in a NSFW Channel.")

(await ctx.send(embed = embed2))

I'm not entirely sure what's going on, does anyone know the solution to this?

Upvotes: 0

Views: 35

Answers (1)

Skully
Skully

Reputation: 3126

I'm not sure why you're using re.sub() though you can achieve this using the string replace() method.

swears = ["swear1", "swear2", "swear3"]
sentence = "hello swear1 this is swear2 a sample swear3 sentence!"

for word in swears:
    sentence = sentence.replace(word, "[NSFW]")

print(sentence)

Output: hello [NSFW] this is [NSFW] a sample [NSFW] sentance!

Run Code Snippet

Upvotes: 1

Related Questions