Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | None | 0 0
  1. import discord
  2. import mysql.connector
  3.  
  4. conn = mysql.connector.connect(
  5.     user='juggerdonk',
  6.     password='juggerdonk_pw',
  7.     host='89.163.249.71',
  8.     database='down2clown'
  9. )
  10. cursor = conn.cursor()
  11.  
  12. TOKEN = 'NDQ4NTUzMzAyNDk0NDEyODEx.DeXzWA.iQWtRTrU5c6aIvqZO7upmO5tcIA'
  13. client = discord.Client()
  14.  
  15. @client.event
  16. async def on_message(message):
  17.     # To prevent the bot from responding to itself
  18.     if message.author == client.user:
  19.         return
  20.  
  21.     # If the message starts with !joint, this happens:
  22.     if message.content.startswith('!joint'):
  23.         # This is the query we are going to send to the database to insert/update the data.
  24.         # Everything before "ON DUPLICATE" is just inserting the discord user id and amount into the database,
  25.         # If the discord_user_id is already in the database (which means they have used !joint once before), the code after ON DUPLICATE happens:
  26.         # After ON DUPLICATE: if the user has already done !joint before, we just update the row in the database, adding one to the joint_amount column
  27.         query = "INSERT INTO joint_command (discord_user_id, joint_amount) VALUES ('" + message.author.id + "', 1) ON DUPLICATE KEY UPDATE joint_amount=joint_amount+1"
  28.  
  29.         # cursor.execute(), this send the query to the database
  30.         cursor.execute(query)
  31.         #conn.commit, this from what I understand is just confirming the changes? Basically like a "are you sure?" ???
  32.         conn.commit()
  33.  
  34.         # Here we do another query, this time we are SELECTING something, grabbing and looking for some data/info
  35.         # It's pretty human readable and easy to understand what we do here:
  36.         # We SELECT everything(*) from the table(where data is stored) joint_command WHERE the discord_user_id is the same discord_user_id as the one who sent the command.
  37.         query = "SELECT * FROM joint_command WHERE discord_user_id = '" + message.author.id + "'"
  38.  
  39.         # cursor.execute(), this send the second query to the database, we apparently don't need conn.commit() after this.
  40.         cursor.execute(query)
  41.  
  42.         # The SELECT query returns the data from the database we selected. We selected everything(*) which means all columns in the table. We only have discord_user_id and joint_amount
  43.         # This returns the data as an array. So here we send the message "7marre, Amount: 1".
  44.         for (discord_user_id, joint_amount) in cursor:
  45.             # {}, {} is replaced by message.author.name and joint_amount respectively.
  46.             await client.send_message(message.channel, "{}, Amount: {}".format(message.author.name, joint_amount))
  47.  
  48. @client.event
  49. async def on_ready():
  50.     print('Logged in as')
  51.     print(client.user.name)
  52.     print(client.user.id)
  53.     print('------')
  54.  
  55. client.run(TOKEN)
  56. cursor.close()
  57. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement