Skip to content

Commit

Permalink
Merge pull request #184 from disgoorg/patch/remove-customid
Browse files Browse the repository at this point in the history
remove CustomID
  • Loading branch information
topi314 committed Aug 8, 2022
2 parents da55024 + ad4a5d3 commit fab4160
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 53 deletions.
8 changes: 4 additions & 4 deletions _examples/test/listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ func modalListener(event *events.ModalSubmitInteractionCreate) {
func componentListener(event *events.ComponentInteractionCreate) {
switch data := event.Data.(type) {
case discord.ButtonInteractionData:
ids := strings.Split(data.CustomID().String(), ":")
ids := strings.Split(data.CustomID(), ":")
switch ids[0] {
case "modal":
_ = event.CreateModal(discord.ModalCreate{
CustomID: discord.CustomID("test" + ids[1]),
CustomID: "test" + ids[1],
Title: "Test" + ids[1] + " Modal",
Components: []discord.ContainerComponent{
discord.ActionRowComponent{
Expand All @@ -66,7 +66,7 @@ func componentListener(event *events.ComponentInteractionCreate) {

case "test1":
_ = event.CreateMessage(discord.NewMessageCreateBuilder().
SetContent(data.CustomID().String()).
SetContent(data.CustomID()).
Build(),
)

Expand All @@ -78,7 +78,7 @@ func componentListener(event *events.ComponentInteractionCreate) {

case "test4":
_ = event.UpdateMessage(discord.NewMessageUpdateBuilder().
SetContent(data.CustomID().String()).
SetContent(data.CustomID()).
Build(),
)
}
Expand Down
48 changes: 21 additions & 27 deletions discord/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ const (
ComponentTypeTextInput
)

type CustomID string

func (c CustomID) String() string {
return string(c)
}

type Component interface {
json.Marshaler
Type() ComponentType
Expand All @@ -38,7 +32,7 @@ type ContainerComponent interface {

type InteractiveComponent interface {
Component
ID() CustomID
ID() string
interactiveComponent()
}

Expand Down Expand Up @@ -183,7 +177,7 @@ func (c ActionRowComponent) TextInputs() []TextInputComponent {
}

// UpdateComponent returns a new ActionRowComponent with the Component which has the customID replaced
func (c ActionRowComponent) UpdateComponent(customID CustomID, component InteractiveComponent) ActionRowComponent {
func (c ActionRowComponent) UpdateComponent(customID string, component InteractiveComponent) ActionRowComponent {
for i, cc := range c {
if cc.ID() == customID {
c[i] = component
Expand Down Expand Up @@ -219,7 +213,7 @@ const (
)

// NewButton creates a new ButtonComponent with the provided parameters. Link ButtonComponent(s) need a URL and other ButtonComponent(s) need a customID
func NewButton(style ButtonStyle, label string, customID CustomID, url string) ButtonComponent {
func NewButton(style ButtonStyle, label string, customID string, url string) ButtonComponent {
return ButtonComponent{
Style: style,
CustomID: customID,
Expand All @@ -229,22 +223,22 @@ func NewButton(style ButtonStyle, label string, customID CustomID, url string) B
}

// NewPrimaryButton creates a new ButtonComponent with ButtonStylePrimary & the provided parameters
func NewPrimaryButton(label string, customID CustomID) ButtonComponent {
func NewPrimaryButton(label string, customID string) ButtonComponent {
return NewButton(ButtonStylePrimary, label, customID, "")
}

// NewSecondaryButton creates a new ButtonComponent with ButtonStyleSecondary & the provided parameters
func NewSecondaryButton(label string, customID CustomID) ButtonComponent {
func NewSecondaryButton(label string, customID string) ButtonComponent {
return NewButton(ButtonStyleSecondary, label, customID, "")
}

// NewSuccessButton creates a new ButtonComponent with ButtonStyleSuccess & the provided parameters
func NewSuccessButton(label string, customID CustomID) ButtonComponent {
func NewSuccessButton(label string, customID string) ButtonComponent {
return NewButton(ButtonStyleSuccess, label, customID, "")
}

// NewDangerButton creates a new ButtonComponent with ButtonStyleDanger & the provided parameters
func NewDangerButton(label string, customID CustomID) ButtonComponent {
func NewDangerButton(label string, customID string) ButtonComponent {
return NewButton(ButtonStyleDanger, label, customID, "")
}

Expand All @@ -262,7 +256,7 @@ type ButtonComponent struct {
Style ButtonStyle `json:"style"`
Label string `json:"label,omitempty"`
Emoji *ComponentEmoji `json:"emoji,omitempty"`
CustomID CustomID `json:"custom_id,omitempty"`
CustomID string `json:"custom_id,omitempty"`
URL string `json:"url,omitempty"`
Disabled bool `json:"disabled,omitempty"`
}
Expand All @@ -282,11 +276,11 @@ func (ButtonComponent) Type() ComponentType {
return ComponentTypeButton
}

func (c ButtonComponent) ID() CustomID {
func (c ButtonComponent) ID() string {
return c.CustomID
}

func (c ButtonComponent) SetID(id CustomID) InteractiveComponent {
func (c ButtonComponent) SetID(id string) InteractiveComponent {
c.CustomID = id
return c
}
Expand All @@ -313,7 +307,7 @@ func (c ButtonComponent) WithEmoji(emoji ComponentEmoji) ButtonComponent {
}

// WithCustomID returns a new ButtonComponent with the provided custom id
func (c ButtonComponent) WithCustomID(customID CustomID) ButtonComponent {
func (c ButtonComponent) WithCustomID(customID string) ButtonComponent {
c.CustomID = customID
return c
}
Expand Down Expand Up @@ -343,7 +337,7 @@ func (c ButtonComponent) WithDisabled(disabled bool) ButtonComponent {
}

// NewSelectMenu builds a new SelectMenuComponent from the provided values
func NewSelectMenu(customID CustomID, placeholder string, options ...SelectMenuOption) SelectMenuComponent {
func NewSelectMenu(customID string, placeholder string, options ...SelectMenuOption) SelectMenuComponent {
return SelectMenuComponent{
CustomID: customID,
Placeholder: placeholder,
Expand All @@ -357,7 +351,7 @@ var (
)

type SelectMenuComponent struct {
CustomID CustomID `json:"custom_id"`
CustomID string `json:"custom_id"`
Placeholder string `json:"placeholder,omitempty"`
MinValues *int `json:"min_values,omitempty"`
MaxValues int `json:"max_values,omitempty"`
Expand All @@ -380,15 +374,15 @@ func (SelectMenuComponent) Type() ComponentType {
return ComponentTypeSelectMenu
}

func (c SelectMenuComponent) ID() CustomID {
func (c SelectMenuComponent) ID() string {
return c.CustomID
}

func (SelectMenuComponent) component() {}
func (SelectMenuComponent) interactiveComponent() {}

// WithCustomID returns a new SelectMenuComponent with the provided customID
func (c SelectMenuComponent) WithCustomID(customID CustomID) SelectMenuComponent {
func (c SelectMenuComponent) WithCustomID(customID string) SelectMenuComponent {
c.CustomID = customID
return c
}
Expand Down Expand Up @@ -512,24 +506,24 @@ var (
_ InteractiveComponent = (*TextInputComponent)(nil)
)

func NewTextInput(customID CustomID, style TextInputStyle, label string) TextInputComponent {
func NewTextInput(customID string, style TextInputStyle, label string) TextInputComponent {
return TextInputComponent{
CustomID: customID,
Style: style,
Label: label,
}
}

func NewShortTextInput(customID CustomID, label string) TextInputComponent {
func NewShortTextInput(customID string, label string) TextInputComponent {
return NewTextInput(customID, TextInputStyleShort, label)
}

func NewParagraphTextInput(customID CustomID, label string) TextInputComponent {
func NewParagraphTextInput(customID string, label string) TextInputComponent {
return NewTextInput(customID, TextInputStyleParagraph, label)
}

type TextInputComponent struct {
CustomID CustomID `json:"custom_id"`
CustomID string `json:"custom_id"`
Style TextInputStyle `json:"style"`
Label string `json:"label"`
MinLength *int `json:"min_length,omitempty"`
Expand All @@ -554,15 +548,15 @@ func (TextInputComponent) Type() ComponentType {
return ComponentTypeTextInput
}

func (c TextInputComponent) ID() CustomID {
func (c TextInputComponent) ID() string {
return c.CustomID
}

func (TextInputComponent) component() {}
func (TextInputComponent) interactiveComponent() {}

// WithCustomID returns a new SelectMenuComponent with the provided customID
func (c TextInputComponent) WithCustomID(customID CustomID) TextInputComponent {
func (c TextInputComponent) WithCustomID(customID string) TextInputComponent {
c.CustomID = customID
return c
}
Expand Down
14 changes: 7 additions & 7 deletions discord/interaction_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,17 @@ func (ComponentInteraction) interaction() {}

type ComponentInteractionData interface {
Type() ComponentType
CustomID() CustomID
CustomID() string

componentInteractionData()
}

type rawButtonInteractionData struct {
Custom CustomID `json:"custom_id"`
Custom string `json:"custom_id"`
}

type ButtonInteractionData struct {
customID CustomID
customID string
}

func (d *ButtonInteractionData) UnmarshalJSON(data []byte) error {
Expand All @@ -115,7 +115,7 @@ func (ButtonInteractionData) Type() ComponentType {
return ComponentTypeButton
}

func (d ButtonInteractionData) CustomID() CustomID {
func (d ButtonInteractionData) CustomID() string {
return d.customID
}

Expand All @@ -126,12 +126,12 @@ var (
)

type rawSelectMenuInteractionData struct {
Custom CustomID `json:"custom_id"`
Custom string `json:"custom_id"`
Values []string `json:"values"`
}

type SelectMenuInteractionData struct {
customID CustomID
customID string
Values []string
}

Expand All @@ -156,7 +156,7 @@ func (SelectMenuInteractionData) Type() ComponentType {
return ComponentTypeSelectMenu
}

func (d SelectMenuInteractionData) CustomID() CustomID {
func (d SelectMenuInteractionData) CustomID() string {
return d.customID
}

Expand Down
14 changes: 7 additions & 7 deletions discord/interaction_modal_submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func (ModalSubmitInteraction) Type() InteractionType {
func (ModalSubmitInteraction) interaction() {}

type ModalSubmitInteractionData struct {
CustomID CustomID `json:"custom_id"`
Components map[CustomID]InteractiveComponent `json:"components"`
CustomID string `json:"custom_id"`
Components map[string]InteractiveComponent `json:"components"`
}

func (d *ModalSubmitInteractionData) UnmarshalJSON(data []byte) error {
Expand All @@ -54,7 +54,7 @@ func (d *ModalSubmitInteractionData) UnmarshalJSON(data []byte) error {
*d = ModalSubmitInteractionData(iData.modalSubmitInteractionData)

if len(iData.Components) > 0 {
d.Components = make(map[CustomID]InteractiveComponent, len(iData.Components))
d.Components = make(map[string]InteractiveComponent, len(iData.Components))
for _, containerComponent := range iData.Components {
for _, component := range containerComponent.Component.(ContainerComponent).Components() {
d.Components[component.ID()] = component
Expand All @@ -64,27 +64,27 @@ func (d *ModalSubmitInteractionData) UnmarshalJSON(data []byte) error {
return nil
}

func (d ModalSubmitInteractionData) Component(customID CustomID) (InteractiveComponent, bool) {
func (d ModalSubmitInteractionData) Component(customID string) (InteractiveComponent, bool) {
component, ok := d.Components[customID]
return component, ok
}

func (d ModalSubmitInteractionData) TextInputComponent(customID CustomID) (TextInputComponent, bool) {
func (d ModalSubmitInteractionData) TextInputComponent(customID string) (TextInputComponent, bool) {
if component, ok := d.Component(customID); ok {
textInputComponent, ok := component.(TextInputComponent)
return textInputComponent, ok
}
return TextInputComponent{}, false
}

func (d ModalSubmitInteractionData) OptText(customID CustomID) (string, bool) {
func (d ModalSubmitInteractionData) OptText(customID string) (string, bool) {
if textInputComponent, ok := d.TextInputComponent(customID); ok {
return textInputComponent.Value, true
}
return "", false
}

func (d ModalSubmitInteractionData) Text(customID CustomID) string {
func (d ModalSubmitInteractionData) Text(customID string) string {
if text, ok := d.OptText(customID); ok {
return text
}
Expand Down
12 changes: 6 additions & 6 deletions discord/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (m *Message) InteractiveComponents() []InteractiveComponent {
}

// ComponentByID returns the Component with the specific CustomID
func (m *Message) ComponentByID(customID CustomID) InteractiveComponent {
func (m *Message) ComponentByID(customID string) InteractiveComponent {
for i := range m.Components {
for ii := range m.Components[i].Components() {
if m.Components[i].Components()[ii].ID() == customID {
Expand All @@ -166,7 +166,7 @@ func (m *Message) Buttons() []ButtonComponent {
}

// ButtonByID returns a ButtonComponent with the specific customID from this Message
func (m *Message) ButtonByID(customID CustomID) *ButtonComponent {
func (m *Message) ButtonByID(customID string) *ButtonComponent {
for i := range m.Components {
for ii := range m.Components[i].Components() {
if button, ok := m.Components[i].Components()[ii].(*ButtonComponent); ok && button.ID() == customID {
Expand All @@ -191,7 +191,7 @@ func (m *Message) SelectMenus() []SelectMenuComponent {
}

// SelectMenuByID returns a SelectMenuComponent with the specific customID from this Message
func (m *Message) SelectMenuByID(customID CustomID) *SelectMenuComponent {
func (m *Message) SelectMenuByID(customID string) *SelectMenuComponent {
for i := range m.Components {
for ii := range m.Components[i].Components() {
if button, ok := m.Components[i].Components()[ii].(*SelectMenuComponent); ok && button.ID() == customID {
Expand Down Expand Up @@ -223,7 +223,7 @@ type MessageReaction struct {
// MessageActivityType is the type of MessageActivity https://com/developers/docs/resources/channel#message-object-message-activity-types
type MessageActivityType int

//Constants for MessageActivityType
// Constants for MessageActivityType
const (
MessageActivityTypeJoin MessageActivityType = iota + 1
MessageActivityTypeSpectate
Expand All @@ -232,13 +232,13 @@ const (
MessageActivityTypeJoinRequest
)

//MessageActivity is used for rich presence-related chat embeds in a Message
// MessageActivity is used for rich presence-related chat embeds in a Message
type MessageActivity struct {
Type MessageActivityType `json:"type"`
PartyID *string `json:"party_id,omitempty"`
}

//MessageApplication is used for rich presence-related chat embeds in a Message
// MessageApplication is used for rich presence-related chat embeds in a Message
type MessageApplication struct {
ID snowflake.ID `json:"id"`
CoverImage *string `json:"cover_image,omitempty"`
Expand Down
4 changes: 2 additions & 2 deletions discord/modal_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package discord
var _ InteractionResponseData = (*ModalCreate)(nil)

type ModalCreate struct {
CustomID CustomID `json:"custom_id"`
CustomID string `json:"custom_id"`
Title string `json:"title"`
Components []ContainerComponent `json:"components"`
}
Expand All @@ -20,7 +20,7 @@ type ModalCreateBuilder struct {
}

// SetCustomID sets the CustomID of the ModalCreate
func (b *ModalCreateBuilder) SetCustomID(customID CustomID) *ModalCreateBuilder {
func (b *ModalCreateBuilder) SetCustomID(customID string) *ModalCreateBuilder {
b.CustomID = customID
return b
}
Expand Down

0 comments on commit fab4160

Please sign in to comment.