diff --git a/spam.txt b/spam.txt index e69de29..2bff9ec 100644 --- a/spam.txt +++ b/spam.txt @@ -0,0 +1,4 @@ +1157772674920099931 +1157772674920099931 +1145749111157555312 +1157772674920099931 diff --git a/src/bot.py b/src/bot.py index d20c324..5654b13 100644 --- a/src/bot.py +++ b/src/bot.py @@ -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,)) @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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): """ @@ -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): """ diff --git a/src/office_hours.py b/src/office_hours.py index 0859749..58c4056 100644 --- a/src/office_hours.py +++ b/src/office_hours.py @@ -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): diff --git a/test/test_expansion.py b/test/test_expansion.py new file mode 100644 index 0000000..ee5f0bc --- /dev/null +++ b/test/test_expansion.py @@ -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() \ No newline at end of file