-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
205 lines (155 loc) · 7.5 KB
/
main.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import discord
import secrets
from random import randint
from aiosmtplib.errors import SMTPException
from email_sender import send_email
from registration_mail_template import REGISTRATION_MAIL_TEMPLATE
from welcome_msg_template import WELCOME_MSG_TEMPLATE
from course_specification_template import COURSE_SPECIFICATION_TEMPLATE
TUM_STUDENT_ROLE_NAME = "TUM Student"
GUEST_ROLE_NAME = "Guest"
WELCOME_CHANNEL_NAME = "welcome"
REGISTRATION_CHANNEL_NAME = "registration"
RULES_CHANNEL_NAME = "rules"
SELF_INTRODUCTION_CHANNEL_NAME = "self-introduction"
COURSE_CODE_TO_ROLENAME = {
"IEB": "IE Bachelor",
"IEM": "IE Master",
"MTB": "MT Bachelor",
"MIM": "MI Master",
"MAM": "MA Master",
}
intents = discord.Intents.all()
client = discord.Client(intents=intents)
register_codes = []
@client.event
async def on_ready():
print("The bot has started successfully.")
@client.event
async def on_message(message):
"""
This specifies how to react to the messages from users.
"""
try:
# Ignore messages from bots.
if message.author.bot or len(message.content) == 0:
return
try:
# Give Guest role to people who introduce themselves.
guest_role = discord.utils.get(message.guild.roles, name=GUEST_ROLE_NAME)
self_introduction_channel = discord.utils.get(message.author.guild.channels, name=SELF_INTRODUCTION_CHANNEL_NAME)
if message.channel == self_introduction_channel and guest_role not in message.author.roles:
await message.author.add_roles(guest_role)
except AttributeError:
pass
message.content = message.content.split()
if message.content[0] == "/test":
# Email test
try:
if message.content[1] == "mail":
try:
await send_email(
to_address=message.content[2],
subject="TEST",
body="Hello World. This is a test."
)
await message.channel.send("{} The test mail has been sent.".format(message.author.mention))
except SMTPException:
await message.channel.send("{} Sending a test mail failed.".format(message.author.mention))
except IndexError:
await message.channel.send(
"{} Specify the email address to send a test mail.".format(message.author.mention)
)
# Default
except IndexError:
await message.channel.send("{} Hello World. The bot is running.".format(message.author.mention))
if message.content[0] == "/register":
try:
if message.content[1].isalnum() and not message.content[1].isnumeric():
register_code = str(randint(100000, 999999))
register_codes.append(register_code)
try:
try:
await message.delete()
except discord.Forbidden:
pass
await send_email(
to_address="{}@mytum.de".format(message.content[1]),
subject="TUM Heilbronn Discord Server Registration",
body=REGISTRATION_MAIL_TEMPLATE.format(
username=message.author.name,
register_code=register_code,
)
)
except SMTPException:
await message.channel.send(
"{} Sending a registration mail failed.".format(message.author.mention)
)
else:
await message.channel.send(
"{} The registration mail has been sent.".format(message.author.mention)
)
elif message.content[1].isnumeric():
try:
student_role = discord.utils.get(message.guild.roles, name=TUM_STUDENT_ROLE_NAME)
except AttributeError:
await message.channel.send("{} You need to do that in the server.".format(message.author.mention))
else:
if message.content[1] in register_codes:
await message.author.add_roles(student_role)
register_codes.remove(message.content[1])
await message.channel.send(
"{} The student role is added to your account.".format(message.author.mention)
)
else:
await message.channel.send(
"{} The registration code is invalid.".format(message.author.mention)
)
except IndexError:
await message.channel.send("{} Invalid command.".format(message.author.mention))
elif message.content[0] == "/course":
try:
student_role = discord.utils.get(message.guild.roles, name=TUM_STUDENT_ROLE_NAME)
if student_role in message.author.roles:
course_code_to_role = {}
for code, rolename in COURSE_CODE_TO_ROLENAME.items():
course_code_to_role[code] = discord.utils.get(message.guild.roles, name=rolename)
await message.author.add_roles(course_code_to_role[message.content[1]])
await message.channel.send(
"{} You are given the `{}` role.".format(
message.author.mention, course_code_to_role[message.content[1]]
)
)
else:
await message.channel.send(
"{} You need to be registered as a student at first".format(message.author.mention)
)
except IndexError:
await message.channel.send(COURSE_SPECIFICATION_TEMPLATE.format(message.author.mention))
except KeyError:
await message.channel.send("{} Please specify your course.".format(message.author.mention))
except Exception as err:
await message.channel.send("{} Unknow error happened.".format(message.author.mention))
print(err)
# Welcome message
@client.event
async def on_member_join(member):
try:
if member.bot:
return
welcome_channel = discord.utils.get(member.guild.channels, name=WELCOME_CHANNEL_NAME)
rules_channel = discord.utils.get(member.guild.channels, name=RULES_CHANNEL_NAME)
registartion_channel = discord.utils.get(member.guild.channels, name=REGISTRATION_CHANNEL_NAME)
self_introduction_channel = discord.utils.get(member.guild.channels, name=SELF_INTRODUCTION_CHANNEL_NAME)
await welcome_channel.send(
WELCOME_MSG_TEMPLATE.format(
username=member.mention,
rules_channel=rules_channel.mention,
registration_channel=registartion_channel.mention,
self_introduction_channel=self_introduction_channel.mention,
)
)
except Exception as err:
await member.send("{} Unknow error happened.".format(member.mention))
print(err)
client.run(secrets.DISCORD_TOKEN)