From b44a7000cf478d39956750855e91ef3d54df496b Mon Sep 17 00:00:00 2001 From: Birger Moell Date: Tue, 13 Dec 2022 23:50:25 +0100 Subject: [PATCH 1/4] Added buttons and commands to bot --- bot/bot.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/bot/bot.py b/bot/bot.py index 03da1483..1515273b 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -52,6 +52,39 @@ intents = discord.Intents.default() intents.message_content = True client = OpenChatGPTClient(intents=intents) +class LikeButton(discord.ui.Button): + def __init__(self, label, channel, username, prompt): + super().__init__(label=label, style=discord.ButtonStyle.green, emoji="👍") + self.channel = channel + self.username = username + self.prompt = prompt + async def callback(self, interaction): + # interaction holds the interaction object + #await interaction.response.defer() + await interaction.response.send_message("Thanks for your feedback. You liked this 👍 ") + +class NeutralButton(discord.ui.Button): + def __init__(self, label, channel, username, prompt): + super().__init__(label=label, style=discord.ButtonStyle.green, emoji="😐") + self.channel = channel + self.username = username + self.prompt = prompt + async def callback(self, interaction): + # interaction holds the interaction object + #await interaction.response.defer() + await interaction.response.send_message("Thanks for your feedback. You thought this was neutral 😐 ") + +class DislikeButton(discord.ui.Button): + def __init__(self, label, channel, username, prompt): + super().__init__(label=label, style=discord.ButtonStyle.green, emoji="👎") + self.channel = channel + self.username = username + self.prompt = prompt + async def callback(self, interaction): + # interaction holds the interaction object + #await interaction.response.defer() + # send the feedback to the backend # + await interaction.response.send_message("Thanks for your feedback. You disliked this 👎 ") @client.tree.command() async def register(interaction: discord.Interaction): @@ -80,6 +113,37 @@ async def list_participants(interaction: discord.Interaction): await interaction.response.send_message("Failed to fetch participants") +async def send_propmt_with_response_and_buttons(channel, username, prompt, response): + await channel.send(f'What do you think about the following interaction: \nprompt: {prompt} \nresponse: {response}') + #await channel.send(f'Please click on the button that best describes your reaction to the response:') + + # add buttons + view = discord.ui.View() + like=LikeButton(label="Like", channel=channel, username=username, prompt=prompt) + neutral=NeutralButton(label="Neutral", channel=channel, username=username, prompt=prompt) + dislike=DislikeButton(label="Dislike", channel=channel, username=username, prompt=prompt) + + view.add_item(item=like) + view.add_item(item=neutral) + view.add_item(item=dislike) + await channel.send(view=view) + + +@client.tree.command() +async def review_prompts(interaction: discord.Interaction, number_of_prompts: int): + # get the prompt from the db + url = f"{prompts_url}?begin_id=0&limit={number_of_prompts}" + response = requests.get(url, headers=headers) + if response.status_code == 200: + prompts = response.json() + print("the responses are", prompts) + for prompt in prompts: + await send_propmt_with_response_and_buttons(interaction.channel, interaction.user.name, prompt['prompt'], prompt["response"]) + else: + await interaction.response.send_message("Failed to get prompts for review") + + + @client.tree.command() async def add_prompt(interaction: discord.Interaction, prompt: str, response: str, language: str = "en"): """Uploads a single prompt to the server.""" @@ -92,7 +156,9 @@ async def add_prompt(interaction: discord.Interaction, prompt: str, response: st } response = requests.post(prompts_url, headers=headers, json=prompt) if response.status_code == 200: - await interaction.response.send_message("Added your prompt") + await send_propmt_with_response_and_buttons(interaction.channel, interaction.user.name, prompt['prompt'], prompt["response"]) + # send the prompt back with buttons for the user to click on + #await interaction.response.send_message("Added your prompt") else: await interaction.response.send_message("Failed to add the prompt") From a10c337b3de9426ca5389c85edd7a27f37c1ad53 Mon Sep 17 00:00:00 2001 From: Birger Moell Date: Tue, 13 Dec 2022 23:51:14 +0100 Subject: [PATCH 2/4] Lint fixes --- bot/bot.py | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/bot/bot.py b/bot/bot.py index 1515273b..f3cd1acf 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -52,27 +52,32 @@ intents = discord.Intents.default() intents.message_content = True client = OpenChatGPTClient(intents=intents) + class LikeButton(discord.ui.Button): def __init__(self, label, channel, username, prompt): super().__init__(label=label, style=discord.ButtonStyle.green, emoji="👍") self.channel = channel self.username = username self.prompt = prompt + async def callback(self, interaction): # interaction holds the interaction object - #await interaction.response.defer() + # await interaction.response.defer() await interaction.response.send_message("Thanks for your feedback. You liked this 👍 ") + class NeutralButton(discord.ui.Button): def __init__(self, label, channel, username, prompt): super().__init__(label=label, style=discord.ButtonStyle.green, emoji="😐") self.channel = channel self.username = username self.prompt = prompt + async def callback(self, interaction): # interaction holds the interaction object - #await interaction.response.defer() - await interaction.response.send_message("Thanks for your feedback. You thought this was neutral 😐 ") + # await interaction.response.defer() + await interaction.response.send_message("Thanks for your feedback. You thought this was neutral 😐 ") + class DislikeButton(discord.ui.Button): def __init__(self, label, channel, username, prompt): @@ -80,12 +85,14 @@ class DislikeButton(discord.ui.Button): self.channel = channel self.username = username self.prompt = prompt + async def callback(self, interaction): # interaction holds the interaction object - #await interaction.response.defer() + # await interaction.response.defer() # send the feedback to the backend # await interaction.response.send_message("Thanks for your feedback. You disliked this 👎 ") + @client.tree.command() async def register(interaction: discord.Interaction): """Registers the user for submissions.""" @@ -114,14 +121,14 @@ async def list_participants(interaction: discord.Interaction): async def send_propmt_with_response_and_buttons(channel, username, prompt, response): - await channel.send(f'What do you think about the following interaction: \nprompt: {prompt} \nresponse: {response}') - #await channel.send(f'Please click on the button that best describes your reaction to the response:') + await channel.send(f"What do you think about the following interaction: \nprompt: {prompt} \nresponse: {response}") + # await channel.send(f'Please click on the button that best describes your reaction to the response:') # add buttons view = discord.ui.View() - like=LikeButton(label="Like", channel=channel, username=username, prompt=prompt) - neutral=NeutralButton(label="Neutral", channel=channel, username=username, prompt=prompt) - dislike=DislikeButton(label="Dislike", channel=channel, username=username, prompt=prompt) + like = LikeButton(label="Like", channel=channel, username=username, prompt=prompt) + neutral = NeutralButton(label="Neutral", channel=channel, username=username, prompt=prompt) + dislike = DislikeButton(label="Dislike", channel=channel, username=username, prompt=prompt) view.add_item(item=like) view.add_item(item=neutral) @@ -138,12 +145,13 @@ async def review_prompts(interaction: discord.Interaction, number_of_prompts: in prompts = response.json() print("the responses are", prompts) for prompt in prompts: - await send_propmt_with_response_and_buttons(interaction.channel, interaction.user.name, prompt['prompt'], prompt["response"]) + await send_propmt_with_response_and_buttons( + interaction.channel, interaction.user.name, prompt["prompt"], prompt["response"] + ) else: await interaction.response.send_message("Failed to get prompts for review") - @client.tree.command() async def add_prompt(interaction: discord.Interaction, prompt: str, response: str, language: str = "en"): """Uploads a single prompt to the server.""" @@ -156,9 +164,11 @@ async def add_prompt(interaction: discord.Interaction, prompt: str, response: st } response = requests.post(prompts_url, headers=headers, json=prompt) if response.status_code == 200: - await send_propmt_with_response_and_buttons(interaction.channel, interaction.user.name, prompt['prompt'], prompt["response"]) + await send_propmt_with_response_and_buttons( + interaction.channel, interaction.user.name, prompt["prompt"], prompt["response"] + ) # send the prompt back with buttons for the user to click on - #await interaction.response.send_message("Added your prompt") + # await interaction.response.send_message("Added your prompt") else: await interaction.response.send_message("Failed to add the prompt") From c807737ab42f7c89fecc75a97814cacaacd83be9 Mon Sep 17 00:00:00 2001 From: Birger Moell Date: Wed, 14 Dec 2022 01:15:20 +0100 Subject: [PATCH 3/4] Fix typo and added logger --- bot/bot.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/bot/bot.py b/bot/bot.py index f3cd1acf..a29993b3 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -6,8 +6,10 @@ import os import discord import requests from discord import app_commands +from loguru import logger from dotenv import load_dotenv + bot_url = "https://discord.com/api/oauth2/authorize?client_id=1051614245940375683&permissions=8&scope=bot" # Load up all the important environment variables. @@ -44,7 +46,7 @@ class OpenChatGPTClient(discord.Client): else: # This can take up to an hour for the commands to be registered. await self.tree.sync() - print("Ready!") + logger.debug("Ready!") # List the set of intents needed for commands to operate properly. @@ -105,7 +107,7 @@ async def register(interaction: discord.Interaction): if response.status_code == 200: await interaction.response.send_message(f"Added you {interaction.user.name}") else: - print(response) + logger.debug(response) await interaction.response.send_message("Failed to add you") @@ -120,7 +122,7 @@ async def list_participants(interaction: discord.Interaction): await interaction.response.send_message("Failed to fetch participants") -async def send_propmt_with_response_and_buttons(channel, username, prompt, response): +async def send_prompt_with_response_and_button(channel, username, prompt, response): await channel.send(f"What do you think about the following interaction: \nprompt: {prompt} \nresponse: {response}") # await channel.send(f'Please click on the button that best describes your reaction to the response:') @@ -143,9 +145,9 @@ async def review_prompts(interaction: discord.Interaction, number_of_prompts: in response = requests.get(url, headers=headers) if response.status_code == 200: prompts = response.json() - print("the responses are", prompts) + logger.debug("the responses are:", prompts) for prompt in prompts: - await send_propmt_with_response_and_buttons( + await send_prompt_with_response_and_button( interaction.channel, interaction.user.name, prompt["prompt"], prompt["response"] ) else: @@ -164,7 +166,7 @@ async def add_prompt(interaction: discord.Interaction, prompt: str, response: st } response = requests.post(prompts_url, headers=headers, json=prompt) if response.status_code == 200: - await send_propmt_with_response_and_buttons( + await send_prompt_with_response_and_button( interaction.channel, interaction.user.name, prompt["prompt"], prompt["response"] ) # send the prompt back with buttons for the user to click on From 0ff400c947673b0fb3a7f8c32d7c95de636198c1 Mon Sep 17 00:00:00 2001 From: Birger Moell Date: Wed, 14 Dec 2022 01:16:03 +0100 Subject: [PATCH 4/4] lint fix --- bot/bot.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bot/bot.py b/bot/bot.py index a29993b3..2509138a 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -6,9 +6,8 @@ import os import discord import requests from discord import app_commands -from loguru import logger from dotenv import load_dotenv - +from loguru import logger bot_url = "https://discord.com/api/oauth2/authorize?client_id=1051614245940375683&permissions=8&scope=bot"