Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support guild scheduled events #319

Merged
merged 8 commits into from
Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
162 changes: 162 additions & 0 deletions api/scheduled_events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package api

import (
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/arikawa/v3/utils/httputil"
"github.com/diamondburned/arikawa/v3/utils/json/option"
)

// CreateScheduledEventData is the structure for creating a scheduled event.
//
// https://discord.com/developers/docs/resources/guild-scheduled-event#create-guild-scheduled-event-json-params
type CreateScheduledEventData struct {
// ChannelID is the channel id of the scheduled event.
ChannelID discord.ChannelID `json:"channel_id"`
// EntityMetadata is the entity metadata of the scheduled event.
EntityMetadata *discord.EntityMetadata `json:"entity_metadata"`
// Name is the name of the scheduled event.
Name string `json:"name"`
// PrivacyLevel is the privacy level of the scheduled event.
PrivacyLevel discord.ScheduledEventPrivacyLevel `json:"privacy_level"`
// StartTime is when the scheduled event begins.
StartTime discord.Timestamp `json:"scheduled_start_time"`
// EndTime is when the scheduled event ends, if it does.
EndTime *discord.Timestamp `json:"scheduled_end_time,omitempty"`
// Description is the description of the schduled event.
Description string `json:"description"`
// EntityType is the entity type of the scheduled event.
EntityType discord.EntityType `json:"entity_type"`
// Image is the cover image of the scheduled event.
Image Image `json:"image"`
}

// EditScheduledEventData is the structure for modifying a scheduled event.
//
// https://discord.com/developers/docs/resources/guild-scheduled-event#modify-guild-scheduled-event-json-params
type EditScheduledEventData struct {
// ChannelID is the new channel id of the scheduled event.
ChannelID discord.ChannelID `json:"channel_id,omitempty"`
// EntityMetadata is the new entity metadata of the scheduled event.
EntityMetadata *discord.EntityMetadata `json:"entity_metadata,omitempty"`
// Name is the new name of the scheduled event.
Name option.NullableString `json:"name,omitempty"`
// PrivacyLevel is the new privacy level of the scheduled event.
PrivacyLevel discord.ScheduledEventPrivacyLevel `json:"privacy_level,omitempty"`
// StartTime is the new starting time for when the scheduled event begins.
StartTime *discord.Timestamp `json:"scheduled_start_time,omitempty"`
// EndTime is the new time of which the scheduled event ends
EndTime *discord.Timestamp `json:"scheduled_end_time,omitempty"`
// Description is the new description of the scheduled event.
Description option.NullableString `json:"description,omitempty"`
// EntityType is the new entity type of the scheduled event.
EntityType discord.EntityType `json:"entity_type,omitempty"`
// Status is the new event status of the scheduled event.
Status discord.EventStatus `json:"status,omitempty"`
// Image is the new image of the scheduled event.
Image *Image `json:"image,omitempty"`
}

// GuildScheduledEventUser represents a user interested in a scheduled event.
//
// https://discord.com/developers/docs/resources/guild-scheduled-event#guild-scheduled-event-user-object
type GuildScheduledEventUser struct {
// EventID is the id of the scheduled event.
EventID discord.EventID `json:"guild_scheduled_event_id"`
// User is the user object of the user.
User discord.User `json:"user"`
// Member is the member object of the user.
Member *discord.Member `json:"member"`
}

// ListScheduledEventUsers returns a list of users currently in a scheduled event.
//
// https://discord.com/developers/docs/resources/guild-scheduled-event#get-guild-scheduled-event-users
func (c *Client) ListScheduledEventUsers(
guildID discord.GuildID, eventID discord.EventID, limit option.NullableInt,
withMember bool, before, after discord.UserID) ([]*GuildScheduledEventUser, error) {
itslychee marked this conversation as resolved.
Show resolved Hide resolved
var eventUsers []*GuildScheduledEventUser
var params struct {
Limit option.NullableInt `schema:"limit,omitempty"`
WithMember bool `schema:"with_member,omitempty"`
Before discord.UserID `schema:"before,omitempty"`
After discord.UserID `schema:"after,omitempty"`
}
params.Limit = limit
params.WithMember = withMember
params.Before = before
params.After = after

return eventUsers, c.RequestJSON(
&eventUsers, "GET", EndpointGuilds+guildID.String()+"/scheduled-events/"+eventID.String()+"/users",
httputil.WithSchema(c, params),
)

}

// ListScheduledEvents lists the scheduled events in a guild.
//
// https://discord.com/developers/docs/resources/guild-scheduled-event#get-guild-scheduled-event-users
func (c *Client) ListScheduledEvents(guildID discord.GuildID, withUserCount bool) ([]*discord.GuildScheduledEvent, error) {
itslychee marked this conversation as resolved.
Show resolved Hide resolved
var scheduledEvents []*discord.GuildScheduledEvent
var params struct {
WithUserCount bool `schema:"with_user_count"`
}
params.WithUserCount = withUserCount
return scheduledEvents, c.RequestJSON(
&scheduledEvents, "GET", EndpointGuilds+guildID.String()+"/scheduled-events",
httputil.WithSchema(c, params),
)
}

// CreateScheduledEvent creates a new scheduled event.
//
// https://discord.com/developers/docs/resources/guild-scheduled-event#create-guild-scheduled-event
func (c *Client) CreateScheduledEvent(guildID discord.GuildID, reason AuditLogReason,
data CreateScheduledEventData) (*discord.GuildScheduledEvent, error) {
var scheduledEvent *discord.GuildScheduledEvent
return scheduledEvent, c.RequestJSON(
&scheduledEvent, "POST",
EndpointGuilds+guildID.String()+"/scheduled-events",
httputil.WithJSONBody(data),
httputil.WithHeaders(reason.Header()),
)
}

// EditScheduledEvent modifies the attributes of a scheduled event.
//
// https://discord.com/developers/docs/resources/guild-scheduled-event#modify-guild-scheduled-event
func (c *Client) EditScheduledEvent(guildID discord.GuildID, eventID discord.EventID, reason AuditLogReason,
data EditScheduledEventData) (*discord.GuildScheduledEvent, error) {
var modifiedEvent *discord.GuildScheduledEvent
return modifiedEvent, c.RequestJSON(
&modifiedEvent,
"PATCH", EndpointGuilds+guildID.String()+"/scheduled-events/"+eventID.String(),
httputil.WithHeaders(reason.Header()),
httputil.WithJSONBody(data),
)
}

// DeleteScheduledEvent deletes a scheduled event.
//
// https://discord.com/developers/docs/resources/guild-scheduled-event#delete-guild-scheduled-event
func (c *Client) DeleteScheduledEvent(guildID discord.GuildID, eventID discord.EventID) error {
return c.FastRequest(
"DELETE", EndpointGuilds+guildID.String()+"/scheduled-events/"+eventID.String(),
)
}

// ScheduledEvent retrieves the information on the scheduled event
//
// https://discord.com/developers/docs/resources/guild-scheduled-event#get-guild-scheduled-event
func (c *Client) ScheduledEvent(guildID discord.GuildID, eventID discord.EventID, withUserCount bool) (*discord.GuildScheduledEvent, error) {
var params struct {
WithUserCount bool `schema:"with_user_count"`
}
params.WithUserCount = withUserCount
var event *discord.GuildScheduledEvent
return event, c.RequestJSON(
&event, "GET", EndpointGuilds+guildID.String()+"/scheduled-events/"+eventID.String(),
httputil.WithSchema(c, params),
)

}
6 changes: 4 additions & 2 deletions discord/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ const (
// Allows for requesting to speak in stage channels. (This permission is
// under active development and may be changed or removed.)
PermissionRequestToSpeak
_
// Allows for creating, editing, and deleting scheduled events.
PermissionManageEvents
// Allows for deleting and archiving threads, and viewing all private
// threads
PermissionManageThreads
Expand Down Expand Up @@ -139,7 +140,8 @@ const (
PermissionManageEmojisAndStickers |
PermissionManageNicknames |
PermissionChangeNickname |
PermissionViewAuditLog
PermissionViewAuditLog |
PermissionManageEvents
)

func (p Permissions) Has(perm Permissions) bool {
Expand Down
85 changes: 85 additions & 0 deletions discord/scheduled_events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package discord

import (
"time"

"github.com/diamondburned/arikawa/v3/utils/json/option"
)

// EventStatus describes the different statuses GuildScheduledEvent can be.
//
// https://discord.com/developers/docs/resources/guild-scheduled-event#guild-scheduled-event-object-guild-scheduled-event-status
type EventStatus int

const (
Scheduled EventStatus = iota + 1
itslychee marked this conversation as resolved.
Show resolved Hide resolved
Active
Completed
Cancelled
)

// EntityType describes the different types GuildScheduledEvent can be.
type EntityType int

const (
StageInstanceEntity EntityType = iota + 1
VoiceEntity
ExternalEntity
)

// ScheduledEventPrivacy describes the privacy levels of GuildScheduledEvent.
//
// https://discord.com/developers/docs/resources/guild-scheduled-event#guild-scheduled-event-object-guild-scheduled-event-privacy-level
type ScheduledEventPrivacyLevel int

const (
// GuildOnly requires the scheduled event to be only accessible to guild members.
GuildOnly ScheduledEventPrivacyLevel = iota + 2
)

// GuildScheduledEvent describes the scheduled event structure.
//
// https://discord.com/developers/docs/resources/guild-scheduled-event#guild-scheduled-event-object-guild-scheduled-event-structure
type GuildScheduledEvent struct {
// ID is the id of the scheduled event.
ID EventID `json:"id"`
// GuildID is the guild id of where the scheduled event belongs to.
GuildID GuildID `json:"guild_id"`
// ChannelID is the channel id in which the scheduled event will be
// hosted at, this may be NullChannelID if the EntityType is set
// to ExternalEntity.
ChannelID ChannelID `json:"channel_id"`
// CreatorID is the user id of who created the scheduled event.
CreatorID UserID `json:"creator_id"`
// Name is the name of the scheduled event.
Name string `json:"name"`
// Description is the description of the scheduled event.
Description option.NullableString `json:"description"`
itslychee marked this conversation as resolved.
Show resolved Hide resolved
// StartTime is when the scheduled event will start at.
StartTime time.Time `json:"scheduled_start_time"`
itslychee marked this conversation as resolved.
Show resolved Hide resolved
// EndTime is when the scheduled event will end at, if it does.
EndTime *time.Time `json:"scheduled_end_time"`
itslychee marked this conversation as resolved.
Show resolved Hide resolved
// PrivacyLevel is the privacy level of the scheduled event.
PrivacyLevel ScheduledEventPrivacyLevel `json:"privacy_level"`
// Status is the status of the scheduled event.
Status EventStatus `json:"status"`
// EntityType describes the type of scheduled event.
EntityType EntityType `json:"entity_type"`
// EntityID is the id of an entity associated with a scheduled event.
EntityID EntityID `json:"entity_id"`
// EntityMetadata is additional metadata for the scheduled event.
EntityMetadata *EntityMetadata `json:"entity_metadata"`
// Creator is the the user responsible for creating the scheduled event.
itslychee marked this conversation as resolved.
Show resolved Hide resolved
Creator *User `json:"creator"`
// UserCount is the number of users subscribed to the scheduled event.
UserCount int `json:"user_count"`
// Image is the cover image hash of the scheduled event.
Image *Hash `json:"image"`
itslychee marked this conversation as resolved.
Show resolved Hide resolved
}

// EntityMetadata is the entity metadata of GuildScheduledEvent.
type EntityMetadata struct {
// Location describes where the event takes place at. This is not
// optional when GuildScheduled#EntityType is set as ExternalEntity.
Location string `json:"location,omitempty"`
}
2 changes: 1 addition & 1 deletion discord/snowflake.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func DurationSinceEpoch(t time.Time) time.Duration {
return time.Duration(t.UnixNano()) - Epoch
}

//go:generate go run ../utils/cmd/gensnowflake -o snowflake_types.go AppID AttachmentID AuditLogEntryID ChannelID CommandID EmojiID GuildID IntegrationID InteractionID MessageID RoleID StageID StickerID StickerPackID TeamID UserID WebhookID
//go:generate go run ../utils/cmd/gensnowflake -o snowflake_types.go AppID AttachmentID AuditLogEntryID ChannelID CommandID EmojiID GuildID IntegrationID InteractionID MessageID RoleID StageID StickerID StickerPackID TeamID UserID WebhookID EventID EntityID

// Mention generates the mention syntax for this channel ID.
func (s ChannelID) Mention() string { return "<#" + s.String() + ">" }
Expand Down
48 changes: 48 additions & 0 deletions discord/snowflake_types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions gateway/event_methods.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.