Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Import necessary libraries
- import discord
- from discord.ext import commands #library for creating commands
- import time #library for time-related functions
- import datetime #library for date and time-related functions
- import requests #library for making HTTP requests
- import random #library for generating random numbers
- import openai #library for accessing the OpenAI API
- #Define values for API tokens and the owner's ID
- DCtoken = ''
- AItoken = ''
- DCtokenTest = ''
- OwnerID = ''
- #Set the OpenAI API key
- openai.api_key = AItoken
- #Set the prefix for bot commands
- prefix = "."
- #List of AI models to use for generating text
- txtmodels=['text-davinci-003']
- #Create default Discord intents and enable the ability to read message content
- intents = discord.Intents.default()
- intents.message_content = True
- #Create the bot with the defined prefix and intents
- bot = commands.Bot(command_prefix=prefix, intents=intents)
- #Remove the default "help" command
- bot.remove_command("help")
- #Create a shortcut to refer to the bot as "client"
- client = bot
- #Class for custom exceptions
- class CustomException(Exception):
- """Exception class"""
- #Event that runs when the bot is ready
- @bot.event
- async def on_ready():
- #Print a message indicating that the bot is ready
- print("Ready")
- #Change the bot's presence to indicate that it is online and display the prefix
- await bot.change_presence(status=discord.Status.online, activity=discord.Game(name="Prefix: '.' / .help"))
- #Command to check the bot's latency
- @bot.command()
- async def ping(ctx):
- #Calculate the bot's latency and round it to one decimal place
- latency = round(bot.latency * 1000, 1)
- #Send a message with the bot's latency
- await ctx.send(f"Pong! {latency})
- @bot.command()
- async def maintenance(ctx):
- #Print the ID of the user who ran the command
- print(ctx.author.id)
- #Check if the user's ID matches the owner's ID
- if ctx.author.id == OwnerID:
- #Create an embed object with a message indicating that maintenance mode has been enabled
- embed = discord.Embed(title='🚧 Maintenance 🚧', description='>Maintenance Mode Enabled!\n>Bot will shutdown soon!')
- #Send the embed object
- msg = await ctx.reply("", embed=embed)
- #Change the bot's presence to indicate that it is in maintenance mode
- await bot.change_presence(status=discord.Status.online, activity=discord.Game(name="🚧 Maintenance 🚧"))
- #Print a message indicating that maintenance mode has been enabled
- print('Maintenance Mode Enabled!')
- #Wait two seconds
- time.sleep(2)
- #Close the bot's connection
- await client.close()
- #Print a message indicating that the bot is offline
- print('Bot Offline...')
- #Quit the program
- quit()
- #If the user's ID does not match the owner's ID
- else:
- #Create an embed object with a message indicating that the user does not have permission to enable maintenance mode
- embed = discord.Embed(title='❗ Error ❗', description='>You dont have permissions to enable Maintenance')
- #Send the embed object
- msg = await ctx.reply("", embed=embed)
- #Command to display a list of commands and their descriptions
- @bot.command()
- async def help(ctx):
- #Create an embed object with a title, a description of the bot's functions, and a list of commands and their descriptions
- embed = discord.Embed(title='🤖 Bot Commands 🤖', description='>This bot can generate text and images using OpenAI GPT-3 models,
- and hold conversations using the DialoGPT model.\n>It also has a few utility commands.\n\n', color=0x00ff00)
- embed.add_field(name='.createtext [prompt]', value='Generates text based on the given prompt.', inline=False)
- embed.add_field(name='.createimage [prompt]', value='Generates an image based on the given prompt.', inline=False)
- embed.add_field(name='.ping', value='Checks the bot's latency.', inline=False)
- embed.add_field(name='.help', value='Displays this message.', inline=False)
- #Send the embed object
- await ctx.send(embed=embed)
- #Command to generate text based on a prompt
- @bot.command()
- async def createtext(ctx, *, prompt):
- #Choose a random model from the list of text generation models
- model = random.choice(txtmodels)
- #Use the chosen model to generate text based on the prompt
- #Use the chosen model to generate text based on the prompt
- response = openai.Completion.create(engine=model, prompt=prompt, max_tokens=2048, temperature=0.5, frequency_penalty=0,
- presence_penalty=0)
- #Get the generated text from the response
- text = response['choices'][0]['text']
- #Send the generated text
- await ctx.send(text)
- #Command to generate an image based on a prompt
- @bot.command()
- async def createimage(ctx, *, prompt):
- #Use the Dall-E model to generate an image based on the prompt
- response = openai.DallE.create(prompt=prompt, size='512x512')
- #Get the URL for the generated image from the response
- image_url = response['data']['url']
- #Send the URL for the generated image
- await ctx.send(image_url)
- bot.run(DCtoken)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement