diff --git a/README.md b/README.md index a3665f2..a13098a 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ platforms such as GitHub discussions/issues might be added in the future. | GITHUB_CLIENT_ID | True | `None` | GitHub OAuth2 client id. | | GITHUB_CLIENT_SECRET | True | `None` | GitHub OAuth2 client secret. | | GITHUB_REDIRECT_URI | False | `https://localhost:8080/github/callback` | The redirect uri for OAuth2. Must be publicly accessible. | +| GITHUB_TOKEN | True | `None` | GitHub personal access token. Must have `read:org` | | GITHUB_WEBHOOK_SECRET_KEY | True | `None` | A secret value to ensure webhooks are from trusted sources. | | GRAVATAR_EMAIL | False | `None` | Gravatar email address for bot avatar. | | IGDB_CLIENT_ID | False | `None` | Required if daily_releases is enabled. | diff --git a/src/discord/tasks.py b/src/discord/tasks.py index 8e63a1d..705dce2 100644 --- a/src/discord/tasks.py +++ b/src/discord/tasks.py @@ -182,13 +182,20 @@ async def daily_task(bot: Bot) -> bool: @tasks.loop(minutes=1.0) -async def role_update_task(bot: Bot) -> bool: +async def role_update_task(bot: Bot, test_mode: bool = False) -> bool: """ Run the role update task. This function runs on a schedule, every 1 minute. If the current time is not divisible by 10, return False. e.g. Run every 10 minutes. + Parameters + ---------- + bot : Bot + The Discord bot instance. + test_mode : bool, optional + Whether to run the task in test mode, by default False. This simply affects how the roles are assigned. + Returns ------- bool @@ -234,14 +241,14 @@ async def role_update_task(bot: Bot) -> bool: user_data['roles'] = [] if user_data.get('github_username'): - user_data['roles'].append('github-users') + user_data['roles'].append('github-user') # update the discord user roles for g in bot.guilds: roles = g.roles role_map = { - 'github-users': discord.utils.get(roles, name='github-users'), + 'github-user': discord.utils.get(roles, name='github-user'), 'supporters': discord.utils.get(roles, name='supporters'), 't1-sponsors': discord.utils.get(roles, name='t1-sponsors'), 't2-sponsors': discord.utils.get(roles, name='t2-sponsors'), @@ -258,13 +265,21 @@ async def role_update_task(bot: Bot) -> bool: continue if user_role in user_roles: - # await member.add_roles(role) - add_future = asyncio.run_coroutine_threadsafe(member.add_roles(role), bot.loop) - add_future.result() + if not test_mode: + await member.add_roles(role) + else: + # using a standard await fails inside unit tests, although it works normally + # RuntimeError: Timeout context manager should be used inside a task + add_future = asyncio.run_coroutine_threadsafe(member.add_roles(role), bot.loop) + add_future.result() elif user_role in revocable_roles: - # await member.remove_roles(role) - remove_future = asyncio.run_coroutine_threadsafe(member.remove_roles(role), bot.loop) - remove_future.result() + if not test_mode: + await member.remove_roles(role) + else: + # using a standard await fails inside unit tests, although it works normally + # RuntimeError: Timeout context manager should be used inside a task + remove_future = asyncio.run_coroutine_threadsafe(member.remove_roles(role), bot.loop) + remove_future.result() with bot.db as db: db['discord_users'] = discord_users diff --git a/tests/unit/discord/test_tasks.py b/tests/unit/discord/test_tasks.py index 49f16f0..2f22b24 100644 --- a/tests/unit/discord/test_tasks.py +++ b/tests/unit/discord/test_tasks.py @@ -122,7 +122,7 @@ async def test_role_update_task(discord_bot, discord_db_users, mocker, skip): mock_datetime.now.return_value = datetime(2023, 1, 1, 0, 1 if skip else 0, 0, tzinfo=timezone.utc) # Run the task - result = await tasks.role_update_task(bot=discord_bot) + result = await tasks.role_update_task(bot=discord_bot, test_mode=True) assert result is not skip