Переглянути джерело

Add support of slash commands

Harupi 7 місяців тому
батько
коміт
9bf7ad9f67
4 змінених файлів з 80 додано та 39 видалено
  1. 25 22
      README.md
  2. 1 0
      day_planning.py
  3. 54 17
      main.py
  4. BIN
      requirements.txt

+ 25 - 22
README.md

@@ -1,28 +1,31 @@
 # ical-discord-bot
 ___
 
-Ical discord bot is a bot that sends your attends for today from an ical.
+The **Ical Discord Bot** sends your daily events from an .ics calendar directly to Discord.
 
 ___
 
-## 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)
+## Deploy the Bot
+
+1. Clone this repository.
+2. Create a ".env" file at the root of the project and add the following:
+   ```
+   TOKEN=""
+   GUILD=""
+   ```
+3. Create an application on [Discord Developer Portal](https://discord.com/developers/).
+   - Go to the **Bot** section and click **Reset Token**.
+   - Copy the token and paste it into the '.env' file, inside the quotes after "TOKEN=".
+4. Copy the ID of the server where you want to invite the bot.
+   - Right-click the server name in the sidebar (with Developer Mode enabled).
+   - Paste the ID into the ".env" file, inside the quotes after "GUILD=".
+5. Create a "data" folder at the root of the project and place your calendar file inside.
+6. Rename your calendar file to "cal.ics".
+7. Create a virtual environment and install the dependencies:
+   ```
+   pip install -r requirements.txt
+   ```
+8. Run the main file.
+9. Add the bot to your server.
+
+That’s it! You can now view your events for today using the "/day(today)" command (by default).

+ 1 - 0
today.py → day_planning.py

@@ -56,3 +56,4 @@ def tomorrow():
         msg += event[0]
     
     return msg
+    

+ 54 - 17
main.py

@@ -1,27 +1,64 @@
+### IMPORTS ###
+
+# Discord #
 import discord
-from today import today, tomorrow
+from discord.ext import commands
+from discord import app_commands
+
+# For env variables #
+from os import environ
+from dotenv import load_dotenv
+
+# Other #
+from typing import Literal
+
+# Commands #
+from day_planning import today, tomorrow
+
+
+### BOT CLASS ###
+
+class Bot(commands.Bot):
+    def __init__(self):
+        intents = discord.Intents.default()
+        intents.message_content = True
+        super().__init__(command_prefix = "?", intents = intents)
+
+    async def setup_hook(self):
+        await self.tree.sync(guild= discord.Object(id=guild))
+        print("Bot started")
+
+    async def on_command_error(self, ctx, error):
+        await ctx.reply(error, ephemeral = True)
+
+
+### VARIABLES ###
+
+load_dotenv()
+token = environ["TOKEN"]
+guild = environ["GUILD"]
+
+bot = Bot()
 
-# Creating a client instance
-intents = discord.Intents.default()
-intents.message_content = True
 
-client = discord.Client(intents=intents)
+### COMMANDS ###
 
-@client.event
-async def on_ready():                           # Print a message when the bot turns on
-    print(f'We have logged in as {client.user}')
+@bot.hybrid_command(name="day", with_app_command=True, description="Give your attends for one day")
+@app_commands.guilds(discord.Object(id=guild))
+@app_commands.describe(date="The day for which your attends will be displayed")
+async def day(ctx, date: Literal["today", "tomorrow"]):
 
-@client.event                                   
-async def on_message(message):                  
-    if message.author == client.user:           # Check if the message was sens by the bot
-        return
+    if date == "today":
+        msg = today()
 
-    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())
+    elif date == "tomorrow":
+        msg = tomorrow()
+    
+    else:
+        msg = "This date doesn't exist"
 
-    if message.content.startswith("demain?"):
-        await message.channel.send(tomorrow())
+    await ctx.reply(msg)
 
 
-client.run("YOUR_TOKEN_HERE")
+bot.run(token)
 

BIN
requirements.txt