Created
December 17, 2021 16:39
-
-
Save Wolfhound905/e5810156b9ded86c1583a14dc7d34633 to your computer and use it in GitHub Desktop.
Sample of making and editing events
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
from typing import Union | |
from dis_snek.client import Snake | |
from dis_snek.const import MISSING | |
from dis_snek.errors import NotFound | |
from dis_snek.models import ( | |
Embed, | |
InteractionContext, | |
OptionTypes, | |
ScheduledEvent, | |
ScheduledEventEntityType, | |
SlashCommandChoice, | |
StageInstance, | |
Timestamp, | |
TimestampStyles, | |
VoiceChannel, | |
slash_command, | |
slash_option, | |
) | |
from dis_snek.models.listener import listen | |
bot = Snake(sync_interactions=True) | |
guild_id = ur_id_here | |
@listen("on_ready") | |
async def on_ready(): | |
print("Ready!") | |
@slash_command("create_event", "Create a scheduled event", scopes=[guild_id]) | |
@slash_option("name", "The name of the event", required=True, opt_type=OptionTypes.STRING) | |
@slash_option("start_time", "YEAR/MONTH/DAY The start time of the event", required=True, opt_type=OptionTypes.STRING) | |
@slash_option( | |
"event_type", | |
"The type of the event", | |
required=True, | |
opt_type=OptionTypes.INTEGER, | |
choices=[ | |
SlashCommandChoice("Stage", ScheduledEventEntityType.STAGE_INSTANCE), | |
SlashCommandChoice("Voice", ScheduledEventEntityType.VOICE), | |
SlashCommandChoice("External", ScheduledEventEntityType.EXTERNAL), | |
], | |
) | |
@slash_option( | |
"channel", "Either Voice or Stage. The channel to send the event to", required=False, opt_type=OptionTypes.CHANNEL | |
) | |
@slash_option("end_time", "YEAR/MONTH/DAY The end time of the event", required=False, opt_type=OptionTypes.STRING) | |
@slash_option("external_location", "ONLY IF TYPE EXTERNAL", required=False, opt_type=OptionTypes.STRING) | |
@slash_option("description", "The description of the event", required=False, opt_type=OptionTypes.STRING) | |
async def create_event( | |
ctx: InteractionContext, | |
name: str, | |
start_time: str, | |
event_type: ScheduledEventEntityType, | |
channel: Union["VoiceChannel", "StageInstance"] = MISSING, | |
description: str = MISSING, | |
end_time: str = MISSING, | |
external_location: str = MISSING, | |
): | |
"""Create a scheduled event""" | |
start_time = Timestamp.fromdatetime(datetime.strptime(start_time, "%Y/%m/%d")) | |
if end_time: | |
end_time = Timestamp.fromdatetime(datetime.strptime(end_time, "%Y/%m/%d")) | |
event: ScheduledEvent = await ctx.guild.create_scheduled_event( | |
name=name, | |
description=description, | |
scheduled_start_time=start_time, | |
scheduled_end_time=end_time, | |
entity_type=event_type, | |
entity_metadata=dict(location=external_location), | |
channel_id=channel.id, | |
) | |
embed = Embed(title=f"Created event {event.name}", description=f"{event.description}") | |
embed.add_field( | |
name="Info", | |
value=( | |
f"""Starts: {event.scheduled_start_time.format(TimestampStyles.RelativeTime)} | |
Ends: {event.scheduled_end_time.format(TimestampStyles.RelativeTime)}""" | |
), | |
) | |
await ctx.send(embed=embed) | |
@slash_command("edit_event", "Create a scheduled event", scopes=[guild_id]) | |
@slash_option("event_id", "The id of the event", required=True, opt_type=OptionTypes.STRING) | |
@slash_option("name", "The name of the event", required=False, opt_type=OptionTypes.STRING) | |
@slash_option("start_time", "YEAR/MONTH/DAY The start time of the event", required=False, opt_type=OptionTypes.STRING) | |
@slash_option( | |
"event_type", | |
"The type of the event", | |
required=False, | |
opt_type=OptionTypes.INTEGER, | |
choices=[ | |
SlashCommandChoice("Stage", ScheduledEventEntityType.STAGE_INSTANCE), | |
SlashCommandChoice("Voice", ScheduledEventEntityType.VOICE), | |
SlashCommandChoice("External", ScheduledEventEntityType.EXTERNAL), | |
], | |
) | |
@slash_option( | |
"channel", "Either Voice or Stage. The channel to send the event to", required=False, opt_type=OptionTypes.CHANNEL | |
) | |
@slash_option("end_time", "YEAR/MONTH/DAY The end time of the event", required=False, opt_type=OptionTypes.STRING) | |
@slash_option("external_location", "ONLY IF TYPE EXTERNAL", required=False, opt_type=OptionTypes.STRING) | |
@slash_option("description", "The description of the event", required=False, opt_type=OptionTypes.STRING) | |
async def edit_event( | |
ctx: InteractionContext, | |
event_id: int, | |
name: str = MISSING, | |
start_time: str = MISSING, | |
event_type: ScheduledEventEntityType = MISSING, | |
channel: Union["VoiceChannel", "StageInstance"] = MISSING, | |
description: str = MISSING, | |
end_time: str = MISSING, | |
external_location: str = MISSING, | |
): | |
"""Create a scheduled event""" | |
try: | |
event = await ctx.guild.get_scheduled_event(event_id) | |
except NotFound: | |
await ctx.send("Event not found") | |
return | |
try: | |
await event.edit( | |
name=name, | |
description=description, | |
scheduled_start_time=Timestamp.fromdatetime(datetime.strptime(start_time, "%Y/%m/%d")) | |
if start_time | |
else MISSING, | |
scheduled_end_time=Timestamp.fromdatetime(datetime.strptime(end_time, "%Y/%m/%d")) if end_time else MISSING, | |
entity_type=event_type, | |
entity_metadata=dict(location=external_location) if external_location else MISSING, | |
channel_id=channel.id, | |
) | |
await ctx.send("Event edited") | |
except Exception as e: | |
await ctx.send(f"Failed to edit event: {e}") | |
return | |
bot.start("token") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is for NAFTeam/NAFF#112