discord.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import discord
  2. import pytz
  3. import myanimebot.globals as globals # TODO Rename globals module
  4. import myanimebot.utils as utils
  5. def build_embed(user, item_title, item_link, item_description, pub_date, image, service: utils.Service):
  6. ''' Build the embed message related to the anime's status '''
  7. # Get service
  8. if service == utils.Service.MAL:
  9. service_name = 'MyAnimeList'
  10. profile_url = "{}{}".format(globals.MAL_PROFILE_URL, user)
  11. icon_url = globals.MAL_ICON_URL
  12. elif service == utils.Service.ANILIST:
  13. service_name = 'AniList'
  14. profile_url = "{}{}".format(globals.ANILIST_PROFILE_URL, user)
  15. icon_url = globals.ANILIST_ICON_URL
  16. else:
  17. raise NotImplementedError('Unknown service {}'.format(service))
  18. description = "[{}]({})\n```{}```".format(utils.filter_name(item_title), item_link, item_description)
  19. profile_url_label = "{}'s {}".format(user, service_name)
  20. try:
  21. embed = discord.Embed(colour=0xEED000, url=item_link, description=description, timestamp=pub_date.astimezone(pytz.timezone("utc")))
  22. embed.set_thumbnail(url=image)
  23. embed.set_author(name=profile_url_label, url=profile_url, icon_url=icon_url)
  24. embed.set_footer(text="MyAnimeBot", icon_url=globals.iconBot)
  25. return embed
  26. except Exception as e:
  27. globals.logger.error("Error when generating the message: " + str(e))
  28. return
  29. # Function used to send the embed
  30. async def send_embed_wrapper(asyncioloop, channelid, client, embed):
  31. channel = client.get_channel(int(channelid))
  32. try:
  33. await channel.send(embed=embed)
  34. globals.logger.info("Message sent in channel: " + channelid)
  35. except Exception as e:
  36. globals.logger.debug("Impossible to send a message on '" + channelid + "': " + str(e))
  37. return