Advertisement
MrRiptidePastes

Untitled

Jun 27th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1. from discord.ext import commands
  2. import discord
  3. from discord.ext.commands import Cog
  4. import asyncio
  5. import time
  6. import yaml
  7.  
  8.  
  9. class Art(object):
  10. def __init__(self, art, tags, artist):
  11. self.art = art
  12. self.tags = tags
  13. self.artist = artist
  14.  
  15.  
  16. def load_pending():
  17. with open("ascii/pending.yaml", "r", encoding="utf8") as pending_file:
  18. if pending_file.read().strip("\n") == "":
  19. return []
  20. print(pending_file)
  21. pending = yaml.load(pending_file, Loader=yaml.FullLoader)
  22. print(pending)
  23. return pending
  24.  
  25.  
  26. def save_pending(pending):
  27. pending_file = open("ascii/pending.yaml", "w")
  28. yaml.dump(pending, pending_file)
  29. pending_file.close()
  30.  
  31.  
  32. def load_library():
  33. library_file = open("ascii/pending.yaml", "r")
  34. if library_file.read().strip("\n") == "":
  35. return []
  36. library = yaml.load(library_file.read(), Loader=yaml.FullLoader)
  37. library_file.close()
  38. return library
  39.  
  40.  
  41. def save_library(library):
  42. library_file = open("ascii/pending.yaml", "w")
  43. yaml.dump(library, library_file)
  44. library_file.close()
  45.  
  46.  
  47. class ASCII(Cog):
  48. """
  49. ASCII module
  50.  
  51. Includes ASCII library and image to ASCII
  52. """
  53.  
  54. def __init__(self, bot):
  55. self.bot = bot
  56.  
  57. @commands.group("ascii")
  58. async def _ascii(self, ctx):
  59. if ctx.invoked_subcommand is None:
  60. await ctx.send("Invalid subcommand")
  61.  
  62. @_ascii.command(name="search")
  63. async def _ascii_search(self, ctx, *tags):
  64. pass
  65.  
  66. @_ascii.command(name="submit")
  67. async def _ascii_submit(self, ctx):
  68. last = await ctx.send("Enter ascii art:")
  69.  
  70. def check(m):
  71. return m.author == ctx.author and m.channel == ctx.channel
  72. try:
  73. art_message = await self.bot.wait_for("message", check=check, timeout=30)
  74. art = art_message.content
  75. except asyncio.TimeoutError:
  76. await last.delete()
  77. timeout = await ctx.send("Timed out")
  78. time.sleep(10)
  79. await timeout.delete()
  80. return
  81.  
  82. await last.delete()
  83.  
  84. last = await ctx.send("Enter tags separated by a comma")
  85.  
  86. try:
  87. tags_message = await self.bot.wait_for("message", check=check, timeout=30)
  88. tags = tags_message.content.replace(" ", "").split(",")
  89. except asyncio.TimeoutError:
  90. await last.delete()
  91. timeout = await ctx.send("Timed out")
  92. time.sleep(10)
  93. await timeout.delete()
  94. return
  95.  
  96. await last.delete()
  97.  
  98. confirmation = await ctx.send(f"""Are you sure you want to submit this ascii art?\n
  99. **Art:**
  100. {art_message.content}\n
  101. **Tags:**
  102. {", ".join(tags)}\n
  103. React :white_check_mark: to confirm and :x: to cancel.
  104. """)
  105.  
  106. await confirmation.add_reaction("✅")
  107. await confirmation.add_reaction("❌")
  108.  
  109. def check(emoji, user):
  110. return user == ctx.author and str(emoji) in ["✅", "❌"]
  111.  
  112. try:
  113. reaction, user = await self.bot.wait_for("reaction_add", timeout=60, check=check)
  114. except asyncio.TimeoutError:
  115. await confirmation.delete()
  116. timeout = await ctx.send("Timed out")
  117. time.sleep(10)
  118. await timeout.delete()
  119. return
  120. if reaction.emoji == "✅":
  121. ascii_art = Art(art, tags, ctx.author.id)
  122. await confirmation.delete()
  123. pending = load_pending()
  124. pending.append(ascii_art)
  125. save_pending(pending)
  126. final = await ctx.send("Submitted for review")
  127. time.sleep(10)
  128. await final.delete()
  129. return
  130. if reaction.emoji == "❌":
  131. await confirmation.delete()
  132. cancelled = await ctx.send("Cancelled")
  133. time.sleep(10)
  134. await cancelled.delete()
  135. return
  136.  
  137. @_ascii.command(name="review")
  138. async def _ascii_review(self, ctx):
  139. pending = load_pending()
  140. i = 0
  141. msg = await ctx.send("Loading...")
  142. while len(pending) > 0:
  143. embed = discord.Embed(title=f"Pending art {i}/{len(pending)}")
  144. item = pending[i]
  145. embed.add_field(name="Art:", value=item.art)
  146. embed.add_field(name="Tags:", value=", ".join(item.tags))
  147. artist_user = self.bot.get_user(item.artist)
  148. artist_name = f"{artist_user.display_name}#{artist_user.discriminator}"
  149. embed.add_field(name="Artist:", value=artist_name)
  150. await msg.edit("", embed=embed)
  151.  
  152. if i > 0:
  153. await msg.add_reaction("⏪")
  154. await msg.add_reaction("✅")
  155. await msg.add_reaction("❌")
  156. if i < len(pending) - 1:
  157. await msg.add_reaction("⏩")
  158.  
  159. def check(emoji, user):
  160. return user == ctx.author and str(emoji) in ["⏪", "✅", "🏳", "❌", "⏩"]
  161.  
  162. try:
  163. reaction, user = await self.bot.wait_for("reaction_add", timeout=60, check=check)
  164. except asyncio.TimeoutError:
  165. await msg.delete()
  166. timeout = await ctx.send("Timed out")
  167. time.sleep(10)
  168. await timeout.delete()
  169. return
  170. if reaction.emoji == "⏪":
  171. i -= 1
  172. if reaction.emoji == "✅":
  173. library = load_library()
  174. library.append(item)
  175. save_library(library)
  176. pending.pop(i)
  177. temp = await ctx.send("Accepted art")
  178. time.sleep(5)
  179. await temp.delete()
  180. if reaction.emoji == "❌":
  181. pending.pop(i)
  182. temp = await ctx.send("Denied art")
  183. time.sleep(5)
  184. await temp.delete()
  185. if reaction.emoji == "⏩":
  186. i += 1
  187. save_pending(pending)
  188. await msg.edit(content="No more pending ascii art")
  189.  
  190.  
  191. def setup(bot):
  192. bot.add_cog(ASCII(bot))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement