Advertisement
Green0Fox

cog in python discord bot

Feb 28th, 2021
1,423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.66 KB | None | 0 0
  1. from discord.ext.commands import has_any_role
  2. from discord.ext import tasks, commands
  3. from discord.ext import commands
  4. from discord.utils import get
  5. import discord
  6. import asyncio
  7.  
  8. lobby_id = 790994912752566282
  9.  
  10. sights_ids = [
  11.     791322835837648927,
  12.     791322883137732608,
  13.     791322947797123143,
  14.     791322991593914368
  15. ]
  16.  
  17. gamemaster = 'Moderator'
  18.  
  19. escaping_id = 734825521673666611
  20.  
  21. ok_emoji = '\N{THUMBS UP SIGN}'
  22. book_emoji = '\N{BOOKS}'
  23. computer_emoji = '\N{DESKTOP COMPUTER}'
  24. envelope_emoji = '\N{ENVELOPE}'
  25.  
  26. class Escaperoom_General(commands.Cog):
  27.     def __init__(self, client):
  28.         self.client = client
  29.         self.ready = False
  30.  
  31.     @commands.Cog.listener()
  32.     async def on_ready(self):
  33.         print('Cog: Escaperoom Test is loaded in.')
  34.  
  35.     @commands.command()
  36.     @has_any_role(gamemaster)
  37.     async def reset(self, context):
  38.         self.lobby = context.guild.get_channel(lobby_id)
  39.         if context.message.channel != self.lobby:
  40.             return
  41.  
  42.         self.sights = []
  43.         for sight in sights_ids:
  44.             self.sights.append(context.guild.get_channel(sight))
  45.  
  46.         for sight in self.sights:
  47.             while len(await sight.history(limit = 1).flatten()) > 0:
  48.                 await sight.purge(limit = 10)
  49.  
  50.         self.escaper_role = get(context.guild.roles, id = escaping_id)
  51.         await self.lobby.set_permissions(self.escaper_role, read_messages = True,
  52.                                                               send_messages = False,
  53.                                                               add_reactions = False)
  54.         for sight_channel in self.sights:
  55.             await sight_channel.set_permissions(self.escaper_role, read_messages = False)
  56.  
  57.         msg = await self.lobby.send(f'{context.author.mention}, lobby has been reset.')
  58.         await asyncio.sleep(3)
  59.         await msg.delete()
  60.  
  61.         self.ready = True
  62.  
  63.         await self.example_function()
  64.  
  65.         # await self.example_function_2()
  66.  
  67.  
  68.  
  69.  
  70.     @commands.command()
  71.     @has_any_role(gamemaster)
  72.     async def escaperoom(self, context, *args):
  73.         if not self.ready:
  74.             await context.message.channel.send('The escaperoom hasn\'t been reset or is in progress.')
  75.             return
  76.  
  77.         self.amount_of_players = len(args)
  78.         self.players = args
  79.  
  80.         user = context.message.author
  81.         log_msg = 'Escaperoom was started by ' + str(user) + ' with players: '
  82.         for person in context.message.mentions:
  83.             await person.add_roles(self.escaper_role)
  84.             log_msg += person.name + ', '
  85.         print(log_msg[:-2])
  86.  
  87.         self.ready = False
  88.  
  89.  
  90.     async def timer(self):
  91.         while self.game_in_progress:
  92.             await asyncio.sleep(1)
  93.             self.time_so_far += 1
  94.  
  95.  
  96.     @commands.command()
  97.     @has_any_role(gamemaster)
  98.     async def intro(self, context, *args):
  99.         if context.message.channel != self.lobby:
  100.             return
  101.         if self.amount_of_players == 1:
  102.             prefix = 'Welcome, player, '
  103.         elif self.amount_of_players == 2:
  104.             prefix = 'Welcome, you two, '
  105.         else:
  106.             prefix = f'Welcome, you {self.amount_of_players}, '
  107.         intro_msg = await self.lobby.send(prefix + 'to the escape room. Press the reaction if everything is clear.')
  108.         await intro_msg.add_reaction(ok_emoji)
  109.         reaction, _ = await self.client.wait_for('reaction_add')
  110.         while not reaction.count > self.amount_of_players:
  111.             await asyncio.sleep(1)
  112.         await intro_msg.delete()
  113.  
  114.         await self.sights[0].set_permissions(self.escaper_role, read_messages = True, send_messages = False, add_reactions = True)
  115.         await self.sights[1].set_permissions(self.escaper_role, read_messages = True, send_messages = False, add_reactions = True)
  116.         await self.sights[2].set_permissions(self.escaper_role, read_messages = True, send_messages = False, add_reactions = True)
  117.         await self.sights[3].set_permissions(self.escaper_role, read_messages = True, send_messages = False, add_reactions = True)
  118.  
  119.         self.game_in_progress = True
  120.         self.time_so_far = 0
  121.         await self.timer()
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.     # @tasks.loop(seconds=50.0)
  129.     async def example_function(self):
  130.         while len(await self.sights[0].history(limit = 1).flatten()) > 0:
  131.             await self.sights[0].purge(limit = 10)
  132.  
  133.         await self.sights[0].send('This is the north wall. You see a **book**, a **computer** and an **envelope**.')
  134.         msg = await self.sights[0].send(file = discord.File('cogs/walls/north.png'))
  135.  
  136.         await msg.add_reaction(book_emoji)
  137.         book_reaction, _ = await self.client.wait_for('reaction_add')
  138.  
  139.         await msg.add_reaction(computer_emoji)
  140.         computer_reaction, _ = await self.client.wait_for('reaction_add')
  141.  
  142.         await msg.add_reaction(envelope_emoji)
  143.         envelope_reaction, _ = await self.client.wait_for('reaction_add')
  144.  
  145.         while (book_reaction.count == 1 and
  146.                computer_reaction.count == 1 and
  147.                envelope_reaction.count == 1):
  148.             await asyncio.sleep(1)
  149.             print('waiting for reaction north')
  150.  
  151.         if book_reaction.count > 1:
  152.             print('Book!')
  153.         elif computer_reaction.count > 1:
  154.             print('Computer!')
  155.         elif envelope_reaction.count > 1:
  156.             print('Envelope!')
  157.  
  158.     # @tasks.loop(seconds=50.0)
  159.     async def example_function_2(self):
  160.         while len(await self.sights[1].history(limit = 1).flatten()) > 0:
  161.             await self.sights[1].purge(limit = 10)
  162.  
  163.         await self.sights[1].send('This is the east wall. You see a **book**, a **computer** and an **envelope**.')
  164.         msg = await self.sights[1].send(file = discord.File('cogs/walls/north.png'))
  165.  
  166.         await msg.add_reaction(book_emoji)
  167.         book_reaction, _ = await self.client.wait_for('reaction_add')
  168.  
  169.         await msg.add_reaction(computer_emoji)
  170.         computer_reaction, _ = await self.client.wait_for('reaction_add')
  171.  
  172.         await msg.add_reaction(envelope_emoji)
  173.         envelope_reaction, _ = await self.client.wait_for('reaction_add')
  174.  
  175.         while (book_reaction.count == 1 and
  176.                computer_reaction.count == 1 and
  177.                envelope_reaction.count == 1):
  178.             await asyncio.sleep(1)
  179.             print('waiting for reaction north')
  180.  
  181.         if book_reaction.count > 1:
  182.             print('Book!')
  183.         elif computer_reaction.count > 1:
  184.             print('Computer!')
  185.         elif envelope_reaction.count > 1:
  186.             print('Envelope!')
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214. def setup(client):
  215.     client.add_cog(Escaperoom_General(client))
  216.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement