From e8979aa8d4405056ff010a1ad164e09ffff3a691 Mon Sep 17 00:00:00 2001 From: Karl <58265182+KarlKennerley@users.noreply.github.com> Date: Sun, 1 Jan 2023 01:01:32 +0000 Subject: [PATCH 1/9] Update sample.env with welcome message. Issue #34 Added a welcome message env variable to be used when new members join the server. Fixes issue #34 --- sample.env | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sample.env b/sample.env index 83b3c33..85a649e 100644 --- a/sample.env +++ b/sample.env @@ -3,4 +3,5 @@ DISCORD_TOKEN="" DEBUG_GUILD="755420092027633774" DEBUG_CHANNEL="907974109084942396" ALLOWED_GUILDS="971268468148166697,971268468148166697" -ALLOWED_ROLES="Admin,gpt" \ No newline at end of file +ALLOWED_ROLES="Admin,gpt" +WELCOME_MESSAGES="Hi There! Welcome to our Discord server. We hope you'll enjoy our server and we look forward to engaing with you!" From 0b4ce7855764ae8219402b94e9e990fdf8483747 Mon Sep 17 00:00:00 2001 From: Karl <58265182+KarlKennerley@users.noreply.github.com> Date: Sun, 1 Jan 2023 01:14:29 +0000 Subject: [PATCH 2/9] Fixed spelling. Changed "engaing" to "engaging". --- sample.env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sample.env b/sample.env index 85a649e..46f782c 100644 --- a/sample.env +++ b/sample.env @@ -4,4 +4,4 @@ DEBUG_GUILD="755420092027633774" DEBUG_CHANNEL="907974109084942396" ALLOWED_GUILDS="971268468148166697,971268468148166697" ALLOWED_ROLES="Admin,gpt" -WELCOME_MESSAGES="Hi There! Welcome to our Discord server. We hope you'll enjoy our server and we look forward to engaing with you!" +WELCOME_MESSAGE="Hi There! Welcome to our Discord server. We hope you'll enjoy our server and we look forward to engaging with you!" From 49b652bdd3f78996739542941593f5b6e2f5c820 Mon Sep 17 00:00:00 2001 From: Karl <58265182+KarlKennerley@users.noreply.github.com> Date: Sun, 1 Jan 2023 01:24:17 +0000 Subject: [PATCH 3/9] Added getter for welcome message env variable. Added a getter function for the welcome message environment variable. Fixes issue #34. --- models/env_service_model.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/models/env_service_model.py b/models/env_service_model.py index 3536aa4..317058e 100644 --- a/models/env_service_model.py +++ b/models/env_service_model.py @@ -53,3 +53,21 @@ class EnvService: allowed_roles.split(",") if "," in allowed_roles else [allowed_roles] ) return allowed_roles + + @staticmethod + def get_welcome_message(): + # WELCOME_MESSAGE is a string used to welcome new members to the server. + #The string can be blank. + #The string is DMd to the new server member as part of an embed. + try: + welcome_message = os.getenv("WELCOME_MESSAGE") + except: + welcome_message = None + + if welcome_message is None: + raise ValueError( + "WELCOME_MESSAGE is not defined properly in the environment file!" + "Please create a welcome message and put it into WELCOME_MESSAGE in the .env file." + 'For example a line should look like: `WELCOME_MESSAGE:"Welcome to our server! We hop you enjoy it here."`' + ) + return welcome_message From 88abf2946dd7ccade6324927a0cfcc034e412fa3 Mon Sep 17 00:00:00 2001 From: Karl <58265182+KarlKennerley@users.noreply.github.com> Date: Sun, 1 Jan 2023 01:41:02 +0000 Subject: [PATCH 4/9] Added on_member_join listener. Fixes issue #34. Added the on_member_join listener. A DM will be sent to the new member upon joining the server. Fixes issue #34. --- cogs/gpt_3_commands_and_converser.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cogs/gpt_3_commands_and_converser.py b/cogs/gpt_3_commands_and_converser.py index 762f4b3..2620a85 100644 --- a/cogs/gpt_3_commands_and_converser.py +++ b/cogs/gpt_3_commands_and_converser.py @@ -16,6 +16,7 @@ from collections import defaultdict original_message = {} ALLOWED_GUILDS = EnvService.get_allowed_guilds() +WELCOME_MESSAGE = EnvService.get_welcome_message() class GPT3ComCon(commands.Cog, name="GPT3ComCon"): @@ -89,6 +90,12 @@ class GPT3ComCon(commands.Cog, name="GPT3ComCon"): return False return True + @commands.Cog.listener() + async def on_member_join(self, member): + 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 messages.") + await member.send(content=None, embed=welcome_embded) + @commands.Cog.listener() async def on_member_remove(self, member): pass From 357422414ab8bc8b4bf5ddba7a556099b469f18e Mon Sep 17 00:00:00 2001 From: Karl <58265182+KarlKennerley@users.noreply.github.com> Date: Sun, 1 Jan 2023 20:35:18 +0000 Subject: [PATCH 5/9] Added getter and setter for welcome msg setting. Added getter and setter functions to allow for enabling and disabling of welcoming messages. --- models/openai_model.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/models/openai_model.py b/models/openai_model.py index 99ac0f6..9eb7726 100644 --- a/models/openai_model.py +++ b/models/openai_model.py @@ -51,6 +51,7 @@ class Model: self._summarize_conversations = True self._summarize_threshold = 2500 self.model_max_tokens = 4024 + self.send_welcome_message = False try: self.IMAGE_SAVE_PATH = os.environ["IMAGE_SAVE_PATH"] @@ -75,6 +76,21 @@ 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.send_welcome_message + + @send_welcome_message.setter + def welcome_message_enabled(self, value): + if value.lower() == "true": + self.send_welcome_message = True + elif value.lower() == "false": + self.send_welcome_message = False + else: + raise ValueError("Value must be either true or false!") + + @property def summarize_threshold(self): return self._summarize_threshold From f30c9f07e3bb1db76d384dabc552f58057d27ab4 Mon Sep 17 00:00:00 2001 From: Karl <58265182+KarlKennerley@users.noreply.github.com> Date: Sun, 1 Jan 2023 21:46:21 +0000 Subject: [PATCH 6/9] Changed welcome message to query GPT3 for new msg Welcome messages are now generated by GPT3 or default to one stored in env variable if GPT3 in unavailable. --- cogs/gpt_3_commands_and_converser.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/cogs/gpt_3_commands_and_converser.py b/cogs/gpt_3_commands_and_converser.py index 2620a85..600e9a2 100644 --- a/cogs/gpt_3_commands_and_converser.py +++ b/cogs/gpt_3_commands_and_converser.py @@ -16,7 +16,6 @@ from collections import defaultdict original_message = {} ALLOWED_GUILDS = EnvService.get_allowed_guilds() -WELCOME_MESSAGE = EnvService.get_welcome_message() class GPT3ComCon(commands.Cog, name="GPT3ComCon"): @@ -92,9 +91,14 @@ class GPT3ComCon(commands.Cog, name="GPT3ComCon"): @commands.Cog.listener() async def on_member_join(self, member): - 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 messages.") - await member.send(content=None, embed=welcome_embded) + if self.welcome_message_enabled(): + query = f"Please generate a welcome message for {member.name} who has just joined the server." + welcome_message = self.send_request(query, tokens=self.usage_service.count_tokens(query)) + 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_embded) @commands.Cog.listener() async def on_member_remove(self, member): From 3a663c244f134e25be220a8f4f25734ffd1c282a Mon Sep 17 00:00:00 2001 From: Karl <58265182+KarlKennerley@users.noreply.github.com> Date: Sun, 1 Jan 2023 21:52:35 +0000 Subject: [PATCH 7/9] Updated get_welcome_message function Updated the get_welcome_message function to fall back on a string saved in a .env file and then to fall back on a simple default string. --- models/env_service_model.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/models/env_service_model.py b/models/env_service_model.py index 317058e..0271697 100644 --- a/models/env_service_model.py +++ b/models/env_service_model.py @@ -56,18 +56,11 @@ class EnvService: @staticmethod def get_welcome_message(): - # WELCOME_MESSAGE is a string used to welcome new members to the server. - #The string can be blank. + # 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 = None - - if welcome_message is None: - raise ValueError( - "WELCOME_MESSAGE is not defined properly in the environment file!" - "Please create a welcome message and put it into WELCOME_MESSAGE in the .env file." - 'For example a line should look like: `WELCOME_MESSAGE:"Welcome to our server! We hop you enjoy it here."`' - ) - return welcome_message + welcome_message = "Hi there! Welcome to our Discord server!" + return welcome_message From a5f5b0d35d9270e947094fff85d5edcf016bd03c Mon Sep 17 00:00:00 2001 From: Karl <58265182+KarlKennerley@users.noreply.github.com> Date: Mon, 2 Jan 2023 14:42:41 +0000 Subject: [PATCH 8/9] Updated on_member_join function --- cogs/gpt_3_commands_and_converser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/gpt_3_commands_and_converser.py b/cogs/gpt_3_commands_and_converser.py index 600e9a2..641f09f 100644 --- a/cogs/gpt_3_commands_and_converser.py +++ b/cogs/gpt_3_commands_and_converser.py @@ -93,7 +93,7 @@ class GPT3ComCon(commands.Cog, name="GPT3ComCon"): async def on_member_join(self, member): if self.welcome_message_enabled(): query = f"Please generate a welcome message for {member.name} who has just joined the server." - welcome_message = self.send_request(query, tokens=self.usage_service.count_tokens(query)) + welcome_message = self.model.send_request(query, tokens=self.usage_service.count_tokens(query)) if not welcome_message: welcome_message = EnvService.get_welcome_message() welcome_embed = discord.Embed(title=f"Welcome, {member.name}!", description=welcome_message) From e9e05facd1f92326004970e5b3722e3b35abdb5f Mon Sep 17 00:00:00 2001 From: Karl <58265182+KarlKennerley@users.noreply.github.com> Date: Mon, 2 Jan 2023 15:25:23 +0000 Subject: [PATCH 9/9] Added .env welcome_message info Added information on the WELCOME_MESSAGE .env variable. --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 1873c72..8ba4e9a 100644 --- a/README.md +++ b/README.md @@ -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="" DISCORD_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`