Skip to content

Commit

Permalink
style: convert casing according to PEP 8
Browse files Browse the repository at this point in the history
  • Loading branch information
katejoh committed Aug 25, 2023
1 parent 926980a commit eaf02ac
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 233 deletions.
20 changes: 10 additions & 10 deletions api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
client = contentful.Client(SPACE_ID, ACCESS_TOKEN)


def getRandomMarketingPost(category):
categoryPosts = client.entries(
def get_random_marketing_post(category):
category_posts = client.entries(
{
"content_type": "marketing_archives",
"limit": 1000,
Expand All @@ -22,26 +22,26 @@ def getRandomMarketingPost(category):
)

posts = []
for post in categoryPosts:
for post in category_posts:
if post.fields().get("img") != None:
posts.append(post)

return random.choice(posts)


def getNextUpcomingEvent():
upcomingEvents = client.entries(
def get_next_upcoming_event():
upcoming_events = client.entries(
{"content_type": "upcomingEvents", "limit": 1, "order": "fields.index"}
)
return upcomingEvents[0]
return upcoming_events[0]


def getUpcomingEvents():
def get_upcoming_events():
return client.entries({"content_type": "upcomingEvents", "order": "fields.index"})


def getMostRecentEvent():
pastEvents = client.entries(
def get_most_recent_event():
past_events = client.entries(
{"content_type": "pastEvents", "limit": 1, "order": "-fields.index"}
)
return pastEvents[0]
return past_events[0]
124 changes: 62 additions & 62 deletions cogs/askTriviaQuestions.py → cogs/ask_trivia_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
from pyopentdb import OpenTDBClient, Category, QuestionType, Difficulty


class asksTriviaQuestionCog(commands.Cog):
class AskTriviaQuestionsCog(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
# Counter variable for incorrect answers
self.incorrectAnswers = 0
self.answeredUsers = set()
self.incorrect_answers = 0
self.answered_users = set()

@app_commands.command(
name="ask-trivia-questions", description="Asks trivia questions!"
Expand All @@ -22,55 +22,55 @@ def __init__(self, bot: commands.Bot):
app_commands.Choice(name="Hard", value=3),
]
)
async def asksTriviaQuestion(
async def ask_trivia_question(
self, inter: discord.Interaction, difficulty: app_commands.Choice[int]
):
await inter.response.defer()

# Reset the answered_users set
self.answeredUsers = set()
self.answered_users = set()

# Create a client to retrieve 1 "General Knowledge" question with the specified difficulty
client = OpenTDBClient()

if difficulty.value == 1:
questionSet = client.get_questions(
question_set = client.get_questions(
amount=1,
category=Category.GENERAL_KNOWLEDGE,
difficulty=Difficulty.EASY,
)
elif difficulty.value == 2:
questionSet = client.get_questions(
question_set = client.get_questions(
amount=1,
category=Category.GENERAL_KNOWLEDGE,
difficulty=Difficulty.MEDIUM,
)
elif difficulty.value == 3:
questionSet = client.get_questions(
question_set = client.get_questions(
amount=1,
category=Category.GENERAL_KNOWLEDGE,
difficulty=Difficulty.HARD,
)

questionTxt = questionSet.items[0].question
questionDiff = questionSet.items[0].difficulty.name.title()
choices = questionSet.items[0].choices
answerIndx = questionSet.items[0].answer_index
question_txt = question_set.items[0].question
question_diff = question_set.items[0].difficulty.name.title()
choices = question_set.items[0].choices
answer_indx = question_set.items[0].answer_index

embed = discord.Embed(
title=f"Trivia Question!",
description=f"{questionTxt} \n\n Difficulty: {questionDiff} \n\n Incorrect answers so far: {self.incorrectAnswers}",
description=f"{question_txt} \n\n Difficulty: {question_diff} \n\n Incorrect answers so far: {self.incorrect_answers}",
color=discord.Color.orange(),
)

# changed from self to self.inccorect
view = MyView(
choices,
answerIndx,
questionTxt,
questionDiff,
self.incorrectAnswers,
self.answeredUsers,
answer_indx,
question_txt,
question_diff,
self.incorrect_answers,
self.answered_users,
False,
)
await inter.followup.send(
Expand All @@ -83,22 +83,22 @@ class MyView(discord.ui.View):
def __init__(
self,
choices,
answerIndx,
questionTxt,
questionDiff,
incorrectAnswers,
answeredUsers,
answer_indx,
question_txt,
question_diff,
incorrect_answers,
answered_users,
disabled,
):
super().__init__(timeout=None)

self.select_menu = AnswersSelectMenu(
choices,
answerIndx,
questionTxt,
questionDiff,
incorrectAnswers,
answeredUsers,
answer_indx,
question_txt,
question_diff,
incorrect_answers,
answered_users,
disabled,
)

Expand All @@ -110,20 +110,20 @@ class AnswersSelectMenu(discord.ui.Select):
def __init__(
self,
choices,
answerIndx,
questionTxt,
questionDiff,
incorrectAnswers,
answeredUsers,
answer_indx,
question_txt,
question_diff,
incorrect_answers,
answered_users,
disabled,
):
self.questionTxt = questionTxt
self.questionDiff = questionDiff
self.question_txt = question_txt
self.question_diff = question_diff
self.choices = choices
self.correctChoice = choices[answerIndx]
self.answerIndx = answerIndx
self.incorrectAnswers = incorrectAnswers
self.answeredUsers = answeredUsers
self.correct_choice = choices[answer_indx]
self.answer_indx = answer_indx
self.incorrect_answers = incorrect_answers
self.answered_users = answered_users

super().__init__(
placeholder="Select a choice...",
Expand All @@ -135,61 +135,61 @@ def __init__(
self.disabled = disabled

async def callback(self, interaction: discord.Interaction):
userId = interaction.user.id
user_id = interaction.user.id

selectedChoice = self.values[0]
selected_choice = self.values[0]

if userId in self.answeredUsers:
if user_id in self.answered_users:
# User has already answered, send ephemeral message
await interaction.response.send_message(
f"You've already attempted answering this question before!",
ephemeral=True,
)
return

if selectedChoice == self.correctChoice:
if selected_choice == self.correct_choice:
# if the selected choice is correct, display a text
await self.updateMessageCorrect(interaction)
await self.update_message_correct(interaction)
else:
self.incorrectAnswers += 1
await self.updateMessageIncorrect(interaction)
self.incorrect_answers += 1
await self.update_message_incorrect(interaction)

self.answeredUsers.add(userId) # Add the user to the answered users set
self.answered_users.add(user_id) # Add the user to the answered users set

async def updateMessageIncorrect(self, inter: discord.Interaction):
async def update_message_incorrect(self, inter: discord.Interaction):
embed = discord.Embed(
title=f"Trivia Question!",
description=f"{self.questionTxt} \n\n Difficulty: {self.questionDiff} \n\n Incorrect answers so far: {self.incorrectAnswers}",
description=f"{self.question_txt} \n\n Difficulty: {self.question_diff} \n\n Incorrect answers so far: {self.incorrect_answers}",
color=discord.Color.red(),
)
view = MyView(
self.choices,
self.answerIndx,
self.questionTxt,
self.questionDiff,
self.incorrectAnswers,
self.answeredUsers,
self.answer_indx,
self.question_txt,
self.question_diff,
self.incorrect_answers,
self.answered_users,
False,
)
await inter.response.edit_message(embed=embed, view=view)

async def updateMessageCorrect(self, inter: discord.Interaction):
async def update_message_correct(self, inter: discord.Interaction):
embed = discord.Embed(
title=f"Trivia Question!",
description=f"{self.questionTxt} \n\n Difficulty: {self.questionDiff} \n\n <@{inter.user.id}> was the first to guess correctly! The correct answer was: {self.correctChoice} \n\n Incorrect answers so far: {self.incorrectAnswers}",
description=f"{self.question_txt} \n\n Difficulty: {self.question_diff} \n\n <@{inter.user.id}> was the first to guess correctly! The correct answer was: {self.correct_choice} \n\n Incorrect answers so far: {self.incorrect_answers}",
color=discord.Color.green(),
)
view = MyView(
self.choices,
self.answerIndx,
self.questionTxt,
self.questionDiff,
self.incorrectAnswers,
self.answeredUsers,
self.answer_indx,
self.question_txt,
self.question_diff,
self.incorrect_answers,
self.answered_users,
True,
)
await inter.response.edit_message(embed=embed, view=view)


async def setup(bot: commands.Bot):
await bot.add_cog(asksTriviaQuestionCog(bot))
await bot.add_cog(AskTriviaQuestionsCog(bot))
38 changes: 19 additions & 19 deletions cogs/createStudySession.py → cogs/create_study_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from zoneinfo import ZoneInfo


class createStudySessionCog(commands.Cog):
class CreateStudySessionCog(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot

Expand All @@ -23,12 +23,12 @@ async def create_study_session(
self, inter: discord.Interaction, date: str, start_time: str, end_time: str
):
timezone = ZoneInfo("Australia/Sydney")
dateFormat = "%Y-%m-%d %H:%M"
date_format = "%Y-%m-%d %H:%M"
try:
startTime = dt.datetime.strptime(
f"{date} {start_time}", dateFormat
start_time = dt.datetime.strptime(
f"{date} {start_time}", date_format
).replace(tzinfo=timezone)
endTime = dt.datetime.strptime(f"{date} {end_time}", dateFormat).replace(
end_time = dt.datetime.strptime(f"{date} {end_time}", date_format).replace(
tzinfo=timezone
)
except ValueError:
Expand All @@ -40,13 +40,13 @@ async def create_study_session(
return

# Discord timestamp formatting
startString = f"<t:{math.floor(startTime.timestamp())}:F>"
endString = f"<t:{math.floor(endTime.timestamp())}:t>"
start_string = f"<t:{math.floor(start_time.timestamp())}:F>"
end_string = f"<t:{math.floor(end_time.timestamp())}:t>"

id = inter.user.id

messageContent = f"""
**🧡 Study Session! 🧡**\n\n<@{id}> is having a study session on {startString} until {endString} in the WIT Discord Server!\n\nClick the buttons below to RSVP.\n\n
message_content = f"""
**🧡 Study Session! 🧡**\n\n<@{id}> is having a study session on {start_string} until {end_string} in the WIT Discord Server!\n\nClick the buttons below to RSVP.\n\n
"""
embed = discord.Embed(
title=f"Attendees (1):",
Expand All @@ -55,37 +55,37 @@ async def create_study_session(
)
view = MyView(id)
await inter.response.send_message(
content=messageContent,
content=message_content,
embed=embed,
view=view,
)


class MyView(discord.ui.View):
def __init__(self, creatorId):
def __init__(self, creator_id):
super().__init__(timeout=None)
self.attendees = {creatorId} # initialize set of attendees
self.attendees = {creator_id} # initialize set of attendees

@discord.ui.button(style=discord.ButtonStyle.green, label="Going")
async def going(self, inter: discord.Interaction, button: discord.ui.Button):
self.attendees.add(inter.user.id)
await self.updateMessage(inter)
await self.update_message(inter)

@discord.ui.button(style=discord.ButtonStyle.red, label="Not Going")
async def notGoing(self, inter: discord.Interaction, button: discord.ui.Button):
async def not_going(self, inter: discord.Interaction, button: discord.ui.Button):
self.attendees.discard(inter.user.id)
await self.updateMessage(inter)
await self.update_message(inter)

async def updateMessage(self, inter: discord.Interaction):
attendeesList = "\n".join(f"- <@{attendee}>" for attendee in self.attendees)
async def update_message(self, inter: discord.Interaction):
attendees_list = "\n".join(f"- <@{attendee}>" for attendee in self.attendees)

embed = discord.Embed(
title=f"Attendees ({len(self.attendees)}):",
color=discord.Color.orange(),
description=f"\n\n{attendeesList}",
description=f"\n\n{attendees_list}",
)
await inter.response.edit_message(embed=embed)


async def setup(bot: commands.Bot):
await bot.add_cog(createStudySessionCog(bot))
await bot.add_cog(CreateStudySessionCog(bot))
Loading

0 comments on commit eaf02ac

Please sign in to comment.