import discord from discord.ext import commands import os import random import mysql.connector from PIL import Image import datetime bot = commands.Bot(command_prefix='?', intents=discord.Intents.all()) db = { 'host': '127.0.0.1', 'user': 'root', 'password': 'Deathisnotaword1904!', 'database': 'TheBlessedMachine' } cursor = None try: cnx = mysql.connector.connect(**db) cursor = cnx.cursor() print("Connection to MySQL DB successful") except mysql.connector.Error as err: print(f"Error connecting to MySQL: {err}") @bot.event async def on_ready(): print('Bot is ready') @bot.event async def on_disconnect(): global cursor if cnx.is_connected(): cursor.close() cnx.close() @bot.command() async def hello(ctx): await ctx.send('Hi') @bot.command() async def adopt(ctx): global cursor global egg_file_path global directory directory= 'C:/Users/Admin/The Blessed Machine/Eggs' if not cnx.is_connected(): cnx.reconnect() cursor = cnx.cursor() cursor.execute('select count(*) from adoptedEggs where user_id = %s', (ctx.author.id,)) result = cursor.fetchone() if result[0] >= 1: await ctx.send('You already have an egg!') return egg = random.choice(os.listdir(directory)) egg_file_path = os.path.join(directory, egg) with open(egg_file_path, 'rb') as file: egg_image = Image.open(file) file.seek(0) await ctx.send(file=discord.File(file, "random_egg.png")) cursor.execute('insert into adoptedEggs (user_id, egg, timestamp) values (%s, %s, NOW())', (ctx.author.id, egg)) cnx.commit() @bot.command() async def myegg(ctx): global cursor global egg_directory if not cnx.is_connected(): cnx.reconnect() cursor = cnx.cursor() cursor.execute('select egg from adoptedEggs where user_id = %s', (ctx.author.id,)) result = cursor.fetchone() if result is None: await ctx.send('You do not have an egg.') return egg_file_path = os.path.join(egg_directory, result[0]) cursor.execute('select timestamp from adoptedEggs where user_id = %s', (ctx.author.id,)) result = cursor.fetchone() duration = str(datetime.datetime.now() - result[0]).split(' ')[0] with open(egg_file_path, 'rb') as file: egg_image = Image.open(file) await ctx.send(f"Your egg is {duration} old" + '\n\n', file=discord.File(file, "your_egg.png")) @bot.command() async def showpet(ctx): global cursor if not cnx.is_connected(): cnx.reconnect() cursor = cnx.cursor() cursor.execute('select pet_name, pet_image from hatchedPets where user_id = %s', (ctx.author.id,)) result = cursor.fetchone() if result is None: await ctx.send('You do not have a hatched pet.') return hatched_egg_file_path = result[1] with open(hatched_egg_file_path, 'rb') as hatched_file: hatched_egg_image = Image.open(hatched_file) hatched_file.seek(0) await ctx.send(f"Your pet: {result[0]}\n\n", file=discord.File(hatched_file, filename="your_pet.png")) with open('Token.txt') as file: token = file.read() bot.run(token)