Skip to content

Commit

Permalink
Merge pull request #15 from tanmaypardeshi/expansion_WelcomeMessage
Browse files Browse the repository at this point in the history
Integrated important links on guild join
  • Loading branch information
cray94 authored Oct 20, 2023
2 parents 0a78eee + 14c832f commit 8ee2cab
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 12 deletions.
4 changes: 4 additions & 0 deletions spam.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1157772674920099931
1157772674920099931
1145749111157555312
1157772674920099931
24 changes: 12 additions & 12 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,18 @@ async def on_guild_join(guild):
###########################
@bot.event
async def on_member_join(member):
''' run on guild join '''
# channel = get(member.guild.text_channels, name='general')
welcome_message = f"Hello {member}! Welcome to {member.guild.name} important links:.\n"
# Retrieve important links from the 'important-links' channel
important_links_channel = get(member.guild.text_channels, name='important-links')
if important_links_channel:
async for message in important_links_channel.history(limit=10):
link_match = re.search(r'^(.*?):\s*(https:\/\/[^\s]+)', message.content)
if link_match:
link_name, link_url = link_match.group(1), link_match.group(2)
welcome_message += f"**[{link_name}]({link_url})**\n"
await member.send(welcome_message)
channel = get(member.guild.text_channels, name='general')
insert_query = f"INSERT INTO rank (user_id) VALUES (?)"
db.mutation_query(insert_query, (member.id,))
Expand Down Expand Up @@ -419,7 +431,6 @@ async def get_rank(ctx, member_id=None):
await ctx.channel.send(file=file)
except Exception as e:
await ctx.channel.send(f"No {member_id} in the database")

###########################
# Function: get_instructor
# Description: Command used to give Instructor role out by instructors
Expand All @@ -438,7 +449,6 @@ async def get_instructor(ctx):
await ctx.send(instructors + " is the Instructor!")
else:
await ctx.send(instructors + " are the Instructors!")

###########################
# Function: set_instructor
# Description: Command used to give Instructor role out by instructors
Expand All @@ -464,7 +474,6 @@ async def set_instructor(ctx, member:discord.Member):
send_messages=True,read_message_history=False)
else:
await ctx.channel.send('Not a valid command for this channel')

###########################
# Function: remove_instructor
# Description: Command used to remove a user from Instructor role by instructors
Expand All @@ -489,7 +498,6 @@ async def remove_instructor(ctx, member:discord.Member):
await channel.set_permissions(member, overwrite=None)
else:
await ctx.channel.send('Not a valid command for this channel')

###########################
# Function: create_event
# Description: command to create event and send to event_creation module
Expand All @@ -505,7 +513,6 @@ async def remove_instructor(ctx, member:discord.Member):
async def create_event(ctx):
''' run event creation interface '''
await event_creation.create_event(ctx, TESTING_MODE)

###########################
# Function: create_event
# Description: command to create event and send to event_creation module
Expand All @@ -520,7 +527,6 @@ async def create_event(ctx):
async def set_spam_settings(ctx):
''' run spam setting prompts '''
await spam.set(ctx)

###########################
# Function: oh
# Description: command related office hour and send to office_hours module
Expand All @@ -535,7 +541,6 @@ async def set_spam_settings(ctx):
async def office_hour_command(ctx, command, *args):
''' run office hour commands with various args '''
await office_hours.office_hour_command(ctx, command, *args)

###########################
# Function: ask
# Description: command to ask question and sends to qna module
Expand Down Expand Up @@ -604,7 +609,6 @@ async def submit_regrade_request(ctx,name:str,questions:str):
else:
await ctx.author.send('Please submit requests in regrade channel.')
await ctx.message.delete()

@submit_regrade_request.error
async def submit_regrade_request_error(ctx, error):
"""
Expand All @@ -626,15 +630,11 @@ async def display_regrade_request(ctx):
Outputs:
- displays regrade requests present in the database
"""

if ctx.channel.name == 'regrade-requests':
await regrade.display_requests(ctx)

else:
await ctx.author.send('Please submit requests in regrade channel.')
await ctx.message.delete()


@bot.command(name='update-request', help='update regrade request')
async def update_regrade_request(ctx,name:str,questions:str):
"""
Expand Down
2 changes: 2 additions & 0 deletions src/office_hours.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ async def check_office_hour_loop():
begin_time = datetime.strptime(str(begin_time).rsplit(' ', 1)[1],
'%H:%M:%S').time()
end_time = datetime.strptime(str(end_time).rsplit(' ', 1)[1], '%H:%M:%S').time()
if office_hour.ta is None:
office_hour.ta = 'bot'
ta_name_channelified = office_hour.ta.lower().replace(" ", "-")
if (begin_time <= curr_time <= end_time and ta_name_channelified
not in office_hour_queues):
Expand Down
51 changes: 51 additions & 0 deletions test/test_expansion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
###########################
# Tests attendance functionality
###########################


import unittest
from unittest.mock import AsyncMock, MagicMock

# Import the function or method that you want to test.
#from bot import on_member_join

class TestDiscordBot(unittest.IsolatedAsyncioTestCase):
async def test_on_member_join(self):
# Create a mock member and guild objects.
member = MagicMock()
guild = MagicMock()
guild.name = "TestServer"
member.guild = guild

# Create a mock text channel and set its name.
text_channel = MagicMock()
text_channel.name = "general"

# Create a mock important-links channel.
important_links_channel = MagicMock()
important_links_channel.name = "important-links"

# Create a mock message with important links.
message = MagicMock()
message.content = "Link1: https://example1.com\nLink2: https://example2.com"

# Set up the necessary methods and attributes for the mocks.
guild.text_channels = [text_channel, important_links_channel]
important_links_channel.history = AsyncMock(return_value=[message])
import re

# Mock the send method for the member.
member.send = AsyncMock()

# Call the on_member_join event handler.
await on_member_join(member)

# Define your expectations and assertions.
expected_message = "Hello TestMember! Welcome to TestServer :) These are the important links, please follow it: .\n"
expected_message += "**Link1](https://example1.com)\n**[Link2](https://example2.com)\n"

# Assert that the member.send method was called with the expected message.
member.send.assert_called_once_with(expected_message)

if __name__ == '__main__':
unittest.main()

0 comments on commit 8ee2cab

Please sign in to comment.