from genericpath import exists import random import subprocess import json import discord from discord import app_commands from chatgpt import ChatGPT with open('config.json', 'r') as f: data = json.load(f) if exists('config.local.json'): with open('config.local.json') as f: data = json.load(f) intents = discord.Intents.default() intents.message_content = True client = discord.Client(intents=intents) tree = app_commands.CommandTree(client) description = '''Bot to end all bots''' # bot = commands.Bot(command_prefix='?', description=description, intents=intents) @client.event async def on_ready(): await tree.sync(guild=discord.Object(id='745287426402156555')) print(f'Logged in as {client.user}') print('------') if data['update'] == 'pending': await client.get_channel(1173561093579362305).send('Update done') write_json('update', 'none') @tree.command(name = 'cool', description = 'Check how cool you are!', guild=discord.Object(id='745287426402156555')) async def cool(ctx: discord.Interaction): cool_factor = round(random.uniform(0, 100), 2) await ctx.response.send_message(f'{ctx.user.display_name} is {cool_factor}% cool!') @tree.command(name = 'update', description = 'Pulls the latest version from the repository', guild=discord.Object(id='745287426402156555')) @app_commands.checks.has_role(745295653651087381) async def update(ctx: discord.Interaction): await ctx.response.send_message('Updating, see you on the other side') write_json('update', 'pending') subprocess.run('./update.sh') @tree.command(name = 'ask', description = 'Query anything, powered by GPT-3.5', guild=discord.Object(id='745287426402156555')) async def ask(ctx: discord.Interaction, query: str): await ctx.response.defer(thinking=True) gpt = ChatGPT(data['openAI']) response = gpt.completion(query) await ctx.followup.send(response) @tree.command(name = 'image', description = 'Generate an image from a text prompt, powered by DALL-E-3', guild=discord.Object(id='745287426402156555')) async def image(ctx: discord.Interaction, prompt: str): await ctx.response.defer(thinking=True) gpt = ChatGPT(data['openAI']) response = gpt.image(prompt) await ctx.followup.send(response) @update.error async def update_error(ctx: discord.Interaction, error): if isinstance(error, discord.app_commands.MissingRole): text = "Sorry {}, you do not have permissions to do that!".format(ctx.user) await ctx.response.send_message(text) def write_json(key: str, value: any): data[key] = value if exists('config.local.json'): with open('config.local.json', 'w') as f: f.write(json.dumps(data)) elif exists('config.json'): with open('config.json', 'w') as f: f.write(json.dumps(data)) client.run(data['token'])