Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import discord
- import mysql.connector
- conn = mysql.connector.connect(
- user='juggerdonk',
- password='juggerdonk_pw',
- host='89.163.249.71',
- database='down2clown'
- )
- cursor = conn.cursor()
- TOKEN = 'NDQ4NTUzMzAyNDk0NDEyODEx.DeXzWA.iQWtRTrU5c6aIvqZO7upmO5tcIA'
- client = discord.Client()
- @client.event
- async def on_message(message):
- # To prevent the bot from responding to itself
- if message.author == client.user:
- return
- # If the message starts with !joint, this happens:
- if message.content.startswith('!joint'):
- # This is the query we are going to send to the database to insert/update the data.
- # Everything before "ON DUPLICATE" is just inserting the discord user id and amount into the database,
- # If the discord_user_id is already in the database (which means they have used !joint once before), the code after ON DUPLICATE happens:
- # 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
- query = "INSERT INTO joint_command (discord_user_id, joint_amount) VALUES ('" + message.author.id + "', 1) ON DUPLICATE KEY UPDATE joint_amount=joint_amount+1"
- # cursor.execute(), this send the query to the database
- cursor.execute(query)
- #conn.commit, this from what I understand is just confirming the changes? Basically like a "are you sure?" ???
- conn.commit()
- # Here we do another query, this time we are SELECTING something, grabbing and looking for some data/info
- # It's pretty human readable and easy to understand what we do here:
- # 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.
- query = "SELECT * FROM joint_command WHERE discord_user_id = '" + message.author.id + "'"
- # cursor.execute(), this send the second query to the database, we apparently don't need conn.commit() after this.
- cursor.execute(query)
- # 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
- # This returns the data as an array. So here we send the message "7marre, Amount: 1".
- for (discord_user_id, joint_amount) in cursor:
- # {}, {} is replaced by message.author.name and joint_amount respectively.
- await client.send_message(message.channel, "{}, Amount: {}".format(message.author.name, joint_amount))
- @client.event
- async def on_ready():
- print('Logged in as')
- print(client.user.name)
- print(client.user.id)
- print('------')
- client.run(TOKEN)
- cursor.close()
- conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement