Compare commits

...

4 Commits

Author SHA1 Message Date
Stefano Assenzo 54175f01cd
Now it's possible change school time table (testing) 2023-01-25 16:41:05 +00:00
Stefano Assenzo f58b8a06c6
Add comments on the code and add a simple slash commands 2023-01-25 16:00:49 +00:00
Stefano Assenzo d07dbac7f5
Add day on message Discord bot 2023-01-25 15:54:00 +00:00
Stefano Assenzo 6dfa4e3bb3
Fix little bug on Discord bot 2023-01-25 15:51:23 +00:00
1 changed files with 39 additions and 8 deletions

View File

@ -2,9 +2,12 @@ from dotenv import load_dotenv
from bson.objectid import ObjectId from bson.objectid import ObjectId
from discord.ext import tasks, commands from discord.ext import tasks, commands
from discord.commands.context import ApplicationContext
from discord.commands import Option
import discord import discord
import pymongo import pymongo
import urllib.parse import urllib.parse
import datetime
import os import os
load_dotenv() # Load .env file load_dotenv() # Load .env file
@ -17,6 +20,15 @@ database = client["website-class"] # Database name
# Collection school time table current # Collection school time table current
collection = database["school-time-table"] collection = database["school-time-table"]
#Date
current_time = datetime.datetime.now()
day = str(current_time.day)
month = str(current_time.month)
year = str(current_time.year)
hour = str(current_time.hour)
minute = str(current_time.minute)
long_date = day + "-" + month + "-" + year + " " + hour + ":" + minute
bot = discord.Bot() bot = discord.Bot()
@bot.event @bot.event
@ -24,6 +36,7 @@ async def on_ready():
print('We have logged in as {0.user}'.format(bot)) print('We have logged in as {0.user}'.format(bot))
bot.loop.create_task(orario()) bot.loop.create_task(orario())
# Search on MongoDB the subject and send a message on Discord if the subject is found
@tasks.loop(seconds=1) @tasks.loop(seconds=1)
async def orario(): async def orario():
documents = collection.find() documents = collection.find()
@ -33,14 +46,32 @@ async def orario():
for day in document['School Subject']: for day in document['School Subject']:
for i, subject in enumerate(document['School Subject'][day]): for i, subject in enumerate(document['School Subject'][day]):
if subject['Subject'] == "CALF1 LINGUA ITALIANA": 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 # 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") channel = bot.get_channel(1063753802638954519)
await ctx.send(f"Subject found: {subject['Subject']} at index: {i}") await channel.send(f"Day: {day}, Hour school: {i}, Subject found: {subject['Subject']} at index: {i}")
@bot.slash_command(name='change_school_time', description='Change school time')
async def change_school_time(
ctx : ApplicationContext,
day: Option(str,
"Select a day",
choices=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
required=True,
),
hour_school: Option(str,
"Select hour school",
choices=["1", "2", "3", "4", "5", "6"],
required=True,
), text: str
):
await ctx.respond(f"Day selected: {day}, Hour school selected: {hour_school}, Text: {text}")
# Update the subject on MongoDB
find_document = list(collection.find({}, {"Date": long_date}))
array_document = find_document[0]["_id"]
collection.update_one(
{"_id": ObjectId(array_document)},
{"$set": {f"School Subject.{day}.{int(hour_school)}.Subject": text}}
)
@bot.command()
async def testpy(ctx):
bot.loop.create_task(orario(ctx))
await ctx.send("testpy")
bot.run(DISCORD_TOKEN) bot.run(DISCORD_TOKEN)