@ -5,7 +5,7 @@ import traceback
from pathlib import Path
from pathlib import Path
import discord
import discord
from discord. ext import commands
from pycord. multicog import add_to_group
from models . deletion_service_model import Deletion
from models . deletion_service_model import Deletion
from models . env_service_model import EnvService
from models . env_service_model import EnvService
@ -19,7 +19,7 @@ original_message = {}
ALLOWED_GUILDS = EnvService . get_allowed_guilds ( )
ALLOWED_GUILDS = EnvService . get_allowed_guilds ( )
class GPT3ComCon ( commands . Cog , name = " GPT3ComCon " ) :
class GPT3ComCon ( discord . Cog , name = " GPT3ComCon " ) :
def __init__ (
def __init__ (
self ,
self ,
bot ,
bot ,
@ -93,6 +93,23 @@ class GPT3ComCon(commands.Cog, name="GPT3ComCon"):
self . message_queue = message_queue
self . message_queue = message_queue
self . conversation_threads = { }
self . conversation_threads = { }
# Create slash command groups
dalle = discord . SlashCommandGroup ( name = " dalle " ,
description = " Dalle related commands " ,
guild_ids = ALLOWED_GUILDS ,
checks = [ Check . check_dalle_roles ( ) ]
)
gpt = discord . SlashCommandGroup ( name = " gpt " ,
description = " GPT related commands " ,
guild_ids = ALLOWED_GUILDS ,
checks = [ Check . check_gpt_roles ( ) ]
)
system = discord . SlashCommandGroup ( name = " system " ,
description = " Admin/System settings for the bot " ,
guild_ids = ALLOWED_GUILDS ,
checks = [ Check . check_admin_roles ( ) ]
)
@commands.Cog.listener ( )
@commands.Cog.listener ( )
async def on_member_join ( self , member ) :
async def on_member_join ( self , member ) :
if self . model . welcome_message_enabled :
if self . model . welcome_message_enabled :
@ -122,7 +139,7 @@ class GPT3ComCon(commands.Cog, name="GPT3ComCon"):
async def on_member_remove ( self , member ) :
async def on_member_remove ( self , member ) :
pass
pass
@ commands .Cog.listener( )
@ discord .Cog.listener( )
async def on_ready ( self ) :
async def on_ready ( self ) :
self . debug_channel = self . bot . get_guild ( self . DEBUG_GUILD ) . get_channel (
self . debug_channel = self . bot . get_guild ( self . DEBUG_GUILD ) . get_channel (
self . DEBUG_CHANNEL
self . DEBUG_CHANNEL
@ -138,17 +155,18 @@ class GPT3ComCon(commands.Cog, name="GPT3ComCon"):
)
)
print ( f " The debug channel was acquired and commands registered " )
print ( f " The debug channel was acquired and commands registered " )
@add_to_group ( " system " )
@discord.slash_command (
@discord.slash_command (
name = " set-usage " ,
name = " set-usage " ,
description = " Set the current OpenAI usage (in dollars) " ,
description = " Set the current OpenAI usage (in dollars) " ,
checks= [ Check . check_valid_roles ( ) ] ,
guild_ids= ALLOWED_GUILDS ,
)
)
@discord.option (
@discord.option (
name = " usage_amount " ,
name = " usage_amount " ,
description = " The current usage amount in dollars and cents (e.g 10.24) " ,
description = " The current usage amount in dollars and cents (e.g 10.24) " ,
type = float ,
type = float ,
)
)
async def set_usage ( self , ctx , usage_amount : float ) :
async def set_usage ( self , ctx : discord . ApplicationContext , usage_amount : float ) :
await ctx . defer ( )
await ctx . defer ( )
# Attempt to convert the input usage value into a float
# Attempt to convert the input usage value into a float
@ -160,12 +178,13 @@ class GPT3ComCon(commands.Cog, name="GPT3ComCon"):
await ctx . respond ( " The usage value must be a valid float. " )
await ctx . respond ( " The usage value must be a valid float. " )
return
return
@add_to_group ( " system " )
@discord.slash_command (
@discord.slash_command (
name = " delete-conversation-threads " ,
name = " delete-conversation-threads " ,
description = " Delete all conversation threads across the bot servers. " ,
description = " Delete all conversation threads across the bot servers. " ,
checks= [ Check . check_valid_roles ( ) ] ,
guild_ids= ALLOWED_GUILDS ,
)
)
async def delete_all_conversation_threads ( self , ctx ):
async def delete_all_conversation_threads ( self , ctx : discord . ApplicationContext ):
await ctx . defer ( )
await ctx . defer ( )
for guild in self . bot . guilds :
for guild in self . bot . guilds :
@ -219,12 +238,12 @@ class GPT3ComCon(commands.Cog, name="GPT3ComCon"):
title = " GPT3Bot Help " , description = " The current commands " , color = 0xC730C7
title = " GPT3Bot Help " , description = " The current commands " , color = 0xC730C7
)
)
embed . add_field (
embed . add_field (
name = " / g <prompt> " ,
name = " / ask " ,
value = " Ask GPT3 something. Be clear, long, and concise in your prompt. Don ' t waste tokens. " ,
value = " Ask GPT3 something. Be clear, long, and concise in your prompt. Don ' t waste tokens. " ,
inline = False ,
inline = False ,
)
)
embed . add_field (
embed . add_field (
name = " /c hat-gpt " , value = " Start a conversation with GPT3 " , inline = False
name = " /c onverse " , value = " Start a conversation with GPT3 " , inline = False
)
)
embed . add_field (
embed . add_field (
name = " /end-chat " ,
name = " /end-chat " ,
@ -247,7 +266,7 @@ class GPT3ComCon(commands.Cog, name="GPT3ComCon"):
inline = False ,
inline = False ,
)
)
embed . add_field (
embed . add_field (
name = " / img optimize <image prompt>" ,
name = " / optimize <image prompt>" ,
value = " Optimize an image prompt for use with DALL-E2, Midjourney, SD, etc. " ,
value = " Optimize an image prompt for use with DALL-E2, Midjourney, SD, etc. " ,
inline = False ,
inline = False ,
)
)
@ -432,7 +451,7 @@ class GPT3ComCon(commands.Cog, name="GPT3ComCon"):
self . conversating_users [ message . author . id ] . history = new_conversation_history
self . conversating_users [ message . author . id ] . history = new_conversation_history
# A listener for message edits to redo prompts if they are edited
# A listener for message edits to redo prompts if they are edited
@ commands .Cog.listener( )
@ discord .Cog.listener( )
async def on_message_edit ( self , before , after ) :
async def on_message_edit ( self , before , after ) :
if after . author . id in self . redo_users :
if after . author . id in self . redo_users :
if after . id == original_message [ after . author . id ] :
if after . id == original_message [ after . author . id ] :
@ -463,7 +482,7 @@ class GPT3ComCon(commands.Cog, name="GPT3ComCon"):
self . redo_users [ after . author . id ] . prompt = after . content
self . redo_users [ after . author . id ] . prompt = after . content
@ commands .Cog.listener( )
@ discord .Cog.listener( )
async def on_message ( self , message ) :
async def on_message ( self , message ) :
# Get the message from context
# Get the message from context
@ -681,17 +700,17 @@ class GPT3ComCon(commands.Cog, name="GPT3ComCon"):
await self . end_conversation ( ctx )
await self . end_conversation ( ctx )
return
return
@add_to_group ( " gpt " )
@discord.slash_command (
@discord.slash_command (
name = " g " ,
name = " ask " ,
description = " Ask GPT3 something! " ,
description = " Ask GPT3 something! " ,
guild_ids = ALLOWED_GUILDS ,
guild_ids = ALLOWED_GUILDS ,
checks = [ Check . check_valid_roles ( ) ] ,
)
)
@discord.option (
@discord.option (
name = " prompt " , description = " The prompt to send to GPT3 " , required = True
name = " prompt " , description = " The prompt to send to GPT3 " , required = True
)
)
@discord.guild_only ( )
@discord.guild_only ( )
async def g ( self , ctx : discord . ApplicationContext , prompt : str ) :
async def ask ( self , ctx : discord . ApplicationContext , prompt : str ) :
await ctx . defer ( )
await ctx . defer ( )
user = ctx . user
user = ctx . user
@ -703,11 +722,11 @@ class GPT3ComCon(commands.Cog, name="GPT3ComCon"):
await self . encapsulated_send ( user . id , prompt , ctx , from_g_command = True )
await self . encapsulated_send ( user . id , prompt , ctx , from_g_command = True )
@add_to_group ( " gpt " )
@discord.slash_command (
@discord.slash_command (
name = " c hat-gpt " ,
name = " c onverse " ,
description = " Have a conversation with GPT3 " ,
description = " Have a conversation with GPT3 " ,
guild_ids = ALLOWED_GUILDS ,
guild_ids = ALLOWED_GUILDS ,
checks = [ Check . check_valid_roles ( ) ] ,
)
)
@discord.option (
@discord.option (
name = " opener " , description = " Which sentence to start with " , required = False
name = " opener " , description = " Which sentence to start with " , required = False
@ -725,7 +744,7 @@ class GPT3ComCon(commands.Cog, name="GPT3ComCon"):
choices = [ " yes " ] ,
choices = [ " yes " ] ,
)
)
@discord.guild_only ( )
@discord.guild_only ( )
async def c hat_gpt (
async def c onverse (
self , ctx : discord . ApplicationContext , opener : str , private , minimal
self , ctx : discord . ApplicationContext , opener : str , private , minimal
) :
) :
if private :
if private :
@ -805,6 +824,7 @@ class GPT3ComCon(commands.Cog, name="GPT3ComCon"):
self . conversation_threads [ user_id_normalized ] = thread . id
self . conversation_threads [ user_id_normalized ] = thread . id
@add_to_group ( " gpt " )
@discord.slash_command (
@discord.slash_command (
name = " end-chat " ,
name = " end-chat " ,
description = " End a conversation with GPT3 " ,
description = " End a conversation with GPT3 " ,
@ -825,6 +845,7 @@ class GPT3ComCon(commands.Cog, name="GPT3ComCon"):
await ctx . defer ( )
await ctx . defer ( )
await self . send_help_text ( ctx )
await self . send_help_text ( ctx )
@add_to_group ( " system " )
@discord.slash_command (
@discord.slash_command (
name = " usage " ,
name = " usage " ,
description = " Get usage statistics for GPT3Discord " ,
description = " Get usage statistics for GPT3Discord " ,
@ -835,6 +856,7 @@ class GPT3ComCon(commands.Cog, name="GPT3ComCon"):
await ctx . defer ( )
await ctx . defer ( )
await self . send_usage_text ( ctx )
await self . send_usage_text ( ctx )
@add_to_group ( " system " )
@discord.slash_command (
@discord.slash_command (
name = " settings " ,
name = " settings " ,
description = " Get settings for GPT3Discord " ,
description = " Get settings for GPT3Discord " ,