Reputation: 45
How can I replace more than one thing for example I did .replace("dnd", "Do Not Disturb") but how can I do the same with "online", "offline", and "idle" so I can put emojis in them.
embed.add_field(name="**•Status•**", value=str(member.status).replace("dnd", "Do Not Disturb") , inline=True)
Upvotes: 0
Views: 588
Reputation: 15689
You could simply chain the replace
methods like the other answers showed, though I think it's an ugly way of doing so.
You can have a dict that maps the statuses to the actual name, then simply using the status get the value:
status_dict = {
"online": "Online",
"offline": "Offline",
"idle": "Idle",
"dnd": "Do Not Disturb",
"invisible": "Invisible"
} # Change the values accordingly
status = status_dict[str(member.status)]
embed.add_field(name="**Status**", value=status)
Upvotes: 3
Reputation: 926
You can basically do the same thing, because .replace()
will return a string.
status = str(member.status)
formatted = status.replace("dnd", "Do Not Disturb").replace("online", "SomethingOnline").replace("offline", "SomethingOffline")
embed.add_field(
name="**•Status•**",
value=str(member.status).replace("dnd", "Do Not Disturb"),
inline=True
)
Upvotes: 0
Reputation: 666
This seems more like a python question than discord.py but...
str.replace
replaces all substring occurences with the second argument, and returns the new string. Since it returns the new string, you are able to stack once on top of another allowing you to do:
str(member.status).replace("dnd", "Do Not Disturb").replace("idle", "Idle").replace("online", "Online")
However, this is a bit on the bulky side, so I would instead create a list with the replacements and then loop through the list and replace.
mstatus = str(member.status)
l = [
["dnd", "Do Not Disturb"],
["idle", "Idle"],
["online", "Online"]
]
for status in l:
mstatus = mstatus.replace(status[0], status[1])
Upvotes: 0