As of writing this, documentation for the Discord events API is a little lacking and the feature is not yet integrated into Discord.py.
This gist presents a basic class that performs a couple event actions against the Discord API.
To interact with the Discord API you need an async http client, for this gist it'll be aiohttp
. You'll need a Discord bot created, and to have a token generated for that bot. Your bot will also need event permissions in the guilds/servers you are trying to create events in.
For information on the return content of the list_guild_events() function, see this section of Discord's API docs.
Remember, these functions are asynchronous! They'll need to be awaited, not just called like normal functions. If you're new to that sort of thing, see this guide for details.
import json
import aiohttp
class DiscordEvents:
'''Class to create and list Discord events utilizing their API'''
def __init__(self, discord_token: str) -> None:
self.base_api_url = 'https://discord.com/api/v8'
self.auth_headers = {
'Authorization':f'Bot {discord_token}',
'User-Agent':'DiscordBot (https://your.bot/url) Python/3.9 aiohttp/3.8.1',
'Content-Type':'application/json'
}
async def list_guild_events(self, guild_id: str) -> list:
'''Returns a list of upcoming events for the supplied guild ID
Format of return is a list of one dictionary per event containing information.'''
event_retrieve_url = f'{self.base_api_url}/guilds/{guild_id}/scheduled-events'
async with aiohttp.ClientSession(headers=self.auth_headers) as session:
try:
async with session.get(event_retrieve_url) as response:
response.raise_for_status()
assert response.status == 200
response_list = json.loads(await response.read())
except Exception as e:
print(f'EXCEPTION: {e}')
finally:
await session.close()
return response_list
async def create_guild_event(
self,
guild_id: str,
event_name: str,
event_description: str,
event_start_time: str,
event_end_time: str,
event_metadata: dict,
event_privacy_level=2,
channel_id=None
) -> None:
'''Creates a guild event using the supplied arguments
The expected event_metadata format is event_metadata={'location': 'YOUR_LOCATION_NAME'}
The required time format is %Y-%m-%dT%H:%M:%S'''
event_create_url = f'{self.base_api_url}/guilds/{guild_id}/scheduled-events'
event_data = json.dumps({
'name': event_name,
'privacy_level': event_privacy_level,
'scheduled_start_time': event_start_time,
'scheduled_end_time': event_end_time,
'description': event_description,
'channel_id': channel_id,
'entity_metadata': event_metadata,
'entity_type': 3
})
async with aiohttp.ClientSession(headers=self.auth_headers) as session:
try:
async with session.post(event_create_url, data=event_data) as response:
response.raise_for_status()
assert response.status == 200
except Exception as e:
print(f'EXCEPTION: {e}')
finally:
await session.close()
Hi, Thanks for this!
If I wanted the location to be a voice channel, is there a particular way I need to format the event meta data?