-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
343 lines (320 loc) · 13.9 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import interactions, os, firebase_admin, logging
from firebase_admin import credentials, firestore
from datetime import datetime as dt
# These are basic inits for discord bot to function corrrectly
import secret
env = secret.prod
bot = interactions.Client(
token = env.token,
default_scope = env.scope,
)
bot.load("interactions.ext.persistence")
from interactions.ext.persistence import keygen
keygen()
from interactions.utils.get import get
# These are basic inits for firestore
cred = credentials.Certificate("credentials.json") # <-Not for public
firebase_admin.initialize_app(cred)
db = firestore.client()
# These are basic inits for logging
logging.basicConfig(
format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S'
)
#------------------------Helper Functions------------------------
def check_df(collection, name):
doc_ref = db.collection(collection).document(name)
return doc_ref
def id_to_name(discord_id, collection:str):
try:
entry = check_df(collection, discord_id).get()
if entry.exists :
return entry.to_dict()['name']
except:
print("Somethings wrong with id_to_name")
def validate_date(date_text):
try:
return dt.strptime(f'{date_text}', '%Y-%m-%d').date()
except:
return False
def validate_time(time_text):
try:
return dt.strptime(f'{time_text}', '%H:%M').strftime("%H:%M")
except:
return False
#----------------------Main Code Starts Here----------------------
name = interactions.TextInput(
style=interactions.TextStyleType.SHORT,
label="姓名:",
custom_id="name_input"
)
studentid = interactions.TextInput(
style=interactions.TextStyleType.SHORT,
label="學號:",
custom_id="studentid_input",
min_length=6,
max_length=9,
)
@bot.command(
name="verify",
description="自助領取社員身分組",
)
async def verify(ctx):
modal = interactions.Modal(
title="社員身分組自助認證",
custom_id="verify_modal",
components=[name, studentid]
)
await ctx.popup(modal)
@bot.modal("verify_modal")
async def modal_response(ctx, name: str, student_id: int):
discord_id = f'{ctx.author.username}#{ctx.author.discriminator}'
logging.info(f"Discord Account: {discord_id} | Name: {name} | StudentID: {student_id}")
####### 尚未完成將Document ID換成Discord ID的自動取得身分組功能!
doc_ref = check_df(u"1111-member", name)
entry = doc_ref.get()
record = entry.to_dict()
# Authentication logic(check if name and student ID matches the record in database)
try:
if entry.exists and record['student_id'] == student_id:
await ctx.send(f"✅社員核對通過,請稍後...", ephemeral=True)
#Replace Database Record with Discord_ID as Key
db.collection('1111-cadre').document(discord_id).set({
u'name': name,
u'student_id': student_id
})
doc_ref.delete()
logging.info(f" - Successfully Updated Discord Account for {name}")
# Gets role ID
roles, = interactions.search_iterable(ctx.guild.roles, name='第2屆社員 2nd Gen. Club Member')
# Add role to user
await ctx.author.add_role(roles.id)
logging.info(f" - Successfully Added Role to {name}")
# Confirm
await ctx.channel.send(f"{name}您好,已將您加入 `第2屆社員 2nd Gen. Club Member` 身分組!")
else:
# In case cadre needs to add member role
doc_ref = check_df(u"1111-cadre", name)
entry = doc_ref.get()
record = entry.to_dict()
if entry.exists and record['student_id'] == student_id:
# Add the user's discord account name into the database
doc_ref.update({
u'discord_id': f'{ctx.author.username}#{ctx.author.discriminator}'
})
await ctx.send(f"已將您的Discord帳號登錄至資料庫,謝謝!", ephemeral=True)
logging.info(" - Successfully Update Discord Account for {name}")
return
else:
await ctx.send(f"驗證有誤,請確認姓名及學號是否正確。如有疑問,請透過 <#1024724411074498591> 頻道回報問題,謝謝!", ephemeral=True)
except:
await ctx.send(f"驗證有誤,請確認姓名及學號是否正確。如有疑問,請透過 <#1024724411074498591> 頻道回報問題,謝謝!", ephemeral=True)
#----------------------幹部----------------------
@bot.command()
async def eip(ctx: interactions.CommandContext):
"""【幹部專用】社團資訊入口 - Club Information Portal"""
pass
event_dict = {
"例會": "meeting",
"社課": "event",
"檢討會": "AAR",
"專案": "side-project"
}
#-----------請假-----------
@eip.subcommand()
async def leave(ctx):
"""請假"""
leave_menu = interactions.SelectMenu(
custom_id="leave_menu",
options=[
interactions.SelectOption(label="例會", value="例會"),
interactions.SelectOption(label="社課", value="社課"),
interactions.SelectOption(label="檢討會", value="檢討會"),
interactions.SelectOption(label="專案", value="專案"),
],
)
discord_id = f'{ctx.author.username}#{ctx.author.discriminator}'
entry = check_df(u"1111-cadre", discord_id).get()
if entry.exists:
await ctx.send("請選擇欲請假的活動類型", components=leave_menu)
else:
await ctx.send(":warning: 這個功能僅限現任幹部使用", ephemeral=True)
@bot.component("leave_menu")
async def callback(ctx, response: str):
event_selection, *_ = response
custom_id = interactions.ext.persistence.PersistentCustomID(
bot,
"leave_form",
event_dict[event_selection], # Persistence extension will crash when encountering Chinese, [see here](https://discord.com/channels/789032594456576001/1037090379935256576/1037386313592229928)
)
modal = interactions.Modal(
title=f"【{event_selection}】請假單",
components=[
interactions.TextInput(
style=interactions.TextStyleType.SHORT,
label="請假時間(YYYY-MM-DD)",
custom_id="leave_date",
min_length=10,
max_length=10
),
interactions.TextInput(
style=interactions.TextStyleType.SHORT,
label="請假類別(按學校分類)",
custom_id="leave_type",
),
interactions.TextInput(
style=interactions.TextStyleType.PARAGRAPH,
label="請假原因",
custom_id="leave_reason"
)
# type: ignore
],
custom_id = str(custom_id)
)
await ctx.popup(modal)
await ctx.message.delete()
@bot.persistent_modal("leave_form")
async def modal_response(ctx, event_type, leave_date: str, leave_type: str, leave_reason: str):
leave_date = validate_date(leave_date)
if leave_date:
leave_delta = (leave_date - dt.today().date()).days
if leave_delta > 0: #確保不可在當天或逾期請假
channel = await get(bot, interactions.Channel, object_id=env.leave_channel)
discord_id = f'{ctx.author.username}#{ctx.author.discriminator}'
timestamp = dt.now().isoformat()
leave_name = list(event_dict.keys())[list(event_dict.values()).index(event_type)]
# logging.info(f"Leave =>")
embeds=interactions.Embed(title=f"【{leave_name}】請假單", color=0x00bfff)
embeds.set_author(name=f"{id_to_name(discord_id, '1111-cadre')}", icon_url=f"{ctx.author.avatar_url}?size=1024")
embeds.set_thumbnail(url="https://media.discordapp.net/attachments/1031820876032782346/1056797437676769330/3e5d8d13beec7333.png")
embeds.add_field(name="假別", value=f"{leave_type}\a\a\a\a", inline=True)
embeds.add_field(name="時間", value=f"{leave_date}", inline=True)
embeds.add_field(name="請假原因", value=f"{leave_reason}", inline=False)
embeds.set_footer(text=f"假單序號:{timestamp}")
result = await channel.send(embeds=embeds)
btn_custom_id = interactions.ext.persistence.PersistentCustomID(
bot,
"btn_revoke_leave",
[str(result.id), timestamp],
)
db.collection(u'1111-leave').document(timestamp).set({
u'date': timestamp,
u'name': discord_id,
u'type': leave_type,
u'reason': leave_reason
})
db.collection(u'1111-cadre').document(discord_id).update({
u'leave_record': firestore.ArrayUnion([timestamp])
})
button = interactions.Button(
style = interactions.ButtonStyle.DANGER,
label = "↻ 撤回",
custom_id = str(btn_custom_id)
)
await ctx.send("", components=button)
else:
await ctx.send("請假不可於當天/逾期請!\n如有急事非請不可,請在 <#1022436666251677737> 表示請假原因。", ephemeral=True)
else:
await ctx.send("時間格式不正確,請確認是否為 `MM/DD`", ephemeral=True)
@bot.persistent_component("btn_revoke_leave")
async def button_response(ctx, payload: list):
btn_custom_id, timestamp = payload
discord_id = f'{ctx.author.username}#{ctx.author.discriminator}'
db.collection(u'1111-leave').document(timestamp).delete()
db.collection(u'1111-cadre').document(discord_id).update({
u'leave_record': firestore.ArrayRemove([timestamp])
})
await ctx.message.edit(f"已撤回假單")
message = await get(bot, interactions.Message, object_id=btn_custom_id)
await message.delete()
#-----------公告-----------
announcement_dict = {
"活動公告": "event",
"會議通知": "meeting"
}
@eip.subcommand()
async def announcement(ctx):
"""公告"""
announcement_menu = interactions.SelectMenu(
custom_id="announcement_menu",
options=[
interactions.SelectOption(label="活動公告", value="活動公告"),
interactions.SelectOption(label="會議通知", value="會議通知"),
],
)
discord_id = f'{ctx.author.username}#{ctx.author.discriminator}'
entry = check_df(u"1111-cadre", discord_id).get()
if entry.exists:
await ctx.send("請選擇欲公告類型", components=announcement_menu)
else:
await ctx.send(":warning: 這個功能僅限現任幹部使用", ephemeral=True)
@bot.component("announcement_menu")
async def callback(ctx, response: str):
announcement_selection, *_ = response
custom_id = interactions.ext.persistence.PersistentCustomID(
bot,
"announcement_form",
announcement_dict[announcement_selection],
)
modal = interactions.Modal(
title=f"【{announcement_selection}】",
components=[
interactions.TextInput(
style=interactions.TextStyleType.SHORT,
label="標題",
custom_id="announcement_title"
),
interactions.TextInput(
style=interactions.TextStyleType.SHORT,
label="日期 YYYY-MM-DD(選填)",
custom_id="announcement_date",
required=False
),
interactions.TextInput(
style=interactions.TextStyleType.SHORT,
label="時間 HH:MM(選填)",
custom_id="announcement_time",
required=False
),
interactions.TextInput(
style=interactions.TextStyleType.SHORT,
label="地點(選填)",
custom_id="announcement_location",
required=False
),
interactions.TextInput(
style=interactions.TextStyleType.PARAGRAPH,
label="內容",
custom_id="announcement_content"
)
# type: ignore
],
custom_id = str(custom_id)
)
await ctx.popup(modal)
await ctx.message.delete()
@bot.persistent_modal("announcement_form")
async def modal_response(ctx, announcement_type, announcement_title: str, announcement_date:str, announcement_time:str, announcement_location:str, announcement_content: str):
if announcement_date:
announcement_date = validate_date(announcement_date)
if not announcement_date:
await ctx.send("日期格式不正確,請確認是否為 `YYYY-MM-DD`", ephemeral=True)
return
if announcement_time:
announcement_time = validate_time(announcement_time)
if not announcement_time:
await ctx.send("時間格式不正確,請確認是否為 `HH-MM`", ephemeral=True)
return
channel = await get(bot, interactions.Channel, object_id=env.announcement_channel)
name = list(announcement_dict.keys())[list(announcement_dict.values()).index(announcement_type)]
msg = f"【{name}】\n" +\
f"標題:{announcement_title}\n" +\
(f"日期:{announcement_date}\n" if announcement_date else "") +\
(f"時間:{announcement_time}\n" if announcement_time else "") +\
(f"地點:{announcement_location}\n" if announcement_location else "") +\
f"主持人:{announcement_content}"
await channel.send(msg)
await ctx.send("已發公告")
await ctx.message.delete()
bot.start()