Преглед на файлове

Initial commit

Just the bot
Harupi преди 1 година
родител
ревизия
dd4c0879fa
променени са 5 файла, в които са добавени 87 реда и са изтрити 1 реда
  1. 4 1
      .gitignore
  2. 28 0
      README.md
  3. 23 0
      main.py
  4. BIN
      requirements.txt
  5. 32 0
      today.py

+ 4 - 1
.gitignore

@@ -178,4 +178,7 @@ cython_debug/
 #  exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
 #  refer to https://docs.cursor.com/context/ignore-files
 .cursorignore
-.cursorindexingignore
+.cursorindexingignore
+
+# Data
+data/

+ 28 - 0
README.md

@@ -0,0 +1,28 @@
+# ical-discord-bot
+___
+
+Ical discord bot is a bot that sends your attends for today from an ical.
+
+___
+
+## Deploy the bot
+
+- Clone this repo
+
+- Create an application on https://discord.com/developers/, go to Bot and click Reset Token
+
+- Put your token at the bottom of the main.py file, where it is written to
+
+- Create a data folder at the root of the project and paste your calendar in
+
+- Rename your calendar "cal.ics"
+
+- Create a venv and run ```pip install -r requirements.txt```
+
+- Run the main file
+
+- Add the bot to your server
+
+That's it !
+
+You can now view your events for today with the ```edt?``` command (by default)

+ 23 - 0
main.py

@@ -0,0 +1,23 @@
+import discord
+from today import today
+
+# Creating a client instance
+intents = discord.Intents.default()
+intents.message_content = True
+
+client = discord.Client(intents=intents)
+
+@client.event
+async def on_ready():                           # Print a message when the bot turns on
+    print(f'We have logged in as {client.user}')
+
+@client.event                                   
+async def on_message(message):                  
+    if message.author == client.user:           # Check if the message was sens by the bot
+        return
+
+    if message.content.startswith("edt?"):      # Check if the message starts with 'edt', which is the command we defined to view the timetable.
+        await message.channel.send(today())
+
+client.run("PUT YOUR TOKEN HERE")
+

BIN
requirements.txt


+ 32 - 0
today.py

@@ -0,0 +1,32 @@
+import icalendar
+import datetime
+import zoneinfo
+from pathlib import Path
+
+def today():
+    """Returns a string containing all events for the day, their location and their time"""
+    cal_path = Path("data/cal.ics")                                     # Rename your calendar in cal.ics and put in in a folder called data (or just change the path)
+    cal = icalendar.Calendar.from_ical(cal_path.read_bytes())
+    today = datetime.datetime.now(zoneinfo.ZoneInfo("Europe/Paris"))    # The date of today, in the Paris timezone, choose whichever you want
+    
+
+    attends_tab = []
+
+    for event in cal.events:
+        if event.get("DTSTART").dt.day == today.day and event.get("DTSTART").dt.month == today.month :        # Check every event in the calendar and execute the code if their are today
+            event_desc = "\n"+"### "+event.get("SUMMARY")+" ###"
+
+            event_desc += "\n"+event.get("LOCATION")
+
+            event_desc += "\n"+"<t:"+str(round(event.get("DTSTART").dt.timestamp()))+":t> - <t:"+str(round(event.get("DTEND").dt.timestamp()))+":t>"    # Convert the date in vDDDType to a discord timestamp
+            
+            attends_tab.append((event_desc,event.get("DTSTART").dt.timestamp()))
+    
+    attends_tab_sorted = sorted(attends_tab, key= lambda x: x[1])   # Order every eent of today by ascending timestamp
+
+    msg = "Voici l'emploi du temps du jour :\n"
+    for event in attends_tab_sorted:        # Add all events to the message
+        msg += event[0]
+    
+    return msg
+