Facebook
From me, 1 Month ago, written in Python.
Embed
Download Paste or View Raw
Hits: 130
  1. import discord
  2. from discord.ext import commands
  3. import os
  4. import random
  5. import mysql.connector
  6. from PIL import Image
  7. import datetime
  8.  
  9. bot = commands.Bot(command_prefix='?', intents=discord.Intents.all())
  10.  
  11. db = {
  12.     'host': '127.0.0.1',
  13.     'user': 'root',
  14.     'password': 'Deathisnotaword1904!',
  15.     'database': 'TheBlessedMachine'
  16. }
  17.  
  18. cursor = None
  19. try:
  20.     cnx = mysql.connector.connect(**db)
  21.     cursor = cnx.cursor()
  22.     print("Connection to MySQL DB successful")
  23. except mysql.connector.Error as err:
  24.     print(f"Error connecting to MySQL: {err}")
  25.  
  26. @bot.event
  27. async def on_ready():
  28.     print('Bot is ready')
  29.  
  30. @bot.event
  31. async def on_disconnect():
  32.     global cursor
  33.     if cnx.is_connected():
  34.         cursor.close()
  35.         cnx.close()
  36.  
  37. @bot.command()
  38. async def hello(ctx):
  39.     await ctx.send('Hi')
  40.  
  41. @bot.command()
  42. async def adopt(ctx):
  43.     global cursor
  44.     global egg_file_path
  45.     global directory
  46.     directory= 'C:/Users/Admin/The Blessed Machine/Eggs'
  47.     if not cnx.is_connected():
  48.         cnx.reconnect()
  49.         cursor = cnx.cursor()
  50.  
  51.     cursor.execute('select count(*) from adoptedEggs where user_id = %s', (ctx.author.id,))
  52.     result = cursor.fetchone()
  53.  
  54.     if result[0] >= 1:
  55.         await ctx.send('You already have an egg!')
  56.         return
  57.     egg = random.choice(os.listdir(directory))
  58.     egg_file_path = os.path.join(directory, egg)
  59.  
  60.     with open(egg_file_path, 'rb') as file:
  61.         egg_image = Image.open(file)
  62.         file.seek(0)
  63.         await ctx.send(file=discord.File(file, "random_egg.png"))
  64.  
  65.     cursor.execute('insert into adoptedEggs (user_id, egg, timestamp) values (%s, %s, NOW())', (ctx.author.id, egg))
  66.     cnx.commit()
  67.  
  68. @bot.command()
  69. async def myegg(ctx):
  70.     global cursor
  71.     global egg_directory
  72.  
  73.     if not cnx.is_connected():
  74.         cnx.reconnect()
  75.         cursor = cnx.cursor()
  76.  
  77.     cursor.execute('select egg from adoptedEggs where user_id = %s', (ctx.author.id,))
  78.     result = cursor.fetchone()
  79.  
  80.     if result is None:
  81.         await ctx.send('You do not have an egg.')
  82.         return
  83.     egg_file_path = os.path.join(egg_directory, result[0])
  84.  
  85.     cursor.execute('select timestamp from adoptedEggs where user_id = %s', (ctx.author.id,))
  86.     result = cursor.fetchone()
  87.     duration = str(datetime.datetime.now() - result[0]).split(' ')[0]
  88.  
  89.     with open(egg_file_path, 'rb') as file:
  90.         egg_image = Image.open(file)
  91.         await ctx.send(f"Your egg is {duration} old" + '\n\n', file=discord.File(file, "your_egg.png"))
  92.  
  93.  
  94.  
  95. @bot.command()
  96. async def showpet(ctx):
  97.     global cursor
  98.  
  99.     if not cnx.is_connected():
  100.         cnx.reconnect()
  101.         cursor = cnx.cursor()
  102.  
  103.     cursor.execute('select pet_name, pet_image from hatchedPets where user_id = %s', (ctx.author.id,))
  104.     result = cursor.fetchone()
  105.  
  106.     if result is None:
  107.         await ctx.send('You do not have a hatched pet.')
  108.         return
  109.     hatched_egg_file_path = result[1]
  110.     with open(hatched_egg_file_path, 'rb') as hatched_file:
  111.         hatched_egg_image = Image.open(hatched_file)
  112.         hatched_file.seek(0)
  113.  
  114.     await ctx.send(f"Your pet: {result[0]}\n\n", file=discord.File(hatched_file, filename="your_pet.png"))
  115.  
  116.  
  117.  
  118. with open('Token.txt') as file:
  119.     token = file.read()
  120.  
  121. bot.run(token)