Club main and event_details

This commit is contained in:
Rachit Agrawal 2024-04-22 17:29:01 +00:00
parent 425d7766f0
commit a82ea12c0f
5 changed files with 64 additions and 73 deletions

View File

@ -23,13 +23,13 @@ This script creates events across -
3.2 Guild ID [`DISCORD_GUILD_ID`] (developersIndia => `1229786646468362260`) 3.2 Guild ID [`DISCORD_GUILD_ID`] (developersIndia => `1229786646468362260`)
3.3 Calender Id [`GOOGLE_CALENDER_ID`] (developerIndia => `9f1337e4154910eb1bdb3bfac32b88f69546468b1281a6db58f50a909df5049f@group.calendar.google.com`) 3.3 calendar Id [`GOOGLE_calendar_ID`] (developerIndia => `9f1337e4154910eb1bdb3bfac32b88f69546468b1281a6db58f50a909df5049f@group.calendar.google.com`)
4. Connect Google Calender through [Google cloud Console](https://console.cloud.google.com/) 4. Connect Google calendar through [Google cloud Console](https://console.cloud.google.com/)
4.1 Create a Project on Google Cloud Console 4.1 Create a Project on Google Cloud Console
4.2 Search for Calender API and enable it 4.2 Search for calendar API and enable it
4.3 Create Credentials -> OAuth Client ID -> Application type as Desktop 4.3 Create Credentials -> OAuth Client ID -> Application type as Desktop

View File

@ -1,50 +1,47 @@
from datetime import datetime, timedelta from datetime import datetime, timedelta
import os
from dotenv import load_dotenv
import event_details
import discord import discord
from discord.ext import commands from discord.ext import commands
load_dotenv() def create_discord_event(bot_token, guild_id,
bot_token = os.getenv('DISCORD_BOT_TOKEN') EVENT_NAME, EVENT_DESCRIPTION, EVENT_LOCATION, EVENT_DATE, EVENT_START_TIME, EVENT_END_TIME
guild_id = int(os.getenv('DISCORD_GUILD_ID')) ):
bot = commands.Bot(command_prefix='!', intents=discord.Intents.all()) bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())
@bot.event @bot.event
async def on_ready(): async def on_ready():
# Convert the date string to a datetime object # Convert the date string to a datetime object
date_object = datetime.strptime(event_details.EVENT_DATE, "%Y-%m-%d") date_object = datetime.strptime(EVENT_DATE, "%Y-%m-%d")
# Convert the time string to a datetime object # Convert the time string to a datetime object
start_time_object = datetime.strptime(event_details.EVENT_START_TIME, "%H:%M") start_time_object = datetime.strptime(EVENT_START_TIME, "%H:%M")
end_time_object = datetime.strptime(event_details.EVENT_END_TIME, "%H:%M") end_time_object = datetime.strptime(EVENT_END_TIME, "%H:%M")
# Combine the date and time objects # Combine the date and time objects
st = date_object.replace(hour=start_time_object.hour, minute=start_time_object.minute).astimezone() st = date_object.replace(hour=start_time_object.hour, minute=start_time_object.minute).astimezone()
et = date_object.replace(hour=end_time_object.hour, minute=end_time_object.minute).astimezone() et = date_object.replace(hour=end_time_object.hour, minute=end_time_object.minute).astimezone()
gg = bot.get_guild(guild_id) gg = bot.get_guild(guild_id)
try: try:
event = await gg.create_scheduled_event( event = await gg.create_scheduled_event(
name=event_details.EVENT_NAME, name=EVENT_NAME,
entity_type=discord.EntityType.external, entity_type=discord.EntityType.external,
description=event_details.EVENT_DESCRIPTION, description=EVENT_DESCRIPTION,
start_time=st, start_time=st,
end_time=et, end_time=et,
location=event_details.EVENT_LOCATION, location=EVENT_LOCATION,
privacy_level=discord.PrivacyLevel.guild_only, privacy_level=discord.PrivacyLevel.guild_only,
) )
print("Discord Event: ", event.url) print("Discord Event: ", event.url)
except Exception as e:
print(e)
await bot.close()
except Exception as e: # Run the bot
print(e) bot.run(bot_token)
await bot.close()
# Run the bot
bot.run(bot_token)
# END # END

View File

@ -1,17 +0,0 @@
# To get the event details from the user
def get_input():
NAME = input("Event title: ")
DESCRIPTION = input("Event description: ")
LOCATION = input("Event location (red -> for developersindia subreddit): ")
if LOCATION == "red":
LOCATION = "https://www.reddit.com/r/developersindia/"
DATE = input("Enter the event date (yyyy-mm-dd): ")
START_TIME = input("Enter the event start time (hh:mm)- ")
END_TIME = input("Enter the event end time (hh:mm)- ")
return NAME, DESCRIPTION, LOCATION, DATE, START_TIME, END_TIME
EVENT_NAME, EVENT_DESCRIPTION, EVENT_LOCATION, EVENT_DATE, EVENT_START_TIME, EVENT_END_TIME = get_input()
# END

View File

@ -1,19 +1,13 @@
import os.path import os.path
import event_details as main
from dotenv import load_dotenv
from google.auth.transport.requests import Request from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build from googleapiclient.discovery import build
load_dotenv()
calendar_id = os.getenv('GOOGLE_CALENDAR_ID')
SCOPES = ['https://www.googleapis.com/auth/calendar'] SCOPES = ['https://www.googleapis.com/auth/calendar']
def create_event(name, description, location, date, start_time, end_time): def create_google_calendar_event(calendar_id, name, description, location, date, start_time, end_time):
creds = None creds = None
@ -54,13 +48,11 @@ def create_event(name, description, location, date, start_time, end_time):
event = service.events().insert(calendarId=calendar_id, body=event).execute() event = service.events().insert(calendarId=calendar_id, body=event).execute()
print('Google Calender: %s' % (event.get('htmlLink'))) print('Google calendar: %s' % (event.get('htmlLink')))
return event return event
except Exception as e: except Exception as e:
print(e) print(e)
return None return None
create_event(main.EVENT_NAME, main.EVENT_DESCRIPTION, main.EVENT_LOCATION, main.EVENT_DATE, main.EVENT_START_TIME, main.EVENT_END_TIME)
# END # END

View File

@ -1,12 +1,31 @@
# SCRIPT import os
from dotenv import load_dotenv
# Execute event_details.py load_dotenv()
import event_details bot_token = os.getenv('DISCORD_BOT_TOKEN')
guild_id = int(os.getenv('DISCORD_GUILD_ID'))
calendar_id = os.getenv('GOOGLE_CALENDAR_ID')
# Execute discord_.py def get_input():
import discord_ NAME = input("Event title: ")
DESCRIPTION = input("Event description: ")
LOCATION = input("Event location (red -> for developersindia subreddit): ")
if LOCATION == "red":
LOCATION = "https://www.reddit.com/r/developersindia/"
DATE = input("Enter the event date (yyyy-mm-dd): ")
START_TIME = input("Enter the event start time (hh:mm)- ")
END_TIME = input("Enter the event end time (hh:mm)- ")
# Execute google_calender.py return NAME, DESCRIPTION, LOCATION, DATE, START_TIME, END_TIME
import google_calender_
EVENT_NAME, EVENT_DESCRIPTION, EVENT_LOCATION, EVENT_DATE, EVENT_START_TIME, EVENT_END_TIME = get_input()
# Execute create_discord_event
from discord_ import create_discord_event
create_discord_event(bot_token, guild_id, EVENT_NAME, EVENT_DESCRIPTION, EVENT_LOCATION, EVENT_DATE, EVENT_START_TIME, EVENT_END_TIME)
# Execute google_calendar_event
from google_calendar_ import create_google_calendar_event
create_google_calendar_event(calendar_id, EVENT_NAME, EVENT_DESCRIPTION, EVENT_LOCATION, EVENT_DATE, EVENT_START_TIME, EVENT_END_TIME)
# END # END