1+ import time
2+
13import discord
24from discord import Colour , Embed , Member , app_commands
35from discord .ext import commands
46from discord .ext .commands import Context
5- from slaobot import SlaoBot
67from utils .config import settings
78
9+ from slaobot import SlaoBot
10+
11+
12+ class SignUpRequest (discord .ui .View ):
13+ """View that will give starting role to a newcomer"""
14+
15+ def __init__ (self ):
16+ super ().__init__ (timeout = None )
17+
18+ @discord .ui .button (label = 'Принять' , style = discord .ButtonStyle .green , custom_id = 'sur:accept' )
19+ async def accept (self , interaction : discord .Interaction , button : discord .ui .Button ):
20+ if interaction .message is None :
21+ return None
22+
23+ # noinspection PyUnresolvedReferences
24+ await interaction .response .defer ()
25+
26+ # There should be an embed with questionnaire answers
27+ embed : Embed = interaction .message .embeds [0 ]
28+ if not embed :
29+ return None
30+
31+ try :
32+ mention = embed .fields [5 ].value
33+ mention = mention .replace ('<' , '' )
34+ mention = mention .replace ('>' , '' )
35+ mention = mention .replace ('@' , '' )
36+ mention = mention .replace ('!' , '' )
37+
38+ candidate = interaction .guild .get_member (int (mention ))
39+ except discord .NotFound :
40+ await interaction .followup .send ('Не нашел пользователя.' , ephemeral = True )
41+ return None
42+
43+ role = discord .utils .get (interaction .guild .roles , name = 'Служитель' )
44+ try :
45+ await candidate .add_roles (role )
46+ except discord .Forbidden :
47+ await interaction .followup .send ('Нет прав.' , ephemeral = True )
48+ return None
49+
50+ # Role set. Let's update embed and stop the view
51+ embed .set_footer (text = f'{ interaction .user } сделал Служителем | { time .asctime (time .localtime (time .time ()))} ' )
52+ await interaction .message .edit (embed = embed , view = None )
53+ self .stop ()
54+
855
956class SignUpModal (discord .ui .Modal , title = 'Информация о себе' ):
57+ """Modal dialog window with basic questions about newcomer."""
58+
1059 name = discord .ui .TextInput (
1160 label = 'Твой имя' ,
1261 placeholder = 'Как тебя зовут' ,
@@ -62,7 +111,7 @@ async def on_submit(self, interaction: discord.Interaction):
62111 signup_embed .add_field (name = 'Пользователь' , value = format (interaction .user .mention ))
63112 if interaction .user .avatar :
64113 signup_embed .set_thumbnail (url = interaction .user .avatar .url )
65- await signup_channel .send (embed = signup_embed )
114+ await signup_channel .send (embed = signup_embed , view = SignUpRequest () )
66115
67116 # noinspection PyUnresolvedReferences
68117 await interaction .response .send_message (
@@ -76,13 +125,15 @@ async def on_error(self, interaction: discord.Interaction, error: Exception) ->
76125
77126
78127class SignUp (commands .Cog ):
79- def __init__ (self , bot : SlaoBot ):
80- """
81- Cog to greet newcomers and give them some basic questionnaire.
128+ """Cog to greet newcomers and give them some basic questionnaire.
129+
130+ :param bot: Bot instance
131+ """
82132
83- :param bot: Bot instance
84- """
133+ def __init__ (self , bot : SlaoBot ):
85134 self .bot : SlaoBot = bot
135+ # Register the persistent view for listening here.
136+ self .bot .add_view (SignUpRequest ())
86137
87138 @commands .Cog .listener ()
88139 async def on_member_join (self , member : Member ) -> None :
@@ -95,7 +146,7 @@ async def on_member_join(self, member: Member) -> None:
95146 dm_channel = await member .create_dm ()
96147 intro_message = (f'Привет, { format (member .mention )} ! \r \n '
97148 'Приветствуем тебя на Discord сервере гильдии <Адепты Катаклизма>! \r \n '
98- 'Мы играем в Wrath of the Lich King Classic за Альянс на Пламегоре. \r \n '
149+ 'Мы играем в Cataclysm Classic за Альянс на Пламегоре. \r \n '
99150 'Если хочешь вступить к нам в гильдию, то напиши /signup в любом \r \n '
100151 'канале Discord сервера Адептов. \r \n '
101152 'Удачного времяпрепровождения!' )
0 commit comments