discord.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 = utils.build_description_string(feed)
  19. content = "[{}]({})\n```{}```".format(utils.filter_name(feed.media.name), feed.media.url, description)
  20. profile_url_label = "{}'s {}".format(feed.user.name, service_name)
  21. try:
  22. embed = discord.Embed(colour=0xEED000, url=feed.media.url, description=content, timestamp=feed.date_publication.astimezone(pytz.timezone("utc")))
  23. embed.set_thumbnail(url=feed.media.image)
  24. embed.set_author(name=profile_url_label, url=profile_url, icon_url=icon_url)
  25. embed.set_footer(text="MyAnimeBot", icon_url=globals.iconBot)
  26. return embed
  27. except Exception as e:
  28. globals.logger.error("Error when generating the message: " + str(e))
  29. return
  30. async def send_embed_wrapper(asyncioloop, channelid, client, embed):
  31. ''' Send an embed message to a channel '''
  32. channel = client.get_channel(int(channelid))
  33. try:
  34. await channel.send(embed=embed)
  35. globals.logger.info("Message sent in channel: {}".format(channelid))
  36. except Exception as e:
  37. globals.logger.debug("Impossible to send a message on '{}': {}".format(channelid, e))
  38. return