I am trying to iterate through all the values in this JSON dictionary to retrieve whenever a user(s) has the desired date set as their birthday. However, it will only return the first value.
This is the dictionary I am referencing:
{
"TARGETGUILD": {
"USERID1": {
"month": "09",
"day": "10"
},
"USERID2": {
"month": "09",
"day": "10"
}
},
"otherGUILD": {
"USERID": {
"month": "09",
"day": "10"
}
}
}
Here is the code:
@commands.command(name="today", pass_context=True)
async def today(self, ctx):
today = datetime.today()
today_month = today.strftime("%m")
today_day = today.strftime("%d")
guild_av = ctx.guild.icon_url
with open('commands/birthday/birthdays.json', 'r') as f:
data = json.load(f)
guild_data = data[str(ctx.guild.id)]
birthday_dict = {"month": str(today_month), "day": str(today_day)}
#length = len(guild_data)
def find_birthday():
for user in guild_data:
if guild_data[user] == birthday_dict:
return ":tada: <@!" + str(user) + ">" + "\n"
else:
return "`None`"
embed = discord.Embed(title=f":calendar_spiral: {today_month}/{today_day}", description="**Today's Birthdays:**" + "\n" + "\n" + str(find_birthday()), color=discord.Color.greyple())
embed.set_thumbnail(url=guild_av)
await ctx.send(embed=embed)
In this screenshot, you'll see it only will display the first user.
The "other" guild ID in the JSON, for example, will obviously work properly as there's only one user with a birthday as seen in this screenshot.
I've tried a couple of things, just to try and find out what my issue was.
For instance, when I do:
def find_birthday():
for user in guild_data:
print(user)
It DOES print each user. So, that's what leads me to believe the issue is within my if statement. Whenever it sees the user's birthday matches, it only will print the first user that matches.
So, after looking through many threads... I just can't figure out how to make it list each person.
I apologize if this is a simple fix and/or easy solution that I'm missing (typically it always is). I have been stuck on it for a bit now, though. I'm still learning python. I'd appreciate it if anyone could steer me in the right direction.
QUESTION
How do re-write this to allow it to return multiple values within an
ifstatement?
Thanks so much.