|
| 1 | +from discord import Message |
| 2 | +from discord.ext import commands |
| 3 | +from libs.utils import RoboContext |
| 4 | +from rodhaj import Rodhaj |
| 5 | + |
| 6 | + |
| 7 | +class DmCommunications(commands.Cog): |
| 8 | + def __init__(self, bot: Rodhaj): |
| 9 | + self._bot = bot |
| 10 | + self._pool = bot.pool |
| 11 | + |
| 12 | + async def handle_user_no_thread(self, context: RoboContext): |
| 13 | + result = await context.prompt( |
| 14 | + "Seems like you do not have an active thread. " |
| 15 | + + "Would you want to create one?", |
| 16 | + timeout=600, |
| 17 | + delete_after=True, |
| 18 | + ) |
| 19 | + |
| 20 | + # Code to create thread here |
| 21 | + if result: |
| 22 | + await context.send( |
| 23 | + "Thread created; please use the following " |
| 24 | + + "thread for all further communications." |
| 25 | + ) |
| 26 | + else: |
| 27 | + await context.send("No worries!") |
| 28 | + |
| 29 | + async def handle_dm(self, message: Message): |
| 30 | + query = """ |
| 31 | + SELECT id |
| 32 | + FROM tickets |
| 33 | + WHERE owner_id = $1 |
| 34 | + """ |
| 35 | + row = await self._pool.fetchrow(query, message.author.id) |
| 36 | + |
| 37 | + message_ctx = await self._bot.get_context(message) |
| 38 | + if row is None: |
| 39 | + await self.handle_user_no_thread(message_ctx) |
| 40 | + |
| 41 | + @commands.Cog.listener() |
| 42 | + async def on_message(self, message: Message): |
| 43 | + if message.guild is None: |
| 44 | + if not message.author.bot: |
| 45 | + # private DM not from bot |
| 46 | + await self.handle_dm(message) |
| 47 | + |
| 48 | + |
| 49 | +async def setup(bot: Rodhaj): |
| 50 | + await bot.add_cog(DmCommunications(bot)) |
0 commit comments