Advertisement
tommarek_CZE

Exaple 1 Offsite Application

Jan 9th, 2023
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.21 KB | None | 0 0
  1. #Import necessary libraries
  2. import discord
  3. from discord.ext import commands #library for creating commands
  4. import time #library for time-related functions
  5. import datetime #library for date and time-related functions
  6. import requests #library for making HTTP requests
  7. import random #library for generating random numbers
  8. import openai #library for accessing the OpenAI API
  9.  
  10. #Define values for API tokens and the owner's ID
  11. DCtoken = ''
  12. AItoken = ''
  13. DCtokenTest = ''
  14. OwnerID = ''
  15.  
  16. #Set the OpenAI API key
  17. openai.api_key = AItoken
  18.  
  19. #Set the prefix for bot commands
  20. prefix = "."
  21.  
  22. #List of AI models to use for generating text
  23. txtmodels=['text-davinci-003']
  24.  
  25. #Create default Discord intents and enable the ability to read message content
  26. intents = discord.Intents.default()
  27. intents.message_content = True
  28.  
  29. #Create the bot with the defined prefix and intents
  30. bot = commands.Bot(command_prefix=prefix, intents=intents)
  31.  
  32. #Remove the default "help" command
  33. bot.remove_command("help")
  34.  
  35. #Create a shortcut to refer to the bot as "client"
  36. client = bot
  37.  
  38. #Class for custom exceptions
  39. class CustomException(Exception):
  40.     """Exception class"""
  41.  
  42. #Event that runs when the bot is ready
  43. @bot.event
  44. async def on_ready():
  45.     #Print a message indicating that the bot is ready
  46.     print("Ready")
  47.  
  48.     #Change the bot's presence to indicate that it is online and display the prefix
  49.     await bot.change_presence(status=discord.Status.online, activity=discord.Game(name="Prefix: '.' / .help"))
  50.  
  51. #Command to check the bot's latency
  52. @bot.command()
  53. async def ping(ctx):
  54.     #Calculate the bot's latency and round it to one decimal place
  55.     latency = round(bot.latency * 1000, 1)
  56.  
  57.     #Send a message with the bot's latency
  58.     await ctx.send(f"Pong! {latency})
  59.                
  60. @bot.command()
  61. async def maintenance(ctx):
  62.    #Print the ID of the user who ran the command
  63.    print(ctx.author.id)
  64.    #Check if the user's ID matches the owner's ID
  65.    if ctx.author.id == OwnerID:
  66.        #Create an embed object with a message indicating that maintenance mode has been enabled
  67.        embed = discord.Embed(title='🚧 Maintenance 🚧', description='>Maintenance Mode Enabled!\n>Bot will shutdown soon!')
  68.  
  69.        #Send the embed object
  70.        msg = await ctx.reply("", embed=embed)
  71.  
  72.        #Change the bot's presence to indicate that it is in maintenance mode
  73.        await bot.change_presence(status=discord.Status.online, activity=discord.Game(name="🚧 Maintenance 🚧"))
  74.  
  75.        #Print a message indicating that maintenance mode has been enabled
  76.        print('Maintenance Mode Enabled!')
  77.  
  78.        #Wait two seconds
  79.        time.sleep(2)
  80.  
  81.        #Close the bot's connection
  82.        await client.close()
  83.  
  84.        #Print a message indicating that the bot is offline
  85.        print('Bot Offline...')
  86.  
  87.        #Quit the program
  88.        quit()
  89.    #If the user's ID does not match the owner's ID
  90.    else:
  91.        #Create an embed object with a message indicating that the user does not have permission to enable maintenance mode
  92.        embed = discord.Embed(title='❗ Error ❗', description='>You dont have permissions to enable Maintenance')
  93.  
  94.        #Send the embed object
  95.        msg = await ctx.reply("", embed=embed)
  96.  
  97.    #Command to display a list of commands and their descriptions
  98. @bot.command()
  99. async def help(ctx):
  100.    #Create an embed object with a title, a description of the bot's functions, and a list of commands and their descriptions
  101.     embed = discord.Embed(title='🤖 Bot Commands 🤖', description='>This bot can generate text and images using OpenAI GPT-3 models,             
  102.    and hold conversations using the DialoGPT model.\n>It also has a few utility commands.\n\n', color=0x00ff00)
  103.    embed.add_field(name='.createtext [prompt]', value='Generates text based on the given prompt.', inline=False)
  104.    embed.add_field(name='.createimage [prompt]', value='Generates an image based on the given prompt.', inline=False)
  105.    embed.add_field(name='.ping', value='Checks the bot's latency.', inline=False)
  106.    embed.add_field(name='.help', value='Displays this message.', inline=False)
  107.    #Send the embed object
  108.    await ctx.send(embed=embed)
  109.  
  110. #Command to generate text based on a prompt
  111. @bot.command()
  112. async def createtext(ctx, *, prompt):
  113.    #Choose a random model from the list of text generation models
  114.    model = random.choice(txtmodels)
  115.    #Use the chosen model to generate text based on the prompt
  116.    #Use the chosen model to generate text based on the prompt
  117.     response = openai.Completion.create(engine=model, prompt=prompt, max_tokens=2048, temperature=0.5, frequency_penalty=0,                  
  118.    presence_penalty=0)
  119.  
  120.    #Get the generated text from the response
  121.    text = response['choices'][0]['text']
  122.  
  123.    #Send the generated text
  124.    await ctx.send(text)
  125.  
  126. #Command to generate an image based on a prompt
  127. @bot.command()
  128. async def createimage(ctx, *, prompt):
  129.    #Use the Dall-E model to generate an image based on the prompt
  130.    response = openai.DallE.create(prompt=prompt, size='512x512')
  131.  
  132.    #Get the URL for the generated image from the response
  133.    image_url = response['data']['url']
  134.  
  135.    #Send the URL for the generated image
  136.    await ctx.send(image_url)
  137.  
  138. bot.run(DCtoken)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement