Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 63e8ab9

Browse files
author
renzo
committed
Refactored send message to discord user
Part of #4870
1 parent 10a502e commit 63e8ab9

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

pythonpro/discord/api_client.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ def remove_guild_member(self, guild_id, user_id):
117117
"\nhttps://discord.com/developers/docs/topics/permissions#permission-hierarchy"
118118
) from error
119119

120+
def send_user_message(self, user_id: str, message: str):
121+
"""
122+
This is only a shortcut for sending a message to a user
123+
It calls get_dm_channel function to get a discord bot channel and after that
124+
it calls create_message function to send a message to a user
125+
"""
126+
dm_channel = self.get_dm_channel(user_id)
127+
return self.create_message(dm_channel['id'], message)
128+
120129

121130
class DiscordAppAndBotClient(DiscordAppClient, DiscordBotClient):
122131
def __init__(self, access_token: str, bot_token: str):

pythonpro/discord/tasks.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ def clean_discord_user(discord_user_id):
3131
)
3232

3333
if not has_discord_access:
34-
dm_channel = discord_bot_client.get_dm_channel(discord_user_id)
35-
discord_bot_client.create_message(dm_channel['id'], msg)
34+
discord_bot_client.send_user_message(discord_user_id, msg)
3635
discord_bot_client.remove_guild_member(settings.DISCORD_GUILD_ID, discord_user_id)
3736

3837
logging.info(f'Clean discord user: {discord_user_id} with status: {lead_status.label}')

pythonpro/discord/tests/test_api_client.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,67 @@ def test_remove_guild_member(responses):
153153
bot_client.remove_guild_member(guild_id=guild_id, user_id=user_id)
154154

155155

156+
def test_get_dm_channel(responses):
157+
bot_token = 'MTAwNDU0MTg3NTk1NDQwNTQzNg.GFcT5X.Kp4gVn0U1kOPvdwLku-oki7LI_wtbMma2E_ET4'
158+
bot_client = DiscordBotClient(bot_token)
159+
user_id = 946364767864504360
160+
161+
channel_id = '122334232132323'
162+
responses.add(
163+
responses.POST, 'https://discord.com/api/v10/users/@me/channels', json={'id': channel_id}, status=200,
164+
match=[
165+
matchers.json_params_matcher({'recipient_id': user_id})
166+
]
167+
)
168+
169+
resp_json = bot_client.get_dm_channel(user_id)
170+
assert resp_json == {'id': channel_id}
171+
172+
173+
def test_create_msg(responses):
174+
bot_token = 'MTAwNDU0MTg3NTk1NDQwNTQzNg.GFcT5X.Kp4gVn0U1kOPvdwLku-oki7LI_wtbMma2E_ET4'
175+
bot_client = DiscordBotClient(bot_token)
176+
177+
message = 'some new message'
178+
channel_id = '122334232132323'
179+
responses.add(
180+
responses.POST, f'https://discord.com/api/v10/channels/{channel_id}/messages', json={'id': 'message_id'},
181+
status=200,
182+
match=[
183+
matchers.json_params_matcher({'content': message})
184+
]
185+
)
186+
187+
resp_json = bot_client.create_message(channel_id, message)
188+
assert resp_json == {'id': 'message_id'}
189+
190+
191+
def test_send_user_msg(responses):
192+
bot_token = 'MTAwNDU0MTg3NTk1NDQwNTQzNg.GFcT5X.Kp4gVn0U1kOPvdwLku-oki7LI_wtbMma2E_ET4'
193+
bot_client = DiscordBotClient(bot_token)
194+
195+
message = 'some new message'
196+
channel_id = '122334232132323'
197+
user_id = 946364767864504360
198+
199+
responses.add(
200+
responses.POST, 'https://discord.com/api/v10/users/@me/channels', json={'id': channel_id}, status=200,
201+
match=[
202+
matchers.json_params_matcher({'recipient_id': user_id})
203+
]
204+
)
205+
responses.add(
206+
responses.POST, f'https://discord.com/api/v10/channels/{channel_id}/messages', json={'id': 'message_id'},
207+
status=200,
208+
match=[
209+
matchers.json_params_matcher({'content': message})
210+
]
211+
)
212+
213+
resp_json = bot_client.send_user_message(user_id, message)
214+
assert resp_json == {'id': 'message_id'}
215+
216+
156217
def test_remove_guild_member_bot_missing_permissionts(responses):
157218
bot_token = 'MTAwNDU0MTg3NTk1NDQwNTQzNg.GFcT5X.Kp4gVn0U1kOPvdwLku-oki7LI_wtbMma2E_ET4'
158219
bot_client = DiscordBotClient(bot_token)

0 commit comments

Comments
 (0)