discord.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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
  39. def in_allowed_role(user : discord.Member, server : int) -> bool :
  40. '''Check if a user has the permissions to configure the bot on a specific server '''
  41. targetRole = utils.get_allowed_role(server.id)
  42. globals.logger.debug ("Role target: " + str(targetRole))
  43. if user.guild_permissions.administrator:
  44. globals.logger.debug (str(user) + " is server admin on " + str(server) + "!")
  45. return True
  46. elif (targetRole is None):
  47. globals.logger.debug ("No group specified for " + str(server))
  48. return True
  49. else:
  50. for role in user.roles:
  51. if str(role.id) == str(targetRole):
  52. globals.logger.debug ("Permissions validated for " + str(user))
  53. return True
  54. return False