Skip to content

Commit 834a070

Browse files
authored
Merge pull request #2 from PyBotDevs/add-custom-profile-banner-support
Add support for custom user profile banners in `/profile`
2 parents a573d5b + 8e92f77 commit 834a070

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

main.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
# Check for Databases and Autogenerate them
1515
if not os.path.isdir("db"): os.mkdir("db")
16-
if not os.path.isfile("db/profile.json"):
16+
if not os.path.isfile("db/profiles.json"):
1717
with open("db/profiles.json", 'x', encoding="utf-8") as f: json.dump({}, f)
1818
if not os.path.isfile("db/user_ratings.json"):
1919
with open("db/user_ratings.json", 'x', encoding="utf-8") as f: json.dump({}, f)
@@ -27,7 +27,7 @@
2727
# Pre-initialization Commands
2828
def save() -> int:
2929
with open("db/user_ratings.json", 'w+') as f: json.dump(user_ratings, f, indent=4)
30-
# with open("db/profiles.json", 'w+') as f: json.dump(profile_metadata, f, indent=4) TODO: Uncomment this line once full profile metadata support is ready
30+
with open("db/profiles.json", 'w+') as f: json.dump(profile_metadata, f, indent=4) # TODO: Uncomment this line once full profile metadata support is ready
3131
return 0
3232

3333
def parse_rating(user_id: Union[int, str]) -> int:
@@ -54,6 +54,7 @@ async def on_ready():
5454
@client.event
5555
async def on_message(ctx):
5656
if str(ctx.author.id) not in user_ratings: user_ratings[str(ctx.author.id)] = {}
57+
if str(ctx.author.id) not in profile_metadata: profile_metadata[str(ctx.author.id)] = {"profile_banner_url": None}
5758
save()
5859

5960
# Slash Commands
@@ -112,7 +113,8 @@ async def profile(ctx: ApplicationContext, user: discord.User = None):
112113
localembed.add_field(name="Joined Discord at", value=f"{user.created_at.strftime('%d %B, %Y')}")
113114
localembed.add_field(name="User id", value=user.id)
114115
localembed.add_field(name="Rating", value=f"{str(parse_rating(user.id))} stars")
115-
# localembed.set_image(url=) TODO: Make profile metadata database, then activate this property
116+
if profile_metadata[str(user.id)]["profile_banner_url"] is not None:
117+
localembed.set_image(url=profile_metadata[str(user.id)]["profile_banner_url"])
116118
await ctx.respond(embed=localembed)
117119

118120
@client.slash_command(
@@ -128,6 +130,23 @@ async def rating(ctx: ApplicationContext, user: discord.User = None):
128130
)
129131
await ctx.respond(embed=localembed)
130132

133+
# User Profile Customization Commands
134+
# customization = discord.commands.SlashCommandGroup("customize", "Commands used to customize the user's /profile command.") Disable because command doesn't sync with this
135+
136+
@client.slash_command(
137+
name="profile_banner",
138+
description="Set a banner to display on your /profile command! (url only)"
139+
)
140+
@option(name="image_url", description="The url of your new profile banner (leave blank to disable)", type=str, default=None)
141+
async def banner(ctx: ApplicationContext, image_url: str = None):
142+
"""Set a banner to display on your /profile command! (url only)"""
143+
if (image_url is not None) and ("https://" not in image_url):
144+
return await ctx.respond("Your custom banner url must contain `https://`!", ephemeral=True)
145+
profile_metadata[str(ctx.author.id)]["profile_banner_url"] = image_url
146+
if image_url is None: localembed = discord.Embed(description=":white_check_mark: Your custom profile banner has been successfully removed.", color=discord.Color.green())
147+
else: localembed = discord.Embed(description=":white_check_mark: Your custom profile banner has been successfully set! Check it out using `/profile`.", color=discord.Color.green())
148+
return await ctx.respond(embed=localembed)
149+
131150
# User Commands
132151
@client.user_command(name="View Profile")
133152
async def _profile(ctx: ApplicationContext, user: discord.User):
@@ -141,7 +160,8 @@ async def _profile(ctx: ApplicationContext, user: discord.User):
141160
localembed.add_field(name="Joined Discord at", value=f"{user.created_at.strftime('%d %B, %Y')}")
142161
localembed.add_field(name="User id", value=user.id)
143162
localembed.add_field(name="Rating", value=f"{str(parse_rating(user.id))} stars")
144-
# localembed.set_image(url=) TODO: Make profile metadata database, then activate this property
163+
if profile_metadata[str(user.id)]["profile_banner_url"] is not None:
164+
localembed.set_image(url=profile_metadata[str(user.id)]["profile_banner_url"])
145165
await ctx.respond(embed=localembed)
146166

147167
@client.user_command(name="View Rating")

0 commit comments

Comments
 (0)