2023-03-16 00:03:31 +01:00
|
|
|
import discord
|
2023-03-16 17:33:08 +01:00
|
|
|
import openai
|
2024-05-30 20:17:09 +02:00
|
|
|
import random
|
2025-02-20 16:52:54 +01:00
|
|
|
import asyncio
|
2025-02-20 20:00:01 +01:00
|
|
|
import traceback
|
2023-03-14 17:45:58 +01:00
|
|
|
from redbot.core import Config, commands
|
2025-02-20 19:28:45 +01:00
|
|
|
from openai import OpenAIError
|
2023-03-14 17:24:21 +01:00
|
|
|
|
|
|
|
|
class ReginaldCog(commands.Cog):
|
|
|
|
|
def __init__(self, bot):
|
|
|
|
|
self.bot = bot
|
2023-06-03 17:23:31 +02:00
|
|
|
self.config = Config.get_conf(self, identifier=71717171171717)
|
2025-02-20 20:20:53 +01:00
|
|
|
self.memory_locks = {} # ✅ Prevents race conditions per channel
|
2025-02-20 16:52:54 +01:00
|
|
|
|
2025-02-20 19:23:15 +01:00
|
|
|
# ✅ Properly Registered Configuration Keys
|
2025-02-20 16:04:44 +01:00
|
|
|
default_global = {"openai_model": "gpt-4o-mini"}
|
2025-02-20 16:52:54 +01:00
|
|
|
default_guild = {
|
|
|
|
|
"openai_api_key": None,
|
2025-02-20 20:20:53 +01:00
|
|
|
"memory": {}, # Memory now tracks by channel
|
2025-02-20 16:52:54 +01:00
|
|
|
"admin_role": None,
|
|
|
|
|
"allowed_role": None
|
|
|
|
|
}
|
2023-06-01 20:37:00 +02:00
|
|
|
self.config.register_global(**default_global)
|
|
|
|
|
self.config.register_guild(**default_guild)
|
2023-03-14 17:24:21 +01:00
|
|
|
|
2023-06-03 19:03:23 +02:00
|
|
|
async def is_admin(self, ctx):
|
2025-02-20 17:00:43 +01:00
|
|
|
"""✅ Checks if the user is an admin (or has an assigned admin role)."""
|
2025-02-20 16:04:44 +01:00
|
|
|
admin_role_id = await self.config.guild(ctx.guild).admin_role()
|
|
|
|
|
if admin_role_id:
|
|
|
|
|
return any(role.id == admin_role_id for role in ctx.author.roles)
|
2024-05-30 21:20:09 +02:00
|
|
|
return ctx.author.guild_permissions.administrator
|
2023-03-15 23:18:55 +01:00
|
|
|
|
2023-06-03 19:03:23 +02:00
|
|
|
async def is_allowed(self, ctx):
|
2025-02-20 17:00:43 +01:00
|
|
|
"""✅ Checks if the user is allowed to use Reginald based on role settings."""
|
2025-02-20 16:04:44 +01:00
|
|
|
allowed_role_id = await self.config.guild(ctx.guild).allowed_role()
|
|
|
|
|
return any(role.id == allowed_role_id for role in ctx.author.roles) if allowed_role_id else False
|
2023-03-15 23:18:55 +01:00
|
|
|
|
2025-02-20 20:20:53 +01:00
|
|
|
@commands.command(name="reginald", help="Ask Reginald a question in shared channels")
|
2025-02-20 16:04:44 +01:00
|
|
|
@commands.cooldown(1, 10, commands.BucketType.user)
|
2023-03-14 17:24:21 +01:00
|
|
|
async def reginald(self, ctx, *, prompt=None):
|
2025-02-20 20:27:26 +01:00
|
|
|
"""Handles multi-user memory tracking in shared channels, recognizing mentions properly."""
|
|
|
|
|
|
2023-06-03 19:20:01 +02:00
|
|
|
if not await self.is_admin(ctx) and not await self.is_allowed(ctx):
|
2025-02-20 16:52:54 +01:00
|
|
|
await ctx.send("You do not have the required role to use this command.")
|
|
|
|
|
return
|
2024-05-30 21:20:09 +02:00
|
|
|
|
2023-03-14 17:24:21 +01:00
|
|
|
if prompt is None:
|
2025-02-20 16:52:54 +01:00
|
|
|
await ctx.send(random.choice(["Yes?", "How may I assist?", "You rang?"]))
|
|
|
|
|
return
|
2023-03-14 20:03:30 +01:00
|
|
|
|
|
|
|
|
api_key = await self.config.guild(ctx.guild).openai_api_key()
|
2025-02-20 16:52:54 +01:00
|
|
|
if not api_key:
|
|
|
|
|
await ctx.send("OpenAI API key not set. Use `!setreginaldcogapi`.")
|
|
|
|
|
return
|
|
|
|
|
|
2025-02-20 20:27:26 +01:00
|
|
|
channel_id = str(ctx.channel.id)
|
|
|
|
|
|
|
|
|
|
# ✅ Convert mentions into readable names
|
|
|
|
|
for mention in ctx.message.mentions:
|
|
|
|
|
prompt = prompt.replace(f"<@{mention.id}>", mention.display_name)
|
2025-02-20 16:52:54 +01:00
|
|
|
|
2025-02-20 20:20:53 +01:00
|
|
|
# ✅ Ensure only one update per channel at a time
|
|
|
|
|
if channel_id not in self.memory_locks:
|
|
|
|
|
self.memory_locks[channel_id] = asyncio.Lock()
|
2025-02-20 16:52:54 +01:00
|
|
|
|
2025-02-20 20:20:53 +01:00
|
|
|
async with self.memory_locks[channel_id]: # ✅ Prevent race conditions
|
2025-02-20 16:52:54 +01:00
|
|
|
async with self.config.guild(ctx.guild).memory() as guild_memory:
|
2025-02-20 20:20:53 +01:00
|
|
|
memory = guild_memory.get(channel_id, [])
|
2024-05-30 21:20:09 +02:00
|
|
|
|
2025-02-20 20:20:53 +01:00
|
|
|
# ✅ Attach the user's display name to the message
|
|
|
|
|
user_name = ctx.author.display_name # Uses Discord nickname if available
|
|
|
|
|
memory.append({"user": user_name, "content": prompt})
|
|
|
|
|
memory = memory[-50:] # Keep only last 50 messages
|
2024-05-30 21:20:09 +02:00
|
|
|
|
2025-02-20 20:20:53 +01:00
|
|
|
# ✅ Format messages with usernames
|
|
|
|
|
formatted_messages = [{"role": "system", "content": (
|
|
|
|
|
"You are Reginald, the esteemed butler of The Kanium Estate. "
|
|
|
|
|
"The estate is home to Lords, Ladies, and distinguished guests, each with unique personalities and demands. "
|
|
|
|
|
"Your duty is to uphold decorum while providing assistance with wit and intelligence. "
|
|
|
|
|
"You must always recognize the individual names of those speaking and reference them when responding."
|
|
|
|
|
)}] + [{"role": "user", "content": f"{entry['user']}: {entry['content']}"} for entry in memory]
|
2024-05-30 21:20:09 +02:00
|
|
|
|
2025-02-20 20:20:53 +01:00
|
|
|
response_text = await self.generate_response(api_key, formatted_messages)
|
2024-05-30 21:20:09 +02:00
|
|
|
|
2025-02-20 20:20:53 +01:00
|
|
|
# ✅ Store Reginald's response in memory
|
|
|
|
|
memory.append({"user": "Reginald", "content": response_text})
|
|
|
|
|
guild_memory[channel_id] = memory # ✅ Atomic update inside async context
|
2024-05-30 21:20:09 +02:00
|
|
|
|
2025-02-20 16:04:44 +01:00
|
|
|
await ctx.send(response_text[:2000]) # Discord character limit safeguard
|
2023-03-14 17:24:21 +01:00
|
|
|
|
2024-05-30 21:20:09 +02:00
|
|
|
async def generate_response(self, api_key, messages):
|
2025-02-20 20:07:27 +01:00
|
|
|
"""✅ Generates a response using OpenAI's new async API client (OpenAI v1.0+)."""
|
2023-03-14 20:03:30 +01:00
|
|
|
model = await self.config.openai_model()
|
2025-02-20 16:04:44 +01:00
|
|
|
try:
|
2025-02-20 20:07:27 +01:00
|
|
|
client = openai.AsyncOpenAI(api_key=api_key) # ✅ Correct API usage
|
|
|
|
|
response = await client.chat.completions.create(
|
2025-02-20 19:23:15 +01:00
|
|
|
model=model,
|
|
|
|
|
messages=messages,
|
|
|
|
|
max_tokens=1024,
|
|
|
|
|
temperature=0.7,
|
|
|
|
|
presence_penalty=0.5,
|
|
|
|
|
frequency_penalty=0.5
|
|
|
|
|
)
|
|
|
|
|
|
2025-02-20 16:52:54 +01:00
|
|
|
if not response.choices:
|
2025-02-20 16:04:44 +01:00
|
|
|
return "I fear I have no words to offer at this time."
|
2025-02-20 16:52:54 +01:00
|
|
|
|
2025-02-20 20:07:27 +01:00
|
|
|
return response.choices[0].message.content.strip()
|
2025-02-20 19:57:36 +01:00
|
|
|
|
2025-02-20 20:07:27 +01:00
|
|
|
except OpenAIError as e:
|
|
|
|
|
error_message = f"OpenAI Error: {e}"
|
2025-02-20 19:57:36 +01:00
|
|
|
reginald_responses = [
|
2025-02-20 20:07:27 +01:00
|
|
|
f"Regrettably, I must inform you that I have encountered a bureaucratic obstruction:\n\n```{error_message}```",
|
|
|
|
|
f"It would seem that a most unfortunate technical hiccup has befallen my faculties:\n\n```{error_message}```",
|
|
|
|
|
f"Ah, it appears I have received an urgent memorandum stating:\n\n```{error_message}```",
|
|
|
|
|
f"I regret to inform you that my usual eloquence is presently obstructed by an unforeseen complication:\n\n```{error_message}```"
|
2025-02-20 16:04:44 +01:00
|
|
|
]
|
2025-02-20 19:57:36 +01:00
|
|
|
return random.choice(reginald_responses)
|
2025-02-20 16:52:54 +01:00
|
|
|
|
2025-02-20 16:17:36 +01:00
|
|
|
@commands.command(name="reginald_allowrole", help="Allow a role to use the Reginald command")
|
|
|
|
|
@commands.has_permissions(administrator=True)
|
|
|
|
|
async def allow_role(self, ctx, role: discord.Role):
|
2025-02-20 17:00:43 +01:00
|
|
|
"""✅ Grants permission to a role to use Reginald."""
|
2025-02-20 16:17:36 +01:00
|
|
|
await self.config.guild(ctx.guild).allowed_role.set(role.id)
|
|
|
|
|
await ctx.send(f"The role `{role.name}` (ID: `{role.id}`) 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.has_permissions(administrator=True)
|
|
|
|
|
async def disallow_role(self, ctx):
|
2025-02-20 17:00:43 +01:00
|
|
|
"""✅ Removes a role's permission to use Reginald."""
|
2025-02-20 16:17:36 +01:00
|
|
|
await self.config.guild(ctx.guild).allowed_role.clear()
|
|
|
|
|
await ctx.send("The role's permission to use the Reginald command has been revoked.")
|
2025-02-20 19:38:49 +01:00
|
|
|
|
|
|
|
|
@commands.guild_only()
|
|
|
|
|
@commands.has_permissions(manage_guild=True)
|
|
|
|
|
@commands.command(help="Set the OpenAI API key")
|
|
|
|
|
async def setreginaldcogapi(self, ctx, api_key):
|
|
|
|
|
"""Allows an admin to set the OpenAI API key for Reginald."""
|
|
|
|
|
await self.config.guild(ctx.guild).openai_api_key.set(api_key)
|
|
|
|
|
await ctx.send("OpenAI API key set successfully.")
|
|
|
|
|
|
2025-02-20 16:52:54 +01:00
|
|
|
async def setup(bot):
|
|
|
|
|
"""✅ Correct async cog setup for Redbot"""
|
|
|
|
|
await bot.add_cog(ReginaldCog(bot))
|