-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzat113_check_in.py
103 lines (70 loc) · 3.84 KB
/
zat113_check_in.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
#Handles most of the code for the checking in assignment in ZAT113
#Check .git for creation and updates information
#Author: Johannes Nicholas, https://github.com/JohannesNicholas
import discord # pycord
import db_handler as db
import csv
# When a message is sent anywhere
async def message(message):
#ignore messages in other channels
if message.channel.id not in [994167372942413906, 994121873816293407]:
return
#ignore messages from bots
if message.author.bot:
return
#if we don't have the student id
if db.get_student_id(message.author.id) == -1:
#ask the user for their student id
await message.channel.send(f"""Thank you <@{message.author.id}> for your check in!
I don't seem to have your student ID yet.""",
view=View(check_in_msg=message))
else:
await message.add_reaction('🎫')
#the view for the replies to the check in message
class View(discord.ui.View):
check_in_msg = None
#initialization
def __init__(self, check_in_msg, *args, **kwargs):
super().__init__(*args, **kwargs)
self.check_in_msg = check_in_msg
self.user_id = check_in_msg.author.id
supportButton = discord.ui.Button(label='What is my student ID?', style=discord.ButtonStyle.url, url='https://askus.utas.edu.au/app/answers/detail/a_id/1060/~/what-is-my-utas-student-id-number%3F')
self.add_item(supportButton)
@discord.ui.button(label="Enter student ID", style=discord.ButtonStyle.primary) # Create a button
async def button_callback(self, button, interaction):
await interaction.response.send_modal(MyModal(title="Enter student ID", check_in_msg=self.check_in_msg, user_id=self.user_id))
# the modal for entering student IDs
class MyModal(discord.ui.Modal):
check_in_msg = None
def __init__(self, check_in_msg, user_id, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.check_in_msg = check_in_msg
self.user_id = user_id #the user id of the user who checked in
self.add_item(discord.ui.InputText(label="Student id", placeholder="123456", min_length=6, max_length=6))
async def callback(self, interaction: discord.Interaction):
#check the student id
sid = self.children[0].value
if not sid.isdigit() or len(sid) != 6:
#tell the user they entered an invalid student id
await interaction.response.send_message(f"'{sid}' does not seem to be a student ID", ephemeral=True)
return
db.set_student_id(interaction.user.id, int(sid))#save the student id
await interaction.response.send_message(f"Thank you <@{interaction.user.id}> for your student ID", ephemeral=True)
#if the user who checked in entered their student id
if interaction.user.id == self.user_id:
await interaction.message.delete()
await self.check_in_msg.add_reaction('🎫')
#returns a csv file of all the check ins
async def get_check_ins(channel_id:int, bot:discord.Bot):
file_path = 'check_ins.csv' # a temporary file to store the check ins
with open(file_path, 'w', encoding="utf-8") as csv_file:
writer = csv.writer(csv_file)
writer.writerow(['Student ID', 'Message', 'Time', 'Link to message', 'student discord name', 'student discord ID'])
channel = bot.get_channel(channel_id)
async for message in channel.history(limit=None, oldest_first=True):
time = message.created_at.strftime("%d/%m/%Y %H:%M:%S")
if message.edited_at is not None:
time = message.edited_at.strftime("%d/%m/%Y %H:%M:%S")
writer.writerow([db.get_student_id(message.author.id), message.content, time, message.jump_url, message.author.display_name, message.author.id])
print("compiled all messages")
return discord.File(fp=file_path)