Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. @bot.command(pass_context=True)
  2. async def marry(ctx):
  3. """Get together with the person of your dreams <3"""
  4. if len(ctx.message.mentions) < 1:
  5. await bot.say('I\'m sorry, I can\'t seem to find that person.')
  6. else:
  7. target = ctx.message.mentions[0]
  8.  
  9. #STEP 0: Create DB connection
  10. #you might want to put this in a helper function and just have it return the conn object
  11.  
  12. db = await aiomysql.connect(host='localhost', port=3306,
  13. user='tohru', password='dfordragon1987', db='TOHRU_DB',
  14. loop=bot.loop)
  15. '''except:
  16. print("Unable to establish database connection")
  17. print("Function ended prematurely")
  18. return
  19. '''
  20.  
  21. if ctx.message.author.id == target.id and ctx.message.author.id != '128568253336387584':
  22. await bot.say('Why would you try to marry yourself? You must be lonely...')
  23. return
  24.  
  25. #STEP 1: check if users are in the DB
  26. async with db.cursor() as cur:
  27. #check if person sending message is in DB
  28. await cur.execute("SELECT UserID FROM USERS WHERE UserID = %s", ctx.message.author.id)
  29. if cur.rowcount == 0:
  30. #add them
  31. await cur.execute("INSERT INTO USERS VALUES (%s)", ctx.message.author.id)
  32. await db.commit()
  33.  
  34. #check if target is in db
  35. await cur.execute("SELECT UserID FROM USERS WHERE UserID = %s", target.id)
  36. if cur.rowcount == 0:
  37. #add them
  38. await cur.execute("INSERT INTO USERS VALUES (%s)", target.id)
  39. await db.commit()
  40.  
  41. #STEP 2: check if either user is married
  42. #if the person using the command is maried
  43. await cur.execute("SELECT UserOneID, UserTwoID FROM IS_MARRIED WHERE UserOneID = %s OR UserTwoID = %s",
  44. (ctx.message.author.id, ctx.message.author.id))
  45. if cur.rowcount != 0:
  46. #There exists a relationship featuring the person who sent the command
  47. r = cur.fetchone()
  48.  
  49. if (r[0] == ctx.message.author.id and r[1] == target.id) or (r[1] == ctx.mesage.author.id and r[0] == target.id):
  50. #The person who called the command is already married to the target
  51. #give message here
  52. await bot.say('You two are already married, silly.')
  53. await db.commit()
  54. return
  55. else:
  56. #only other option is that the person who called the command is still married but to someone else
  57. #give message here
  58. if r[0] == ctx.message.author.id:
  59. married_id = r[1]
  60. else:
  61. married_id = r[0]
  62. spouse = await bot.get_user_info(id=married_id)
  63. await bot.say('You\'re already married to **' + spouse.name + '**. I cannot allow you to cheat on your significant other!')
  64. await db.commit()
  65. return
  66. await db.commit()
  67.  
  68. #check if the target is married
  69. await cur.execute("SELECT UserOneID, UserTwoID FROM IS_MARRIED WHERE UserOneID = %s OR UserTwoID = %s",
  70. (target.id, target.id))
  71. if cur.rowcount != 0:
  72. #target is already married, give them a message or something
  73. if r[0] == target.id:
  74. married_id = r[1]
  75. else:
  76. married_id = r[0]
  77. spouse = await bot.get_user_info(id=married_id)
  78. await bot.say('**' + target.name + '** is already married to **' + spouse.name + '**. I cannot allow you to ruin their relationship!')
  79. await db.commit()
  80. return
  81. await db.commit()
  82.  
  83. #STEP 3: marry users
  84. await cur.execute("INSERT INTO IS_MARRIED VALUES (%s, %s)", (ctx.message.author.id, target.id))
  85. await db.commit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement