Skip to content

Commit

Permalink
Update notify_discord.py
Browse files Browse the repository at this point in the history
  • Loading branch information
CollinHeist committed Dec 29, 2023
1 parent 7f6705c commit 199c26d
Showing 1 changed file with 61 additions and 56 deletions.
117 changes: 61 additions & 56 deletions src/build/notify_discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,62 +43,67 @@ def notify_discord() -> None:

# Get environment data
for issue in issues:
# Parse issue from this issue
environment = {
'ISSUE_BODY': dumps(issue['body']),
'ISSUE_CREATOR': issue['user']['login'],
'ISSUE_CREATOR_ICON_URL': issue['user']['avatar_url'],
}
data = parse_submission(environment=environment)

# Create Embed object for webhook
embed = DiscordEmbed(
title=f'New Blueprint for {data["series_name"].strip()} ({data["series_year"]})',
description='\n'.join(data['blueprint']['description']), color='6391d2'
)

# Add creator as author
embed.set_author(
name=data['creator'],
icon_url=environment.get('ISSUE_CREATOR_ICON_URL', DEFAULT_AVATAR_URL),
)

# Add preview
embed.set_image(url=data['preview_urls'][0])

# Add thumbnail if >1 preview
if len(data['preview_urls']) > 1:
embed.set_thumbnail(url=data['preview_urls'][1])

# Add fields
if (templates := len(data['blueprint'].get('templates', []))):
label = 'Templates' if templates > 1 else 'Template'
embed.add_embed_field(label, templates)
if (fonts := len(data['blueprint'].get('fonts', []))):
label = 'Fonts' if fonts > 1 else 'Font'
embed.add_embed_field(label, fonts)
if (episodes := len(data['blueprint'].get('episodes', []))):
label = 'Episodes' if episodes > 1 else 'Episode'
embed.add_embed_field(label, episodes)
if (zip_url := data['source_file_zip_url']):
source_files = len(download_zip(zip_url, Path(__file__).parent / '.tmp'))
if source_files:
label = 'Source Files' if source_files > 1 else 'Source File'
embed.add_embed_field(label, source_files)

# Add timestamp
embed.set_timestamp()

# Create Webhook for adding embeds
webhook = DiscordWebhook(
url=environ.get('DISCORD_WEBHOOK'),
username=environ.get('DISCORD_USERNAME', 'MakerBot'),
avatar_url=environ.get('DISCORD_AVATAR', DEFAULT_AVATAR_URL)
)

# Add embed to Webhook, execute webhook
webhook.add_embed(embed)
webhook.execute()
try:
# Parse issue from this issue
environment = {
'ISSUE_BODY': dumps(issue['body']),
'ISSUE_CREATOR': issue['user']['login'],
'ISSUE_CREATOR_ICON_URL': issue['user']['avatar_url'],
}
data = parse_submission(environment=environment)

# Create Embed object for webhook
embed = DiscordEmbed(
title=f'New Blueprint for {data["series_name"].strip()} ({data["series_year"]})',
description='\n'.join(data['blueprint']['description']), color='6391d2'
)

# Add creator as author
embed.set_author(
name=data['creator'],
icon_url=environment.get('ISSUE_CREATOR_ICON_URL', DEFAULT_AVATAR_URL),
)

# Add preview
embed.set_image(url=data['preview_urls'][0])

# Add thumbnail if >1 preview
if len(data['preview_urls']) > 1:
embed.set_thumbnail(url=data['preview_urls'][1])

# Add fields
if (templates := len(data['blueprint'].get('templates', []))):
label = 'Templates' if templates > 1 else 'Template'
embed.add_embed_field(label, templates)
if (fonts := len(data['blueprint'].get('fonts', []))):
label = 'Fonts' if fonts > 1 else 'Font'
embed.add_embed_field(label, fonts)
if (episodes := len(data['blueprint'].get('episodes', []))):
label = 'Episodes' if episodes > 1 else 'Episode'
embed.add_embed_field(label, episodes)
if (zip_url := data['source_file_zip_url']):
temp_dir = Path(__file__).parent / '.tmp'
temp_dir.mkdir(parents=True, exist_ok=True)
source_files = len(download_zip(zip_url, temp_dir))
if source_files:
label = 'Source Files' if source_files > 1 else 'Source File'
embed.add_embed_field(label, source_files)

# Add timestamp
embed.set_timestamp()

# Create Webhook for adding embeds
webhook = DiscordWebhook(
url=environ.get('DISCORD_WEBHOOK'),
username=environ.get('DISCORD_USERNAME', 'MakerBot'),
avatar_url=environ.get('DISCORD_AVATAR', DEFAULT_AVATAR_URL)
)

# Add embed to Webhook, execute webhook
webhook.add_embed(embed)
webhook.execute()
except Exception as exc:
print(f'Error notifying Issue - {exc}')


if __name__ == '__main__':
Expand Down

0 comments on commit 199c26d

Please sign in to comment.