1
0

discord.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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(feed : utils.Feed):
  6. ''' Build the embed message related to the anime's status '''
  7. # Get service
  8. if feed.service == utils.Service.MAL:
  9. service_name = 'MyAnimeList'
  10. profile_url = "{}{}".format(globals.MAL_PROFILE_URL, feed.user.name)
  11. icon_url = globals.MAL_ICON_URL
  12. elif feed.service == utils.Service.ANILIST:
  13. service_name = 'AniList'
  14. profile_url = "{}{}".format(globals.ANILIST_PROFILE_URL, feed.user.name)
  15. icon_url = globals.ANILIST_ICON_URL
  16. else:
  17. raise NotImplementedError('Unknown service {}'.format(feed.service))
  18. description = "[{}]({})\n```{}```".format(utils.filter_name(feed.media.name), feed.media.url, feed.status)
  19. profile_url_label = "{}'s {}".format(feed.user.name, service_name)
  20. try:
  21. embed = discord.Embed(colour=0xEED000, url=feed.media.url, description=description, timestamp=feed.date_publication.astimezone(pytz.timezone("utc")))
  22. embed.set_thumbnail(url=feed.media.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