Skip to content

Commit

Permalink
fix #67
Browse files Browse the repository at this point in the history
update dasgo
fix message components usage
add message components half example
  • Loading branch information
switchupcb committed Jul 30, 2023
1 parent 064e1a0 commit d88baa4
Show file tree
Hide file tree
Showing 14 changed files with 1,244 additions and 745 deletions.
48 changes: 48 additions & 0 deletions _examples/message/components/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Example: Send a Message Component

This example sends message components to a channel.

_This example is UNFINISHED: User input to message components are handled using [interactions](/_examples/command/)._

## Setup

**You must create a Discord Application in the [Discord Developer Portal](https://discord.com/developers/docs/getting-started#creating-an-app) to receive your Bot Token.**

### Environment Variables

Assign an environment variable in the command line you will be running the program from.

#### Windows

```
set TOKEN=value
```

#### Mac/Linux

```
export TOKEN=value
```

**NEVER SHOW YOUR TOKEN TO THE PUBLIC.**

### Flags

This example uses [command line flags](https://pkg.go.dev/flag) to input the channel and message content.

```
> components -h
Usage of send:
-c string
Set the channel (ID) the message components will be sent to using -c.
```

## Usage

Use `go build` to build the executable binary. Use `components` to run it from the command line.

_NOTE: Get the Channel ID by enabling **Developer Mode** from the settings of your account, then right clicking any channel._

```
components -c 1041179872518737990
```
File renamed without changes.
137 changes: 137 additions & 0 deletions _examples/message/components/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package main

import (
"flag"
"log"
"os"

"github.com/switchupcb/disgo"
)

// Environment Variables.
var (
// token represents the bot's token.
token = os.Getenv("TOKEN")
)

// Command Line Flags.
var (
channelID = flag.String("c", "", "Set the channel (ID) the message components will be sent to using -c.")
)

func main() {
// enable the logger for the API Wrapper.
// zerolog.SetGlobalLevel(zerolog.DebugLevel)

// parse the command line flags.
flag.Parse()

// ensure that the program has the necessary data to succeed.
if token == "" {
log.Println("The bot's token must be set, but is currently empty.")

return
}

if *channelID == "" {
log.Println("The channel to send the message to is not set.")
flag.Usage()

return
}

// create a new Bot Client with the information required to send a request.
bot := &disgo.Client{
Authentication: disgo.BotToken(token), // or BearerToken("TOKEN")
Config: disgo.DefaultConfig(),
}

// ensure that the bot has access to the channel.
//
// This is useful for the validation of this program, but unnecessary.
getChannelRequest := disgo.GetChannel{ChannelID: *channelID}
_, err := getChannelRequest.Send(bot)
if err != nil {
log.Printf("error occurred getting channel %q: %v", *channelID, err)

return
}

// send an Action Row (containing a Button) to the channel.
createMessageRequestActionRowButton := &disgo.CreateMessage{

Check failure on line 61 in _examples/message/components/main.go

View workflow job for this annotation

GitHub Actions / Static Code Analysis

Nonce, TTS, AllowedMentions, MessageReference, Flags, Embeds, StickerIDS, Files, Attachments are missing in CreateMessage (exhaustruct)
ChannelID: *channelID,
Content: disgo.Pointer("This is an Action Row (containing a Button)."),
Components: []disgo.Component{
&disgo.ActionRow{
Type: disgo.FlagComponentTypeActionRow,
Components: []disgo.Component{
&disgo.Button{
Type: disgo.FlagComponentTypeButton,
Style: disgo.FlagButtonStyleRED,
Label: disgo.Pointer("Button Label."),
Emoji: nil,
CustomID: disgo.Pointer("example-button"),
URL: nil,
Disabled: nil,
},
},
},
},
}

message, err := createMessageRequestActionRowButton.Send(bot)
if err != nil {
log.Printf("error occurred sending a message to channel %q: %v", *channelID, err)

return
}

log.Printf("Successfully sent message with ID %q", message.ID)

// send an Action Row (containing a Select Menu) to the channel.
createMessageRequestActionRowSelectMenu := &disgo.CreateMessage{

Check failure on line 92 in _examples/message/components/main.go

View workflow job for this annotation

GitHub Actions / Static Code Analysis

Nonce, TTS, AllowedMentions, MessageReference, Flags, Embeds, StickerIDS, Files, Attachments are missing in CreateMessage (exhaustruct)
ChannelID: *channelID,
Content: disgo.Pointer("This is an Action Row (containing a String Select Menu)."),
Components: []disgo.Component{
&disgo.ActionRow{
Type: disgo.FlagComponentTypeActionRow,
Components: []disgo.Component{
&disgo.SelectMenu{
Type: disgo.FlagComponentTypeStringSelect,
CustomID: "example-select-menu",
Placeholder: disgo.Pointer("Select an option."),
Options: []disgo.SelectMenuOption{
{
Label: "Yes",
Value: "yes",
Description: disgo.Pointer("Yessir."),
Emoji: nil,
Default: nil,
},
{
Label: "No",
Value: "no",
Description: disgo.Pointer("Nope!"),
Emoji: nil,
Default: nil,
},
},
MinValues: nil,
MaxValues: nil,
Disabled: nil,
ChannelTypes: nil,
},
},
},
},
}

message, err = createMessageRequestActionRowSelectMenu.Send(bot)
if err != nil {
log.Printf("error occurred sending a message to channel %q: %v", *channelID, err)

return
}

log.Printf("Successfully sent message with ID %q", message.ID)
}
Empty file.
Empty file.
9 changes: 5 additions & 4 deletions _examples/message/send/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func main() {

// ensure that the bot has access to the channel.
//
// This is useful for the validation of this program, but not necessary.
// This is useful for the validation of this program, but unnecessary.
getChannelRequest := disgo.GetChannel{ChannelID: *channelID}
_, err := getChannelRequest.Send(bot)
if err != nil {
Expand All @@ -80,12 +80,13 @@ func main() {
return
}

// send a message in the channel.
// send a message to the channel.
//
// Explicitly defining every field of a Create Message struct is unnecessary.
//
// Explicitly defining every field of a Create Message struct is not necessary.
// In any case, a description of each field is found in the Discord API Documentation.
// https://discord.com/developers/docs/resources/channel#create-message-jsonform-params
createMessageRequest := disgo.CreateMessage{
createMessageRequest := &disgo.CreateMessage{
ChannelID: *channelID,
Content: msg,
Nonce: nil,
Expand Down
3 changes: 3 additions & 0 deletions _gen/coverage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var (
"GetFollowupMessage": {"CreateFollowupMessage"},
"EditFollowupMessage": {"CreateFollowupMessage"},
"DeleteFollowupMessage": {"CreateFollowupMessage"},
"GetCurrentApplication": {},
"GetApplicationRoleConnectionMetadataRecords": {},
"UpdateApplicationRoleConnectionMetadataRecords": {},
"GetGuildAuditLog": {"CreateGuild"},
Expand Down Expand Up @@ -126,6 +127,7 @@ var (
"GetGuildWelcomeScreen": {"CreateGuild"},
"ModifyGuildWelcomeScreen": {"CreateGuild"},
"GetGuildOnboarding": {"CreateGuild"},
"ModifyGuildOnboarding": {"CreateGuild"},
"ModifyCurrentUserVoiceState": {"CreateGuild"},
"ModifyUserVoiceState": {"CreateGuild", "AddGuildMember"},
"ListScheduledEventsforGuild": {"CreateGuild"},
Expand Down Expand Up @@ -264,6 +266,7 @@ var (
"GetGuildWelcomeScreen": true,
"ModifyGuildWelcomeScreen": true,
"GetGuildOnboarding": true,
"ModifyGuildOnboarding": true,
"GetGuildTemplate": true,
"CreateGuildfromGuildTemplate": true,
"CreateGuildTemplate": true,
Expand Down
4 changes: 4 additions & 0 deletions _gen/tools/_copygen/requests/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ type Copygen interface {
// http DELETE
DeleteFollowupMessage(*disgo.DeleteFollowupMessage) error
// http GET
GetCurrentApplication(*disgo.GetCurrentApplication) (*disgo.Application, error)
// http GET
GetApplicationRoleConnectionMetadataRecords(*disgo.GetApplicationRoleConnectionMetadataRecords) ([]*disgo.ApplicationRoleConnectionMetadata, error)
// http PUT
UpdateApplicationRoleConnectionMetadataRecords(*disgo.UpdateApplicationRoleConnectionMetadataRecords) ([]*disgo.ApplicationRoleConnectionMetadata, error)
Expand Down Expand Up @@ -248,6 +250,8 @@ type Copygen interface {
ModifyGuildWelcomeScreen(*disgo.ModifyGuildWelcomeScreen) (*disgo.WelcomeScreen, error)
// http GET
GetGuildOnboarding(*disgo.GetGuildOnboarding) (*disgo.GuildOnboarding, error)
// http PUT
ModifyGuildOnboarding(*disgo.ModifyGuildOnboarding) (*disgo.GuildOnboarding, error)
// http PATCH
ModifyCurrentUserVoiceState(*disgo.ModifyCurrentUserVoiceState) error
// http PATCH
Expand Down

0 comments on commit d88baa4

Please sign in to comment.