Skip to content

Commit

Permalink
Refactored send message to discord user
Browse files Browse the repository at this point in the history
Part of #4870
  • Loading branch information
renzo authored and renzon committed May 10, 2024
1 parent 10a502e commit d8a5de1
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
9 changes: 9 additions & 0 deletions pythonpro/discord/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ def remove_guild_member(self, guild_id, user_id):
"\nhttps://discord.com/developers/docs/topics/permissions#permission-hierarchy"
) from error

def send_user_message(self, user_id: str, message: str):
"""
This is only a shortcut for sending a message to a user
It calls get_dm_channel function to get a discord bot channel and after that
it calls create_message function to send a message to a user
"""
dm_channel = self.get_dm_channel(user_id)
return self.create_message(dm_channel['id'], message)


class DiscordAppAndBotClient(DiscordAppClient, DiscordBotClient):
def __init__(self, access_token: str, bot_token: str):
Expand Down
3 changes: 1 addition & 2 deletions pythonpro/discord/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ def clean_discord_user(discord_user_id):
)

if not has_discord_access:
dm_channel = discord_bot_client.get_dm_channel(discord_user_id)
discord_bot_client.create_message(dm_channel['id'], msg)
discord_bot_client.send_user_message(discord_user_id, msg)
discord_bot_client.remove_guild_member(settings.DISCORD_GUILD_ID, discord_user_id)

logging.info(f'Clean discord user: {discord_user_id} with status: {lead_status.label}')
Expand Down
61 changes: 61 additions & 0 deletions pythonpro/discord/tests/test_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,67 @@ def test_remove_guild_member(responses):
bot_client.remove_guild_member(guild_id=guild_id, user_id=user_id)


def test_get_dm_channel(responses):
bot_token = 'MTAwNDU0MTg3NTk1NDQwNTQzNg.GFcT5X.Kp4gVn0U1kOPvdwLku-oki7LI_wtbMma2E_ET4'
bot_client = DiscordBotClient(bot_token)
user_id = 946364767864504360

channel_id = '122334232132323'
responses.add(
responses.POST, 'https://discord.com/api/v10/users/@me/channels', json={'id': channel_id}, status=200,
match=[
matchers.json_params_matcher({'recipient_id': user_id})
]
)

resp_json = bot_client.get_dm_channel(user_id)
assert resp_json == {'id': channel_id}


def test_create_msg(responses):
bot_token = 'MTAwNDU0MTg3NTk1NDQwNTQzNg.GFcT5X.Kp4gVn0U1kOPvdwLku-oki7LI_wtbMma2E_ET4'
bot_client = DiscordBotClient(bot_token)

message = 'some new message'
channel_id = '122334232132323'
responses.add(
responses.POST, f'https://discord.com/api/v10/channels/{channel_id}/messages', json={'id': 'message_id'},
status=200,
match=[
matchers.json_params_matcher({'content': message})
]
)

resp_json = bot_client.create_message(channel_id, message)
assert resp_json == {'id': 'message_id'}


def test_send_user_msg(responses):
bot_token = 'MTAwNDU0MTg3NTk1NDQwNTQzNg.GFcT5X.Kp4gVn0U1kOPvdwLku-oki7LI_wtbMma2E_ET4'
bot_client = DiscordBotClient(bot_token)

message = 'some new message'
channel_id = '122334232132323'
user_id = 946364767864504360

responses.add(
responses.POST, 'https://discord.com/api/v10/users/@me/channels', json={'id': channel_id}, status=200,
match=[
matchers.json_params_matcher({'recipient_id': user_id})
]
)
responses.add(
responses.POST, f'https://discord.com/api/v10/channels/{channel_id}/messages', json={'id': 'message_id'},
status=200,
match=[
matchers.json_params_matcher({'content': message})
]
)

resp_json = bot_client.send_user_message(user_id, message)
assert resp_json == {'id': 'message_id'}


def test_remove_guild_member_bot_missing_permissionts(responses):
bot_token = 'MTAwNDU0MTg3NTk1NDQwNTQzNg.GFcT5X.Kp4gVn0U1kOPvdwLku-oki7LI_wtbMma2E_ET4'
bot_client = DiscordBotClient(bot_token)
Expand Down

0 comments on commit d8a5de1

Please sign in to comment.