|
| 1 | +import os |
| 2 | +import time |
| 3 | +from os.path import join, dirname |
| 4 | + |
| 5 | +import discord |
| 6 | +from discord.ext import commands as dcmd |
| 7 | +from dotenv import load_dotenv |
| 8 | + |
| 9 | +import teapot |
| 10 | + |
| 11 | +print(""" |
| 12 | + _____ _ |
| 13 | + |_ _|__ __ _ _ __ ___ | |_ |
| 14 | + | |/ _ \/ _` | '_ \ / _ \| __| |
| 15 | + | | __/ (_| | |_) | (_) | |_ |
| 16 | + |_|\___|\__,_| .__/ \___/ \__| |
| 17 | + by ColaIan |_| & RedTea |
| 18 | +""") |
| 19 | + |
| 20 | +load_dotenv(join(dirname(__file__), '.env')) |
| 21 | + |
| 22 | +if os.getenv('CONFIG_VERSION') != teapot.config_version(): |
| 23 | + if os.path.isfile('.env'): |
| 24 | + print("Missing environment variables. Please delete .env and run Teapot.py again.") |
| 25 | + quit() |
| 26 | + print("Unable to find required environment variables. Running setup.py...") |
| 27 | + teapot.setup.__init__() |
| 28 | + |
| 29 | +print("Initializing bot...") |
| 30 | +if teapot.config.storage_type() == "mysql": |
| 31 | + time_start = time.perf_counter() |
| 32 | + database = teapot.database.__init__() |
| 33 | + db = teapot.database.db(database) |
| 34 | + db.execute('CREATE TABLE IF NOT EXISTS `guilds` (`guild_id` BIGINT, `guild_name` TINYTEXT)') |
| 35 | + db.execute('CREATE TABLE IF NOT EXISTS `channels` (`channel_id` BIGINT, `channel_name` TINYTEXT)') |
| 36 | + db.execute("CREATE TABLE IF NOT EXISTS `users` (`user_id` BIGINT, `user_name` TINYTEXT, `user_discriminator` INT)") |
| 37 | + print(f"Connected to database ({teapot.config.db_host()}) in {round(time.perf_counter() - time_start, 2)}s") |
| 38 | + |
| 39 | +bot = dcmd.Bot(command_prefix=teapot.config.bot_prefix()) |
| 40 | + |
| 41 | + |
| 42 | +@bot.event |
| 43 | +async def on_ready(): |
| 44 | + print(f"Connected to DiscordAPI in {round(time.perf_counter() - discord_time_start, 2)}s") |
| 45 | + time_start = time.perf_counter() |
| 46 | + teapot.events.__init__(bot) |
| 47 | + teapot.cogs.cmds.__init__(bot) |
| 48 | + teapot.cogs.music.setup(bot) |
| 49 | + teapot.cogs.osu.setup(bot) |
| 50 | + teapot.cogs.github.setup(bot) |
| 51 | + teapot.cogs.cat.setup(bot) |
| 52 | + teapot.cogs.neko.setup(bot) |
| 53 | + if teapot.config.storage_type() == "mysql": |
| 54 | + for guild in bot.guilds: |
| 55 | + db.execute("SELECT * FROM `guilds` WHERE guild_id = '" + str(guild.id) + "'") |
| 56 | + if db.rowcount == 0: |
| 57 | + db.execute("INSERT INTO `guilds`(guild_id, guild_name) VALUES(%s, %s)", (guild.id, guild.name)) |
| 58 | + database.commit() |
| 59 | + db.execute("CREATE TABLE IF NOT EXISTS `" + str( |
| 60 | + guild.id) + "_logs" + "` (`timestamp` TEXT, `guild_id` BIGINT, `channel_id` BIGINT, `message_id` " |
| 61 | + "BIGINT, `user_id` BIGINT, `action_type` TINYTEXT, `message` MEDIUMTEXT)") |
| 62 | + elif teapot.config.storage_type() == "flatfile": |
| 63 | + print("[!] Flatfile storage has not been implemented yet. MySQL database is recommended") |
| 64 | + print(f"Registered commands and events in {round(time.perf_counter() - time_start, 2)}s") |
| 65 | + await bot.change_presence(status=discord.Status.online, activity=discord.Game(teapot.config.bot_status())) |
| 66 | + |
| 67 | + |
| 68 | +@bot.event |
| 69 | +async def on_message(message): |
| 70 | + guild = message.guild |
| 71 | + if teapot.config.storage_type() == "mysql": |
| 72 | + try: |
| 73 | + db.execute("SELECT * FROM `users` WHERE user_id = '" + str(message.author.id) + "'") |
| 74 | + if db.rowcount == 0: |
| 75 | + db.execute("INSERT INTO `users`(user_id, user_name, user_discriminator) VALUES(%s, %s, %s)", |
| 76 | + (message.author.id, message.author.name, message.author.discriminator.zfill(4))) |
| 77 | + database.commit() |
| 78 | + db.execute("SELECT * FROM `channels` WHERE channel_id = '" + str(message.channel.id) + "'") |
| 79 | + if db.rowcount == 0: |
| 80 | + db.execute("INSERT INTO `channels`(channel_id, channel_name) VALUES(%s, %s)", |
| 81 | + (message.channel.id, message.channel.name)) |
| 82 | + database.commit() |
| 83 | + db.execute("INSERT INTO `" + str( |
| 84 | + guild.id) + "_logs" + "`(timestamp, guild_id, channel_id, message_id, user_id, action_type, message) VALUES(%s, %s, %s, %s, %s, %s, %s)", |
| 85 | + (teapot.time(), message.guild.id, message.channel.id, message.id, message.author.id, |
| 86 | + "MESSAGE_SEND", message.content)) |
| 87 | + database.commit() |
| 88 | + except Exception as e: |
| 89 | + print(e) |
| 90 | + await bot.process_commands(message) |
| 91 | + |
| 92 | + |
| 93 | +try: |
| 94 | + discord_time_start = time.perf_counter() |
| 95 | + bot.run(teapot.config.bot_token()) |
| 96 | +except Exception as e: |
| 97 | + print(e) |
| 98 | + print("[/!\] Failed to connect to DiscordAPI. Please check your bot token!") |
0 commit comments