<reponame>Rookirk/Quote-Bot
import asyncio
import random

import discord
from discord.ext import commands
from discord_slash import cog_ext, SlashContext

import GlobalData

import ShouldRespond
import StripSpoilers
import FindQuote

GlobalData.init()

async def GuessingGameCo( channel, quote ):
    ShouldRespond.SetGameState( True )

    await channel.send( FindQuote.DisplayAnonymousQuote( quote ) )
    await channel.send( "Will reveal the speaker in 30 seconds!")

    await asyncio.sleep(20)

    await channel.send( "Will reveal the speaker in 10 seconds!")

    await asyncio.sleep(10)

    ShouldRespond.SetGameState( False )
    await channel.send( FindQuote.DisplayQuote( quote ) )

class GuessingGameCog( commands.Cog ):
    def __init__( self, bot ):
        self.bot = bot
        self.game = None

    @commands.Cog.listener()
    async def on_message( self, message ):
        if message.author == GlobalData.bot.user or message.author.bot:
            return

        if not ShouldRespond.ShouldSendMessage( message ):
            return

        print( "\n" + message.author.name + ' said: "' + message.content + '"' )

        nonSpoilerMessage = StripSpoilers.StripSpoilers( message.content )

        quote = FindQuote.FindQuote( nonSpoilerMessage )

        if quote:
            ShouldRespond.SetMessageTimeStamp()
            
            guildData = GlobalData.guildData[ message.guild.id ]
            if guildData[ 'mentionRole' ]:
                mention = message.guild.get_role( guildData[ 'mentionRole' ] ).mention
                await message.channel.send( mention )

            self.game = asyncio.create_task( GuessingGameCo( message.channel, quote ) )
    
    @cog_ext.cog_slash( name = "findquote", guild_ids = GlobalData.guildIDList )
    async def _findquote( self, ctx: SlashContext, phrase : str, instant : bool = False ):
        if ShouldRespond.GetGameState() and not instant:
            await ctx.send( 'Already in the middle of a game!')
            return

        print( "\n" + ctx.author.name + ' wanted to find: : "' + phrase + '"' )

        quote = FindQuote.FindQuote( phrase )

        if quote:
            if not instant:
                await ctx.send( 'Finding suitable quote for "' + phrase + '"')
                self.game = asyncio.create_task( GuessingGameCo( ctx.channel, quote ) )
            else:
                await ctx.send( 'Finding suitable quote for "' + phrase + '"\n' + FindQuote.DisplayQuote( quote ) )
        else:
            await ctx.send( 'Could not find suitable quote for "' + phrase + '"')
            
    @cog_ext.cog_slash( name = "findquotefrom", guild_ids = GlobalData.guildIDList )
    async def _findquotefrom( self, ctx: SlashContext, person : str ):

        print( "\n" + ctx.author.name + ' wanted to find a quote from: ' + person )

        quote = FindQuote.FindQuoteFrom( person )

        if quote:
            await ctx.send( FindQuote.DisplayQuote( quote ) )

        else:
            await ctx.send( str( person ) + " is not in the quote book!" )

    @cog_ext.cog_slash( name = "randomquote", guild_ids = GlobalData.guildIDList )
    async def _randomquote( self, ctx: SlashContext ):
        if ShouldRespond.GetGameState():
            await ctx.send( 'Already in the middle of a game!')
            return
        
        print("\nRandom quote!")

        quote = random.choice( GlobalData.quoteList )

        if quote:
            await ctx.send("Here's a random quote!")
            self.game = asyncio.create_task( GuessingGameCo( ctx.channel, quote ) )
        else:
            await ctx.send( 'Somehow I could not find a random quote')

    @cog_ext.cog_slash( name = "stop", guild_ids = GlobalData.guildIDList )
    async def _stop( self, ctx: SlashContext ):
        print("\nStopping Guessing Game")

        await ctx.send("Stopping Guessing Game")
        
        if self.game:
            ShouldRespond.SetGameState( False )
            self.game.cancel()

def setup(bot):
    bot.add_cog( GuessingGameCog(bot) )

import asyncio
import random

import discord
from discord.ext import commands
from discord_slash import cog_ext, SlashContext

import GlobalData

import ShouldRespond
import StripSpoilers
import FindQuote

GlobalData.init()

async def GuessingGameCo( channel, quote ):
    ShouldRespond.SetGameState( True )

    await channel.send( FindQuote.DisplayAnonymousQuote( quote ) )
    await channel.send( "Will reveal the speaker in 30 seconds!")

    await asyncio.sleep(20)

    await channel.send( "Will reveal the speaker in 10 seconds!")

    await asyncio.sleep(10)

    ShouldRespond.SetGameState( False )
    await channel.send( FindQuote.DisplayQuote( quote ) )

class GuessingGameCog( commands.Cog ):
    def __init__( self, bot ):
        self.bot = bot
        self.game = None

    @commands.Cog.listener()
    async def on_message( self, message ):
        if message.author == GlobalData.bot.user or message.author.bot:
            return

        if not ShouldRespond.ShouldSendMessage( message ):
            return

        print( "\n" + message.author.name + ' said: "' + message.content + '"' )

        nonSpoilerMessage = StripSpoilers.StripSpoilers( message.content )

        quote = FindQuote.FindQuote( nonSpoilerMessage )

        if quote:
            ShouldRespond.SetMessageTimeStamp()
            
            guildData = GlobalData.guildData[ message.guild.id ]
            if guildData[ 'mentionRole' ]:
                mention = message.guild.get_role( guildData[ 'mentionRole' ] ).mention
                await message.channel.send( mention )

            self.game = asyncio.create_task( GuessingGameCo( message.channel, quote ) )
    
    @cog_ext.cog_slash( name = "findquote", guild_ids = GlobalData.guildIDList )
    async def _findquote( self, ctx: SlashContext, phrase : str, instant : bool = False ):
        if ShouldRespond.GetGameState() and not instant:
            await ctx.send( 'Already in the middle of a game!')
            return

        print( "\n" + ctx.author.name + ' wanted to find: : "' + phrase + '"' )

        quote = FindQuote.FindQuote( phrase )

        if quote:
            if not instant:
                await ctx.send( 'Finding suitable quote for "' + phrase + '"')
                self.game = asyncio.create_task( GuessingGameCo( ctx.channel, quote ) )
            else:
                await ctx.send( 'Finding suitable quote for "' + phrase + '"\n' + FindQuote.DisplayQuote( quote ) )
        else:
            await ctx.send( 'Could not find suitable quote for "' + phrase + '"')
            
    @cog_ext.cog_slash( name = "findquotefrom", guild_ids = GlobalData.guildIDList )
    async def _findquotefrom( self, ctx: SlashContext, person : str ):

        print( "\n" + ctx.author.name + ' wanted to find a quote from: ' + person )

        quote = FindQuote.FindQuoteFrom( person )

        if quote:
            await ctx.send( FindQuote.DisplayQuote( quote ) )

        else:
            await ctx.send( str( person ) + " is not in the quote book!" )

    @cog_ext.cog_slash( name = "randomquote", guild_ids = GlobalData.guildIDList )
    async def _randomquote( self, ctx: SlashContext ):
        if ShouldRespond.GetGameState():
            await ctx.send( 'Already in the middle of a game!')
            return
        
        print("\nRandom quote!")

        quote = random.choice( GlobalData.quoteList )

        if quote:
            await ctx.send("Here's a random quote!")
            self.game = asyncio.create_task( GuessingGameCo( ctx.channel, quote ) )
        else:
            await ctx.send( 'Somehow I could not find a random quote')

    @cog_ext.cog_slash( name = "stop", guild_ids = GlobalData.guildIDList )
    async def _stop( self, ctx: SlashContext ):
        print("\nStopping Guessing Game")

        await ctx.send("Stopping Guessing Game")
        
        if self.game:
            ShouldRespond.SetGameState( False )
            self.game.cancel()

def setup(bot):
    bot.add_cog( GuessingGameCog(bot) )