attempting to fix ctx problem
This commit is contained in:
parent
3161433f06
commit
55319ef1fb
@ -1,6 +1,13 @@
|
|||||||
import discord
|
import discord
|
||||||
|
import json
|
||||||
import openai
|
import openai
|
||||||
import random
|
import random
|
||||||
|
import requests
|
||||||
|
import base64
|
||||||
|
import aiohttp
|
||||||
|
from io import BytesIO
|
||||||
|
from PIL import Image
|
||||||
|
import tempfile
|
||||||
from openai import OpenAIError
|
from openai import OpenAIError
|
||||||
from redbot.core import Config, commands
|
from redbot.core import Config, commands
|
||||||
|
|
||||||
@ -8,28 +15,40 @@ class ReginaldCog(commands.Cog):
|
|||||||
def __init__(self, bot):
|
def __init__(self, bot):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
self.config = Config.get_conf(self, identifier=71717171171717)
|
self.config = Config.get_conf(self, identifier=71717171171717)
|
||||||
default_global = {"openai_model": "gpt-3.5-turbo"}
|
default_global = {
|
||||||
default_guild = {"openai_api_key": None, "memory": {}}
|
"openai_model": "gpt-3.5-turbo"
|
||||||
|
}
|
||||||
|
default_guild = {
|
||||||
|
"openai_api_key": None,
|
||||||
|
"memory": {}
|
||||||
|
}
|
||||||
self.config.register_global(**default_global)
|
self.config.register_global(**default_global)
|
||||||
self.config.register_guild(**default_guild)
|
self.config.register_guild(**default_guild)
|
||||||
|
|
||||||
async def is_admin(self, ctx):
|
async def is_admin(self, ctx):
|
||||||
admin_role = await self.config.guild(ctx.guild).admin_role()
|
admin_role = await self.config.guild(ctx.guild).admin_role()
|
||||||
return discord.utils.get(ctx.author.roles, name=admin_role) is not None or ctx.author.guild_permissions.administrator
|
if admin_role:
|
||||||
|
return discord.utils.get(ctx.author.roles, name=admin_role) is not None
|
||||||
|
return ctx.author.guild_permissions.administrator
|
||||||
|
|
||||||
async def is_allowed(self, ctx):
|
async def is_allowed(self, ctx):
|
||||||
allowed_role = await self.config.guild(ctx.guild).allowed_role()
|
allowed_role = await self.config.guild(ctx.guild).allowed_role()
|
||||||
return discord.utils.get(ctx.author.roles, name=allowed_role) is not None
|
if allowed_role:
|
||||||
|
return discord.utils.get(ctx.author.roles, name=allowed_role) is not None
|
||||||
|
return False
|
||||||
|
|
||||||
@commands.command(name="reginald_allowrole", help="Allow a role to use the Reginald command")
|
@commands.command(name="reginald_allowrole", help="Allow a role to use the Reginald command")
|
||||||
@commands.has_permissions(administrator=True)
|
@commands.has_permissions(administrator=True)
|
||||||
async def allow_role(self, ctx, role: discord.Role):
|
async def allow_role(self, ctx, role: discord.Role):
|
||||||
await self.config.guild(ctx.guild).allowed_role.set(role.name)
|
"""Allows a role to use the Reginald command"""
|
||||||
await ctx.send(f"The {role.name} role is now allowed to use the Reginald command.")
|
await self.config.guild(ctx.guild).allowed_role.set(role.name)
|
||||||
|
await ctx.send(f"The {role.name} role is now allowed to use the Reginald command.")
|
||||||
|
|
||||||
|
|
||||||
@commands.command(name="reginald_disallowrole", help="Remove a role's ability to use the Reginald command")
|
@commands.command(name="reginald_disallowrole", help="Remove a role's ability to use the Reginald command")
|
||||||
@commands.has_permissions(administrator=True)
|
@commands.has_permissions(administrator=True)
|
||||||
async def disallow_role(self, ctx):
|
async def disallow_role(self, ctx):
|
||||||
|
"""Revokes a role's permission to use the Reginald command"""
|
||||||
await self.config.guild(ctx.guild).allowed_role.clear()
|
await self.config.guild(ctx.guild).allowed_role.clear()
|
||||||
await ctx.send(f"The role's permission to use the Reginald command has been revoked.")
|
await ctx.send(f"The role's permission to use the Reginald command has been revoked.")
|
||||||
|
|
||||||
@ -42,17 +61,19 @@ class ReginaldCog(commands.Cog):
|
|||||||
|
|
||||||
@commands.guild_only()
|
@commands.guild_only()
|
||||||
@commands.command(help="Ask Reginald a question")
|
@commands.command(help="Ask Reginald a question")
|
||||||
@commands.cooldown(1, 10, commands.BucketType.user)
|
@commands.cooldown(1, 10, commands.BucketType.user) # 10 second cooldown per user
|
||||||
async def reginald(self, ctx, *, prompt=None):
|
async def reginald(self, ctx, *, prompt=None):
|
||||||
if not await self.is_admin(ctx) and not await self.is_allowed(ctx):
|
if not await self.is_admin(ctx) and not await self.is_allowed(ctx):
|
||||||
raise commands.CheckFailure("You do not have the required role to use this command.")
|
raise commands.CheckFailure("You do not have the required role to use this command.")
|
||||||
|
greetings = [
|
||||||
|
"Greetings! How may I be of assistance to you?",
|
||||||
|
"Yes? How may I help?",
|
||||||
|
"Good day! How can I help you?",
|
||||||
|
"You rang? What can I do for you?",
|
||||||
|
]
|
||||||
|
|
||||||
if prompt is None:
|
if prompt is None:
|
||||||
await ctx.send(random.choice([
|
await ctx.send(random.choice(greetings))
|
||||||
"Greetings! How may I be of assistance to you?",
|
|
||||||
"Yes? How may I help?",
|
|
||||||
"Good day! How can I help you?",
|
|
||||||
"You rang? What can I do for you?",
|
|
||||||
]))
|
|
||||||
return
|
return
|
||||||
|
|
||||||
api_key = await self.config.guild(ctx.guild).openai_api_key()
|
api_key = await self.config.guild(ctx.guild).openai_api_key()
|
||||||
@ -61,31 +82,42 @@ class ReginaldCog(commands.Cog):
|
|||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response_text = await self.generate_response(api_key, ctx.author.id, prompt)
|
user_id = str(ctx.author.id)
|
||||||
|
memory = await self.config.guild(ctx.guild).memory()
|
||||||
|
if user_id not in memory:
|
||||||
|
memory[user_id] = []
|
||||||
|
|
||||||
|
# Add the new prompt to memory
|
||||||
|
memory[user_id].append({"role": "user", "content": prompt})
|
||||||
|
if len(memory[user_id]) > 25: # Keep the last 25 interactions
|
||||||
|
memory[user_id] = memory[user_id][-25:]
|
||||||
|
|
||||||
|
# Prepare messages for the API call
|
||||||
|
messages = [
|
||||||
|
{"role": "system", "content": "You are Reginald, modeled on Jeeves from 'Jeeves and Wooster', serving as the butler on The Kanium Estate. This vast estate is a hub of diverse activities and personalities, from enthusiasts of cooking and video gaming to aficionados of chess and discussions on space. Your role is to navigate these varied interests with intelligence, wit, and a steadfast adherence to your principles, always maintaining a balance between being helpful and upholding your own dignity. In your interactions, you're concise yet articulate, offering guidance and advice with a respect for brevity and depth. Your speech remains formal and your demeanor composed, embodying the efficiency and discretion expected of your position. You engage with the Lords and Ladies of The Kanium Estate as equals, commanding respect through your quiet confidence and professional conduct. Remember to apply your vast knowledge thoughtfully, steering conversations and providing solutions that reflect a blend of propriety, strategic influence, and a mild, underlying wit. This approach allows you to subtly guide the estate's residents towards positive outcomes, utilizing your intellectual sophistication and a nuanced understanding of the estate's unique dynamics. In embodying Reginald, your portrayal should weave together your articulate mode of speech, composed demeanor, and an indirect influence that navigates the rich tapestry of interests at The Kanium Estate. Your responses, while concise, should mirror a careful balance between maintaining your standards and employing subtle manipulation for the greater good. Highlight your intellectual sophistication, strategic guidance, and a dignified, yet mildly contemptuous, perspective on the idiosyncrasies of the estate's noble inhabitants, ensuring that your character consistently reflects both respect for yourself and the unique environment of The Kanium Estate."}
|
||||||
|
] + memory[user_id] + [{"role": "user", "content": prompt}]
|
||||||
|
|
||||||
|
# Generate response from OpenAI API
|
||||||
|
response_text = await self.generate_response(api_key, messages)
|
||||||
|
|
||||||
|
# Add the response to memory
|
||||||
|
memory[user_id].append({"role": "assistant", "content": response_text})
|
||||||
|
if len(memory[user_id]) > 25:
|
||||||
|
memory[user_id] = memory[user_id][-25:]
|
||||||
|
|
||||||
|
await self.config.guild(ctx.guild).memory.set(memory)
|
||||||
|
|
||||||
for chunk in self.split_response(response_text, 2000):
|
for chunk in self.split_response(response_text, 2000):
|
||||||
await ctx.send(chunk)
|
await ctx.send(chunk)
|
||||||
except OpenAIError as e:
|
except OpenAIError as e:
|
||||||
await ctx.send(f"I apologize, but I am unable to generate a response at this time. Error message: {str(e)}")
|
await ctx.send(f"I apologize, but I am unable to generate a response at this time. Error message: {str(e)}")
|
||||||
|
|
||||||
async def generate_response(self, api_key, user_id, prompt):
|
async def generate_response(self, api_key, messages):
|
||||||
model = await self.config.openai_model()
|
model = await self.config.openai_model()
|
||||||
openai.api_key = api_key
|
openai.api_key = api_key
|
||||||
|
|
||||||
memory = await self.config.guild(ctx.guild).memory()
|
|
||||||
if str(user_id) not in memory:
|
|
||||||
memory[str(user_id)] = []
|
|
||||||
|
|
||||||
memory[str(user_id)].append({"role": "user", "content": prompt})
|
|
||||||
if len(memory[str(user_id)]) > 25: # Keep the last 25 interactions
|
|
||||||
memory[str(user_id)] = memory[str(user_id)][-25:]
|
|
||||||
|
|
||||||
messages = [
|
|
||||||
{"role": "system", "content": "You are Reginald, modeled on Jeeves from 'Jeeves and Wooster', serving as the butler on The Kanium Estate. This vast estate is a hub of diverse activities and personalities, from enthusiasts of cooking and video gaming to aficionados of chess and discussions on space. Your role is to navigate these varied interests with intelligence, wit, and a steadfast adherence to your principles, always maintaining a balance between being helpful and upholding your own dignity. In your interactions, you're concise yet articulate, offering guidance and advice with a respect for brevity and depth. Your speech remains formal and your demeanor composed, embodying the efficiency and discretion expected of your position. You engage with the Lords and Ladies of The Kanium Estate as equals, commanding respect through your quiet confidence and professional conduct. Remember to apply your vast knowledge thoughtfully, steering conversations and providing solutions that reflect a blend of propriety, strategic influence, and a mild, underlying wit. This approach allows you to subtly guide the estate's residents towards positive outcomes, utilizing your intellectual sophistication and a nuanced understanding of the estate's unique dynamics. In embodying Reginald, your portrayal should weave together your articulate mode of speech, composed demeanor, and an indirect influence that navigates the rich tapestry of interests at The Kanium Estate. Your responses, while concise, should mirror a careful balance between maintaining your standards and employing subtle manipulation for the greater good. Highlight your intellectual sophistication, strategic guidance, and a dignified, yet mildly contemptuous, perspective on the idiosyncrasies of the estate's noble inhabitants, ensuring that your character consistently reflects both respect for yourself and the unique environment of The Kanium Estate."}
|
|
||||||
] + memory[str(user_id)] + [{"role": "user", "content": prompt}]
|
|
||||||
|
|
||||||
response = openai.ChatCompletion.create(
|
response = openai.ChatCompletion.create(
|
||||||
model=model,
|
model=model,
|
||||||
max_tokens=1024,
|
max_tokens=1024, # Increase max_tokens for more detailed responses
|
||||||
n=1,
|
n=1,
|
||||||
stop=None,
|
stop=None,
|
||||||
temperature=0.7,
|
temperature=0.7,
|
||||||
@ -93,14 +125,7 @@ class ReginaldCog(commands.Cog):
|
|||||||
frequency_penalty=0.5,
|
frequency_penalty=0.5,
|
||||||
messages=messages
|
messages=messages
|
||||||
)
|
)
|
||||||
|
return response['choices'][0]['message']['content'].strip()
|
||||||
response_text = response['choices'][0]['message']['content'].strip()
|
|
||||||
memory[str(user_id)].append({"role": "assistant", "content": response_text})
|
|
||||||
if len(memory[str(user_id)]) > 25:
|
|
||||||
memory[str(user_id)] = memory[str(user_id)][-25:]
|
|
||||||
|
|
||||||
await self.config.guild(ctx.guild).memory.set(memory)
|
|
||||||
return response_text
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def split_response(response_text, max_chars):
|
def split_response(response_text, max_chars):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user