From bf041c0bbb1997ae492f5e6c1260f5563727c64f Mon Sep 17 00:00:00 2001 From: Stefano Assenzo Date: Sun, 22 Jan 2023 16:34:33 +0000 Subject: [PATCH] So now the bot found School Subject Italiano and send a message --- .env.example | 4 ++- src/events/school_time/discord_bot.py | 45 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/events/school_time/discord_bot.py diff --git a/.env.example b/.env.example index 61a7b7a..d10a015 100644 --- a/.env.example +++ b/.env.example @@ -12,4 +12,6 @@ DOWNLOAD_FOLDER = "" # Nuvola USERNAME_NUVOLA = "" -PASSWORD_NUVOLA = "" \ No newline at end of file +PASSWORD_NUVOLA = "" + +DISCORD_TOKEN = "" \ No newline at end of file diff --git a/src/events/school_time/discord_bot.py b/src/events/school_time/discord_bot.py new file mode 100644 index 0000000..afb29e0 --- /dev/null +++ b/src/events/school_time/discord_bot.py @@ -0,0 +1,45 @@ +from dotenv import load_dotenv +from bson.objectid import ObjectId + +from discord.ext import tasks, commands +import discord +import pymongo +import urllib.parse +import os + +load_dotenv() # Load .env file +DISCORD_TOKEN = os.getenv('DISCORD_TOKEN') # Discord token +PASSWORD_MONGODB = os.getenv('PASSWORD_MONGODB') # Password for MongoDB +URL_MONGODB = os.getenv('URL_MONGODB') # URL for MongoDB +mongo_url = "mongodb+srv://elci:" + \ + urllib.parse.quote_plus(PASSWORD_MONGODB) + \ + URL_MONGODB # URL for MongoDB (with password) +client = pymongo.MongoClient(mongo_url) # Connect to MongoDB +database = client["website-class"] # Database name +# Collection school time table current +collection = database["school-time-table"] + +bot = discord.Bot() + + + +@tasks.loop(seconds=1) +async def orario(ctx): + documents = collection.find() + +# Iterate through the documents + for document in documents: + for day in document['School Subject']: + for i, subject in enumerate(document['School Subject'][day]): + if subject['Subject'] == "CALF1 LINGUA ITALIANA": + print(f"Subject found: {subject['Subject']} at index: {i}") + # Send a message on channel #general with the subject found and the index of the subject + channel = bot.get_channel(1063753802638954519).send("bot is online") + await ctx.send(f"Subject found: {subject['Subject']} at index: {i}") + + +@bot.command() +async def testpy(ctx): + bot.loop.create_task(orario(ctx)) + await ctx.send("testpy") +bot.run(DISCORD_TOKEN) \ No newline at end of file