-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiscord_bot.py
More file actions
91 lines (70 loc) · 3.13 KB
/
discord_bot.py
File metadata and controls
91 lines (70 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import discord
from discord.ext import commands
from discord import ui
import urllib.parse
import os
from dotenv import load_dotenv
load_dotenv()
DISCORD_TOKEN = os.getenv('DISCORD_TOKEN')
AUTH_CHANNEL_ID = int(os.getenv('AUTH_CHANNEL_ID'))
GUILD_ID = int(os.getenv('GUILD_ID'))
CLIENT_ID_42 = os.getenv('CLIENT_ID_42')
REDIRECT_URI = os.getenv('REDIRECT_URI')
intents = discord.Intents.default()
intents.members = True
class AuthButton(ui.View):
def __init__(self, bot):
super().__init__(timeout=None)
self.bot = bot
@ui.button(label="🔑 S'authentifier via 42", style=discord.ButtonStyle.primary, custom_id="persistent_auth_button")
async def auth_callback(self, interaction: discord.Interaction, button: ui.Button):
user_id = str(interaction.user.id)
params = {
'client_id': CLIENT_ID_42,
'redirect_uri': REDIRECT_URI,
'response_type': 'code',
'scope': 'public',
'state': user_id
}
auth_url = 'https://api.intra.42.fr/oauth/authorize?' + urllib.parse.urlencode(params)
ephemeral_message_content = (
f"Bonjour {interaction.user.name},\n\n"
f"**⚠️ ATTENTION :** Ne partagez ce lien avec personne.\n\n"
f"Cliquez sur le lien ci-dessous pour vous connecter avec votre compte 42 :\n\n"
f"**{auth_url}**\n\n"
"Une fois connecté, votre pseudonyme sur le serveur sera mis à jour."
)
try:
await interaction.response.send_message(
ephemeral_message_content,
ephemeral=True
)
except discord.Forbidden:
await interaction.response.send_message("Je ne peux pas vous envoyer de message privé. Veuillez autoriser les DMs sur ce serveur.", ephemeral=True)
intents = discord.Intents.all()
intents.members = True
bot = commands.Bot(command_prefix="!", intents=intents)
@bot.event
async def on_ready():
activity = discord.Game(name="Rocket League",)
await bot.change_presence(status=discord.Status.online, activity=activity)
channel = bot.get_channel(AUTH_CHANNEL_ID)
if channel:
try:
auth_message_content_key = "Cliquez sur le bouton ci-dessous."
history = [msg async for msg in channel.history(limit=5)]
if not any(auth_message_content_key in m.content for m in history if m.author == bot.user):
await channel.send(
auth_message_content_key + " Vous recevrez un lien pour vous connecter grace à l'Intra de 42.",
view=AuthButton(bot)
)
else:
print("Message d'authentification déjà trouvé, saut de l'envoi.")
except Exception as e:
print(f"ERREUR: Impossible d'envoyer le message d'authentification : {e}")
else:
print(f"ERREUR: Canal d'authentification avec l'ID {AUTH_CHANNEL_ID} non trouvé.")
print(f'{bot.user} est connecté à Discord ! Statut: En ligne.')
# --- Lancement du Bot ---
if __name__ == '__main__':
bot.run(DISCORD_TOKEN)