1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-12-01 03:03:48 +00:00
arikawa/discord/component_example_test.go

84 lines
1.7 KiB
Go
Raw Normal View History

package discord_test
import (
"encoding/json"
"fmt"
"log"
"github.com/diamondburned/arikawa/v3/discord"
)
func ExampleContainerComponents_Unmarshal() {
components := &discord.ContainerComponents{
&discord.ActionRowComponent{
&discord.TextInputComponent{
CustomID: "text1",
2022-11-19 17:47:08 +00:00
Value: "hello",
},
},
&discord.ActionRowComponent{
&discord.TextInputComponent{
CustomID: "text2",
2022-11-19 17:47:08 +00:00
Value: "hello 2",
},
&discord.TextInputComponent{
CustomID: "text3",
2022-11-19 17:47:08 +00:00
Value: "hello 3",
},
},
&discord.ActionRowComponent{
2022-11-19 17:47:08 +00:00
&discord.StringSelectComponent{
CustomID: "select1",
Options: []discord.SelectOption{
{Value: "option 1"},
{Value: "option 2"},
},
},
&discord.ButtonComponent{
CustomID: "button1",
},
},
&discord.ActionRowComponent{
2022-11-19 17:47:08 +00:00
&discord.StringSelectComponent{
CustomID: "select2",
Options: []discord.SelectOption{
{Value: "option 1"},
},
},
},
}
var data struct {
Text1 string `discord:"text1"`
Text2 string `discord:"text2?"`
Text3 *string `discord:"text3"`
Text4 string `discord:"text4?"`
Text5 *string `discord:"text5"`
Select1 []string `discord:"select1"`
Select2 string `discord:"select2"`
Button1 bool `discord:"button1"`
}
if err := components.Unmarshal(&data); err != nil {
log.Fatalln(err)
}
b, _ := json.MarshalIndent(data, "", " ")
fmt.Println(string(b))
// Output:
// {
// "Text1": "hello",
// "Text2": "hello 2",
// "Text3": "hello 3",
// "Text4": "",
// "Text5": null,
// "Select1": [
// "option 1",
// "option 2"
// ],
// "Select2": "option 1",
// "Button1": true
// }
}