Skip to content

Commit

Permalink
Made DevPro Bot send warning messages to user with no active subscrip…
Browse files Browse the repository at this point in the history
…tions

part of #4791
  • Loading branch information
renzo authored and renzon committed Mar 21, 2024
1 parent 7ea930b commit 774a0d4
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 9 deletions.
32 changes: 32 additions & 0 deletions pythonpro/discord/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,38 @@ class DiscordBotClient:
def __init__(self, bot_token: str):
self._bot_token = bot_token

def get_dm_channel(self, discord_user_id: str):
"""
Reference: https://discord.com/developers/docs/resources/user#create-dm
"""
headers = {
'Authorization': f'Bot {self._bot_token}'
}
r = requests.post(
f'{_BASE_ENDPOINT_URI}/users/@me/channels',
headers=headers,
json={'recipient_id': discord_user_id}
)
r.raise_for_status()
dm_channel = r.json()
return dm_channel

def create_message(self, channel_id: str, msg: str) -> dict:
"""
Reference: https://discord.com/developers/docs/resources/channel#create-message
"""
headers = {
'Authorization': f'Bot {self._bot_token}'
}
r = requests.post(
f'{_BASE_ENDPOINT_URI}/channels/{channel_id}/messages',
headers=headers,
json={'content': msg}
)
r.raise_for_status()
message = r.json()
return message

def get_member(self, discord_user_id: str):
headers = {
'Authorization': f'Bot {self._bot_token}'
Expand Down
5 changes: 5 additions & 0 deletions pythonpro/discord/bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.conf import settings

from pythonpro.discord.api_client import DiscordBotClient

discord_bot_client = DiscordBotClient(settings.DISCORD_APP_BOT_TOKEN)
4 changes: 1 addition & 3 deletions pythonpro/discord/facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

from django.conf import settings

from pythonpro.discord.api_client import DiscordBotClient
from pythonpro.discord.bot import discord_bot_client
from pythonpro.discord.tasks import clean_discord_user

discord_bot_client = DiscordBotClient(settings.DISCORD_APP_BOT_TOKEN)

logger = logging.getLogger(__name__)


Expand Down
32 changes: 32 additions & 0 deletions pythonpro/discord/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from celery import shared_task

from pythonpro.discord.bot import discord_bot_client
from pythonpro.discord.models import DiscordLead
from pythonpro.memberkit.models import Subscription

Expand All @@ -28,4 +29,35 @@ def clean_discord_user(discord_user_id):
discord_id=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)

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


msg = """Olá, sou o bot da DevPro no Discord.
Eu não identifiquei sua conta de Discord em nosso sistema. Então peço a gentileza de você conectar suas conta:
https://l.dev.pro.br/comunidade
Para fazer essa conexão você precisa ter uma assinatura com acesso ao discord Ativa.
Você pode conferir seu histórico de assinaturas aqui:
https://painel.dev.pro.br
Você pode renovar sua assinatura utilizando o link:
https://painel.dev.pro.br/checkout/pagarme/renovacao-comunidade-devpro
Na próxima semana, usuários sem assinatura ativa serão removidos do servidor do Discord.
Qualquer dúvida, mande mensagem no canal #suporte do servidor do Discord da DevPro:
https://discord.com/channels/971162582624903288/979392834308280380
Eu vou mandar essa mensagem novamente até o dia 26/03/2024. Depois dessa data, sua conta poderá ser removida.
Um abraço do Bot da DevPro
"""
30 changes: 24 additions & 6 deletions pythonpro/discord/tests/test_clean_discord_user.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import pytest
from responses import matchers

from pythonpro.discord.models import DiscordLead, DiscordUser
from pythonpro.discord.tasks import clean_discord_user
from model_bakery import baker
Expand All @@ -20,15 +23,31 @@ def test_active_user(db):
assert user.status == DiscordLead.Status.ACTIVE


def test_no_discord_user(db):
discord_user_id = '1055109241507160165'
discord_user_id = '1055109241507160165'


@pytest.fixture
def mock_discord_bot_msg_requests(responses):
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': discord_user_id})
]
)
responses.add(
responses.POST, f'https://discord.com/api/v10/channels/{channel_id}/messages', json={'id': 'message_id'},
status=200
)


def test_no_discord_user(db, mock_discord_bot_msg_requests):
clean_discord_user(discord_user_id)
user = DiscordLead.objects.get(discord_id=discord_user_id)
assert user.status == DiscordLead.Status.INACTIVE


def test_subscription_inactive(db):
discord_user_id = '1055109241507160165'
def test_subscription_inactive(db, mock_discord_bot_msg_requests):
django_user = baker.make(DiscordUser, discord_id=discord_user_id).user
subscription_type = baker.make(SubscriptionType, has_discord_access=True)
baker.make(
Expand All @@ -42,8 +61,7 @@ def test_subscription_inactive(db):
assert user.status == DiscordLead.Status.INACTIVE


def test_subscription_type_has_no_discord_access(db):
discord_user_id = '1055109241507160165'
def test_subscription_type_has_no_discord_access(db, mock_discord_bot_msg_requests):
django_user = baker.make(DiscordUser, discord_id=discord_user_id).user
subscription_type = baker.make(SubscriptionType, has_discord_access=False)
baker.make(
Expand Down

0 comments on commit 774a0d4

Please sign in to comment.