ADD: Create Scheduled events on Discord_Server and Google_Calender

This commit is contained in:
Rachit Agrawal 2024-04-17 13:35:44 +00:00
parent 58746803ee
commit bd1db29d85
7 changed files with 197 additions and 0 deletions

View File

@ -0,0 +1,41 @@
# Automation to create events across channels (google calendar & discord)
#### by [Rancho-rachit](https://github.com/Rancho-rachit)
---
## Description:
This script creates events across -
1. [Google Calendar](https://developersindia.in/events-calendar/)
2. [Discord](https://discord.com/channels/669880381649977354/)
---
### FIRST TIME SETUP
1. Get Python3 `sudo apt-get install python3 && python3 --version`
2. Install required packages `pip install -r packages.txt`
3. Add respective tokens in the `.env` file
2.1 Discord Bot token (Get it from [Discord Developers portal](https://discord.com/developers/applications/)) (bot must have MANAGE_EVENT & CREATE_EVENT permission)
2.2 Guild ID (developersIndia => 1229786646468362260)
3. Connect Google Calender through [Google cloud Console](https://console.cloud.google.com/)
3.1 Create a Project on Google Cloud Console
3.2 Search for Calender API and enable it
3.3 Create Credentials -> OAuth Client ID -> Application type as Desktop
3.4 Download the Json file
3.5 Rename that JSON file as `credentials.json` and save it to the project directory.
4. `python3 main.py`
---
### NOTES-
> - Google authenication is required for the first time.
>
> - A file `token.json` will be downloaded automatically, and no web login will be needed afterwards.
<!-- END -->

View File

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

View File

@ -0,0 +1,17 @@
# 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

@ -0,0 +1,61 @@
import os.path
import event_details as main
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
SCOPES = ['https://www.googleapis.com/auth/calendar']
def create_event(name, description, location, date, start_time, end_time):
creds = None
# The file token.json stores the user's access and refresh tokens
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
print("Login to your Google account. [THIS IS A ONE TIME PROCESS]")
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
try :
service = build('calendar', 'v3', credentials=creds)
start = f"{date}T{start_time}:00"
end = f"{date}T{end_time}:00"
event = {
'summary': name,
'location': location,
'description': description,
'start': {
'dateTime': start,
'timeZone': 'Asia/Kolkata',
},
'end': {
'dateTime': end,
'timeZone': 'Asia/Kolkata',
},
}
event = service.events().insert(calendarId='primary', body=event).execute()
print('Google Calender: %s' % (event.get('htmlLink')))
return event
except Exception as e:
print(e)
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

View File

@ -0,0 +1,12 @@
# SCRIPT
# Execute event_details.py
import event_details
# Execute discord_.py
import discord_
# Execute google_calender.py
import google_calender_
# END

View File

@ -0,0 +1,4 @@
discord.py
google-api-python-client
google-auth-httplib2
google-auth-oauthlib

View File

@ -0,0 +1,12 @@
# This is the file contains the required tokens.
# DISCORD BOT TOKEN
# MUST HAVE PERMISSIONS TO CREATE EVENTS & MANAGE EVENTS
# https://discord.com/developers/applications
DISCORD_BOT_TOKEN = "TOKEN"
# DISCORD SERVER ID (GUILD ID)
# developersIndia => 1229786646468362260
DISCORD_GUILD_ID = 1229786646468362260