Skip to content
Snippets Groups Projects
appointment_view.py 3.08 KiB
Newer Older
  • Learn to ignore specific revisions
  • dnns01's avatar
    dnns01 committed
    import discord
    from discord import File
    
    
    from models import Appointment, Attendee
    
    dnns01's avatar
    dnns01 committed
    
    
    class AppointmentView(discord.ui.View):
    
    dnns01's avatar
    dnns01 committed
            super().__init__(timeout=None)
    
    
    dnns01's avatar
    dnns01 committed
        @discord.ui.button(label='Anmelden', style=discord.ButtonStyle.green, custom_id='appointment_view:accept', emoji="👍")
    
    dnns01's avatar
    dnns01 committed
        async def accept(self, interaction: discord.Interaction, button: discord.ui.Button):
    
            if appointment := Appointment.get_or_none(Appointment.message == interaction.message.id):
                attendee = appointment.attendees.filter(member_id=interaction.user.id)
                if attendee:
                    await interaction.response.send_message("Du bist bereits Teilnehmerin dieses Termins.",
                                                            ephemeral=True)
                    return
                else:
                    Attendee.create(appointment=appointment.id, member_id=interaction.user.id)
    
                    await interaction.message.edit(embed=appointment.get_embed(1 if appointment.reminder_sent and appointment.reminder > 0 else 0))
    
    
            await interaction.response.defer(thinking=False)
    
    dnns01's avatar
    dnns01 committed
        @discord.ui.button(label='Abmelden', style=discord.ButtonStyle.red, custom_id='appointment_view:decline', emoji="👎")
    
    dnns01's avatar
    dnns01 committed
        async def decline(self, interaction: discord.Interaction, button: discord.ui.Button):
    
            if appointment := Appointment.get_or_none(Appointment.message == interaction.message.id):
                attendee = appointment.attendees.filter(member_id=interaction.user.id)
                if attendee:
                    attendee = attendee[0]
                    attendee.delete_instance()
    
                    await interaction.message.edit(embed=appointment.get_embed(1 if appointment.reminder_sent and appointment.reminder > 0 else 0))
    
                else:
                    await interaction.response.send_message("Du kannst nur absagen, wenn du vorher zugesagt hast.",
                                                            ephemeral=True)
                    return
    
            await interaction.response.defer(thinking=False)
    
    dnns01's avatar
    dnns01 committed
    
        @discord.ui.button(label='Download .ics', style=discord.ButtonStyle.blurple, custom_id='appointment_view:ics',
                           emoji="📅")
        async def ics(self, interaction: discord.Interaction, button: discord.ui.Button):
    
            if appointment := Appointment.get_or_none(Appointment.message == interaction.message.id):
                await interaction.response.send_message("", file=File(appointment.get_ics_file(),
                                                                      filename=f"{appointment.title}_{appointment.uuid}.ics"), ephemeral=True)
    
    dnns01's avatar
    dnns01 committed
    
        @discord.ui.button(label='Löschen', style=discord.ButtonStyle.gray, custom_id='appointment_view:delete', emoji="🗑")
        async def delete(self, interaction: discord.Interaction, button: discord.ui.Button):
    
            await interaction.response.defer(thinking=False)
            if appointment := Appointment.get_or_none(Appointment.message == interaction.message.id):
                if interaction.user.id == appointment.author:
                    appointment.delete_instance(recursive=True)
                    await interaction.message.delete()