deviras/event-manager/discord_bot.py

58 lines
1.6 KiB
Python
Raw Normal View History

from datetime import datetime, timedelta
import discord
from discord.ext import commands
2024-05-05 15:00:53 +05:30
def create_discord_event(
bot_token,
guild_id,
EVENT_NAME,
EVENT_DESCRIPTION,
EVENT_LOCATION,
EVENT_DATE,
EVENT_START_TIME,
EVENT_END_TIME,
):
bot = commands.Bot(command_prefix="!", intents=discord.Intents.default())
@bot.event
async def on_ready():
# Convert the date string to a datetime object
date_object = datetime.strptime(EVENT_DATE, "%Y-%m-%d")
# Convert the time string to a datetime object
start_time_object = datetime.strptime(EVENT_START_TIME, "%H:%M")
end_time_object = datetime.strptime(EVENT_END_TIME, "%H:%M")
# Combine the date and time objects
2024-05-05 15:00:53 +05:30
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(
2024-05-05 15:00:53 +05:30
name=EVENT_NAME,
entity_type=discord.EntityType.external,
description=EVENT_DESCRIPTION,
start_time=st,
2024-05-05 15:00:53 +05:30
end_time=et,
location=EVENT_LOCATION,
privacy_level=discord.PrivacyLevel.guild_only,
2024-05-05 15:00:53 +05:30
)
print("Discord Event created: ", event.url)
2024-05-05 15:00:53 +05:30
except Exception as e:
print(e)
2024-05-05 15:00:53 +05:30
await bot.close()
2024-05-05 15:00:53 +05:30
# Run the bot
bot.run(bot_token)