Merge branch 'main' into slashgroups-refactor

Signed-off-by: Kaveen Kumarasinghe <k5kumara@uwaterloo.ca>
Kaveen Kumarasinghe 1 year ago committed by GitHub
commit 5218c445af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -67,6 +67,8 @@ You also need to add the allowed guilds that the bot can operate on, this is the
You also need to add the roles that can use the bot, this is the `ALLOWED_ROLES` field, enter role names here, separated by commas. Currently, there is no way to give everybody access to the bot, and you have to use roles, but it will be done soon.
You can optionally add a fallback welcomming message for the bot to use when new members join. If enabled, the bot will DM new members with a welcomming message generated by GPT3. If the call to GPT3 fails, the string used in the .env file is used. As further redundency, a hardcoded welcomming message is used.
```
OPENAI_TOKEN="<openai_api_token>"
DISCORD_TOKEN="<discord_bot_token>"
@ -74,6 +76,7 @@ DEBUG_GUILD="974519864045756446" #discord_server_id
DEBUG_CHANNEL="977697652147892304" #discord_chanel_id
ALLOWED_GUILDS="971268468148166697,971268468148166697"
ALLOWED_ROLES="Admin,gpt"
WELCOME_MESSAGE="Hi There! Welcome to our Discord server. We hope you'll enjoy our server and we look forward to engaging with you!" #Optional
```
Optionally, you can include your own conversation starter text for the bot that's used with `!g converse`, with `CONVERSATION_STARTER_TEXT`

@ -43,12 +43,24 @@ class DrawDallEService(discord.Cog, name="DrawDallEService"):
):
await asyncio.sleep(0)
# send the prompt to the model
file, image_urls = await self.model.send_image_request(
prompt, vary=vary if not draw_from_optimizer else None
)
from_context = isinstance(ctx, discord.ApplicationContext)
try:
file, image_urls = await self.model.send_image_request(
prompt, vary=vary if not draw_from_optimizer else None
)
except ValueError as e:
(
await ctx.channel.send(
f"Error: {e}. Please try again with a different prompt."
)
if not from_context
else await ctx.respond(
f"Error: {e}. Please try again with a different prompt."
)
)
return
# Start building an embed to send to the user with the results of the image generation
embed = discord.Embed(
title="Image Generation Results"

@ -17,7 +17,6 @@ from collections import defaultdict
original_message = {}
ALLOWED_GUILDS = EnvService.get_allowed_guilds()
print("THE ALLOWED GUILDS ARE: ", ALLOWED_GUILDS)
class GPT3ComCon(discord.Cog, name="GPT3ComCon"):
@ -111,7 +110,32 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"):
checks=[Check.check_admin_roles()]
)
@discord.Cog.listener()
@commands.Cog.listener()
async def on_member_join(self, member):
if self.model.welcome_message_enabled:
query = f"Please generate a welcome message for {member.name} who has just joined the server."
try:
welcome_message_response = await self.model.send_request(
query, tokens=self.usage_service.count_tokens(query)
)
welcome_message = str(welcome_message_response["choices"][0]["text"])
except:
welcome_message = None
if not welcome_message:
welcome_message = EnvService.get_welcome_message()
welcome_embed = discord.Embed(
title=f"Welcome, {member.name}!", description=welcome_message
)
welcome_embed.add_field(
name="Just so you know...",
value="> My commands are invoked with a forward slash (/)\n> Use /help to see my help message(s).",
)
await member.send(content=None, embed=welcome_embed)
@commands.Cog.listener()
async def on_member_remove(self, member):
pass
@ -858,6 +882,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"):
"num_images",
"summarize_conversations",
"summarize_threshold",
"welcome_message_enabled",
"IMAGE_SAVE_PATH",
],
)

@ -97,3 +97,15 @@ class EnvService:
gpt_roles.lower().strip().split(",") if "," in gpt_roles else [gpt_roles.lower()]
)
return gpt_roles
@staticmethod
def get_welcome_message():
# WELCOME_MESSAGE is a default string used to welcome new members to the server if GPT3 is not available.
# The string can be blank but this is not advised. If a string cannot be found in the .env file, the below string is used.
# The string is DMd to the new server member as part of an embed.
try:
welcome_message = os.getenv("WELCOME_MESSAGE")
except:
welcome_message = "Hi there! Welcome to our Discord server!"
return welcome_message

@ -53,6 +53,7 @@ class Model:
self._summarize_conversations = True
self._summarize_threshold = 2500
self.model_max_tokens = 4024
self._welcome_message_enabled = True
try:
self.IMAGE_SAVE_PATH = os.environ["IMAGE_SAVE_PATH"]
@ -77,6 +78,20 @@ class Model:
self.openai_key = os.getenv("OPENAI_TOKEN")
# Use the @property and @setter decorators for all the self fields to provide value checking
@property
def welcome_message_enabled(self):
return self._welcome_message_enabled
@welcome_message_enabled.setter
def welcome_message_enabled(self, value):
if value.lower() == "true":
self._welcome_message_enabled = True
elif value.lower() == "false":
self._welcome_message_enabled = False
else:
raise ValueError("Value must be either true or false!")
@property
def summarize_threshold(self):
return self._summarize_threshold

@ -4,7 +4,8 @@ DEBUG_GUILD="<debug_guild_id>"
DEBUG_CHANNEL="<debug_channel_id>"
# make sure not to include an id the bot doesn't have access to
ALLOWED_GUILDS="<guild_id>,<guild_id>"
# no spaces, case insensitive
# no spaces, case sensitive, these define which roles have access to what. E.g if GPT_ROLES="gpt", then anyone with the "gpt" role can use GPT3 commands.
ADMIN_ROLES="admin,owner"
DALLE_ROLES="admin,openai,dalle"
GPT_ROLES="admin,openai,gpt"
GPT_ROLES="admin,openai,gpt"
WELCOME_MESSAGE="Hi There! Welcome to our Discord server. We hope you'll enjoy our server and we look forward to engaging with you!"

Loading…
Cancel
Save