Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @bot.command(pass_context=True)
- async def marry(ctx):
- """Get together with the person of your dreams <3"""
- if len(ctx.message.mentions) < 1:
- await bot.say('I\'m sorry, I can\'t seem to find that person.')
- else:
- target = ctx.message.mentions[0]
- #STEP 0: Create DB connection
- #you might want to put this in a helper function and just have it return the conn object
- db = await aiomysql.connect(host='localhost', port=3306,
- user='tohru', password='dfordragon1987', db='TOHRU_DB',
- loop=bot.loop)
- '''except:
- print("Unable to establish database connection")
- print("Function ended prematurely")
- return
- '''
- if ctx.message.author.id == target.id and ctx.message.author.id != '128568253336387584':
- await bot.say('Why would you try to marry yourself? You must be lonely...')
- return
- #STEP 1: check if users are in the DB
- async with db.cursor() as cur:
- #check if person sending message is in DB
- await cur.execute("SELECT UserID FROM USERS WHERE UserID = %s", ctx.message.author.id)
- if cur.rowcount == 0:
- #add them
- await cur.execute("INSERT INTO USERS VALUES (%s)", ctx.message.author.id)
- await db.commit()
- #check if target is in db
- await cur.execute("SELECT UserID FROM USERS WHERE UserID = %s", target.id)
- if cur.rowcount == 0:
- #add them
- await cur.execute("INSERT INTO USERS VALUES (%s)", target.id)
- await db.commit()
- #STEP 2: check if either user is married
- #if the person using the command is maried
- await cur.execute("SELECT UserOneID, UserTwoID FROM IS_MARRIED WHERE UserOneID = %s OR UserTwoID = %s",
- (ctx.message.author.id, ctx.message.author.id))
- if cur.rowcount != 0:
- #There exists a relationship featuring the person who sent the command
- r = cur.fetchone()
- if (r[0] == ctx.message.author.id and r[1] == target.id) or (r[1] == ctx.mesage.author.id and r[0] == target.id):
- #The person who called the command is already married to the target
- #give message here
- await bot.say('You two are already married, silly.')
- await db.commit()
- return
- else:
- #only other option is that the person who called the command is still married but to someone else
- #give message here
- if r[0] == ctx.message.author.id:
- married_id = r[1]
- else:
- married_id = r[0]
- spouse = await bot.get_user_info(id=married_id)
- await bot.say('You\'re already married to **' + spouse.name + '**. I cannot allow you to cheat on your significant other!')
- await db.commit()
- return
- await db.commit()
- #check if the target is married
- await cur.execute("SELECT UserOneID, UserTwoID FROM IS_MARRIED WHERE UserOneID = %s OR UserTwoID = %s",
- (target.id, target.id))
- if cur.rowcount != 0:
- #target is already married, give them a message or something
- if r[0] == target.id:
- married_id = r[1]
- else:
- married_id = r[0]
- spouse = await bot.get_user_info(id=married_id)
- await bot.say('**' + target.name + '** is already married to **' + spouse.name + '**. I cannot allow you to ruin their relationship!')
- await db.commit()
- return
- await db.commit()
- #STEP 3: marry users
- await cur.execute("INSERT INTO IS_MARRIED VALUES (%s, %s)", (ctx.message.author.id, target.id))
- await db.commit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement