Skip to content
This repository was archived by the owner on Feb 7, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions cogs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import psutil
import math
import openai
import framework.isobot.embedengine
from discord import option, ApplicationContext
from discord.ext import commands
Expand All @@ -14,6 +15,8 @@

# Variables
color = discord.Color.random()
openai.api_key = os.getenv("chatgpt_API_KEY")
chatgpt_conversation = dict()

# Commands
class Utils(commands.Cog):
Expand Down Expand Up @@ -117,6 +120,29 @@ async def status(self, ctx: ApplicationContext):
localembed.add_field(name="Release Notes", value="[latest](https://github.com/PyBotDevs/isobot/releases/latest)")
localembed.set_footer(text=f"Requested by {ctx.author.name}", icon_url=ctx.author.avatar_url)
await ctx.respond(embed=localembed)

@commands.slash_command(
name="chatgpt",
description="Talk to ChatGPT and get a response back."
)
@option(name="message", description="What do you want to send to ChatGPT?", type=str)
@commands.cooldown(1, 1, commands.BucketType.user)
async def chatgpt(self, ctx: ApplicationContext, message: str):
if str(ctx.author.id) not in chatgpt_conversation: chatgpt_conversation[str(ctx.author.id)] = [{"role": "system", "content": "You are a intelligent assistant."}]
await ctx.defer()
try:
chatgpt_conversation[str(ctx.author.id)].append({"role": "user", "content": message})
_chat = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=chatgpt_conversation[str(ctx.author.id)])
_reply = _chat.choices[0].message.content
chatgpt_conversation[str(ctx.author.id)].append({"role": "assistant", "content": _reply})
except openai.error.RateLimitError: return await ctx.respond("The OpenAI API is currently being rate-limited. Try again after some time.", ephemeral=True)
except openai.error.ServiceUnavailableError: return await ctx.respond("The ChatGPT service is currently unavailable.\nTry again after some time, or check it's status at https://status.openai.com", ephemeral=True)
except openai.error.APIError: return await ctx.respond("ChatGPT encountered an internal error. Please try again.", ephemeral=True)
except openai.error.Timeout: return await ctx.respond("Your request timed out. Please try again, or wait for a while.", ephemeral=True)
localembed = discord.Embed(description=f"{_reply}", color=discord.Color.random())
localembed.set_author(name="ChatGPT", icon_url="https://upload.wikimedia.org/wikipedia/commons/thumb/0/04/ChatGPT_logo.svg/1200px-ChatGPT_logo.svg.png")
localembed.set_footer(text="Powered by OpenAI")
await ctx.respond(embed=localembed)

# Cog Initialization
def setup(bot): bot.add_cog(Utils(bot))
10 changes: 10 additions & 0 deletions config/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -688,5 +688,15 @@
"usable_by": "everyone",
"disabled": false,
"bugged": false
},
"chatgpt": {
"name": "ChatGPT",
"description": "Talk to ChatGPT and get a response back.",
"type": "general utilities",
"cooldown": 1,
"args": ["message"],
"usable_by": "everyone",
"disabled": false,
"bugged": false
}
}