adding api key set command

This commit is contained in:
unknown 2023-03-18 17:52:59 +01:00
parent 25f28cc24d
commit a99f5c4fb9

View File

@ -5,15 +5,17 @@ import openai
class ReginaldGptCog(commands.Cog): class ReginaldGptCog(commands.Cog):
def __init__(self, bot): def __init__(self, bot):
self.bot = bot
self.bot = bot self.bot = bot
self.config = Config.get_conf(self, identifier=71717171171717) self.config = Config.get_conf(self, identifier=71717171171717)
openai.api_key = os.environ["OPENAI_API_KEY"] self.config.register_global(api_key=None)
@commands.guild_only() @commands.guild_only()
@checks.admin_or_permissions(manage_guild=True) @checks.admin_or_permissions(manage_guild=True)
async def reginaldgpt(self, ctx, *, message): async def reginaldgpt(self, ctx, *, message):
try: try:
api_key = await self.config.api_key()
openai.api_key = api_key
response = openai.ChatCompletion.create( response = openai.ChatCompletion.create(
model="gpt-3.5-turbo", model="gpt-3.5-turbo",
messages=[ messages=[
@ -33,6 +35,14 @@ class ReginaldGptCog(commands.Cog):
except Exception as e: except Exception as e:
await ctx.send("As an AI robot, I errored out.") await ctx.send("As an AI robot, I errored out.")
@commands.guild_only()
@checks.admin_or_permissions(manage_guild=True)
@commands.command()
async def setreginaldgptapikey(self, ctx, api_key):
await self.config.api_key.set(api_key)
openai.api_key = api_key
await ctx.send("ReginaldGpt API key has been set.")
def setup(bot): def setup(bot):
cog = ReginaldGptCog(bot) cog = ReginaldGptCog(bot)
bot.add_cog(cog) bot.add_cog(cog)