added calendar

This commit is contained in:
Tenzing Kandang 2026-03-18 02:09:34 +01:00
parent eeed5b8e03
commit 4da89f496d

View File

@ -1,7 +1,16 @@
import os import os
import discord import discord
from discord.ext import commands import datetime
import google_auth_oauthlib.flow
from weather import OpenWeatherMapAPIClient from weather import OpenWeatherMapAPIClient
from googleapiclient.discovery import build
from discord.ext import commands
global service
service = None
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
service = None
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN") DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
WEATHER_TOKEN = os.getenv("WEATHER_TOKEN") WEATHER_TOKEN = os.getenv("WEATHER_TOKEN")
@ -11,6 +20,40 @@ intents = discord.Intents(messages=True, guilds=True)
intents.message_content = True intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents) bot = commands.Bot(command_prefix='!', intents=intents)
#Calendar authentification
@bot.tree.command()
async def authenticate(interaction: discord.Interaction):
await interaction.response.defer(thinking=True)
global service
try:
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file('./calendar/credentials.json', SCOPES)
credentials = flow.run_local_server(port=0)
service = build('calendar', 'v3', credentials=credentials)
await interaction.followup.send('Successfully authenticated with Google Calendar !')
except Exception as e:
await interaction.followup.send(f'An error occurred: {e}')
@bot.tree.command()
async def events(interaction: discord.Interaction):
if service is None:
await interaction.response.send_message('Please authenticate first using the command authenticate.')
return
now = datetime.datetime.utcnow().isoformat() + 'Z'
events_result =service.events().list(calendarId='primary', timeMin=now, maxResults=1, singleEvents=True, orderBy='startTime').execute()
events = events_result.get('items', [])
if not events:
await interaction.response.send_message('No upcoming events found.')
else:
event_list = "\n".join([f"{event['start'].get('dateTime', event['start'].get('date'))}: {event['summary']}" for event in events])
await interaction.response.send_message(event_list)
#Weather commad tree
@bot.tree.command(name="weather") @bot.tree.command(name="weather")
async def current_weather(interaction: discord.Interaction, location: str): async def current_weather(interaction: discord.Interaction, location: str):
@ -34,7 +77,7 @@ async def current_weather(interaction: discord.Interaction, location: str):
temp = current_weather['main']['temp'] temp = current_weather['main']['temp']
icon = current_weather['weather'][0]['icon'] icon = current_weather['weather']['icon']
embed = discord.Embed( embed = discord.Embed(
title=f"Current weather in {location}", title=f"Current weather in {location}",
description=f"Temperature: {temp}°C\nSky: {weather_condition}", description=f"Temperature: {temp}°C\nSky: {weather_condition}",