command

package
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 3, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DeleteCommand = cli.Command{
	Name:      "delete",
	Usage:     "Delete message",
	ArgsUsage: "<message id>",
	Aliases:   []string{"d"},
	Action: func(c *cli.Context) error {
		client := *webhook.New(discord.WebhookID(c.Int("id")), c.String("token"))

		messageId, err := strconv.Atoi(c.Args().First())
		if err != nil {
			return errors.New("message id not set or not integer")
		}

		err = client.DeleteMessage(discord.MessageID(messageId))
		if err != nil {
			return err
		}

		return nil
	},
}
View Source
var EditCommand = cli.Command{
	Name:      "edit",
	Usage:     "Edit message",
	ArgsUsage: "<message id>",
	Aliases:   []string{"e"},
	Action: func(c *cli.Context) error {
		client := *webhook.New(discord.WebhookID(c.Int("id")), c.String("token"))

		messageId, err := strconv.Atoi(c.Args().First())
		if err != nil {
			return errors.New("message id not set or not integer")
		}

		if util.IsStdin() {
			s := strings.Join(util.ReadStdin(), "\n")
			data := webhook.EditMessageData{}
			if json.Valid([]byte(s)) {
				json.Unmarshal([]byte(s), &data)
			} else {
				data.Content = option.NewNullableString(s)
			}

			message, err := client.EditMessage(discord.MessageID(messageId), data)

			if c.Bool("json") {
				format := c.Bool("format")

				msg, err := util.StringifyObject(message, format)
				if err != nil {
					return err
				}

				fmt.Println(msg)
				return nil
			}

			return err
		}

		if len(c.String("payload")) > 0 {
			payload := c.String("payload")
			data := webhook.EditMessageData{}
			json.Unmarshal([]byte(payload), &data)

			message, err := client.EditMessage(discord.MessageID(messageId), data)

			if c.Bool("json") {
				format := c.Bool("format")

				msg, err := util.StringifyObject(message, format)
				if err != nil {
					return err
				}

				fmt.Println(msg)
				return nil
			}

			return err
		}

		if len(c.String("content")) == 0 &&
			len(c.StringSlice("file")) == 0 &&
			!c.Bool("embed") {
			return errors.New("no content, file, or embed supplied")
		}

		files, err := util.BuildFilesFromContext(c)
		if err != nil {
			return err
		}

		embeds, err := util.BuildEmbedsFromContext(c)
		if err != nil {
			return err
		}

		data := webhook.EditMessageData{
			Content: option.NewNullableString(c.String("content")),
			Files:   files,
		}

		if c.Bool("embed") {
			data.Embeds = &embeds
		}

		message, err := client.EditMessage(
			discord.MessageID(messageId),
			data,
		)

		if err != nil {
			return err
		}

		if c.Bool("json") {
			format := c.Bool("format")

			msg, err := util.StringifyObject(message, format)
			if err != nil {
				return err
			}

			fmt.Println(msg)
			return nil
		}

		return nil
	},
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:    "content",
			Usage:   "plain text",
			Aliases: []string{"c"},
		},
		&cli.StringSliceFlag{
			Name:      "file",
			Usage:     "webhook attachment",
			Aliases:   []string{"f"},
			TakesFile: true,
		},
		&cli.BoolFlag{
			Name:    "embed",
			Usage:   "include embed",
			Aliases: []string{"e"},
		},
		&cli.StringFlag{Name: "embed-title", Usage: "embed title"},
		&cli.StringFlag{Name: "embed-description", Usage: "embed description"},
		&cli.StringFlag{Name: "embed-url", Usage: "embed url"},
		&cli.StringFlag{Name: "embed-timestamp", Usage: "embed timestamp (now|RFC3339 timestamp)"},
		&cli.IntFlag{Name: "embed-color", Usage: "embed color (hex)"},
		&cli.StringFlag{Name: "embed-footer-text", Usage: "embed footer text"},
		&cli.StringFlag{Name: "embed-footer-icon", Usage: "embed footer icon"},
		&cli.StringFlag{Name: "embed-image-url", Usage: "embed image url"},
		&cli.StringFlag{Name: "embed-thumbnail-url", Usage: "embed thumbnail url"},
		&cli.StringFlag{Name: "embed-video-url", Usage: "embed video url"},
		&cli.StringFlag{Name: "embed-provider-name", Usage: "embed provider name"},
		&cli.StringFlag{Name: "embed-provider-url", Usage: "embed provider url"},
		&cli.StringFlag{Name: "embed-author-name", Usage: "embed author name"},
		&cli.StringFlag{Name: "embed-author-url", Usage: "embed author url"},
		&cli.StringFlag{Name: "embed-author-icon", Usage: "embed author icon"},
		&cli.StringSliceFlag{Name: "embed-field", Usage: "embed field (name,value,inline)"},
		&cli.StringFlag{
			Name:    "payload",
			Usage:   "raw json payload",
			Aliases: []string{"p"}},
		&cli.BoolFlag{Name: "json", Usage: "output message object in json"},
		&cli.BoolFlag{Name: "format", Usage: "format output"},
	},
}
View Source
var SendCommand = cli.Command{
	Name:    "send",
	Usage:   "Send message",
	Aliases: []string{"s"},
	Action: func(c *cli.Context) error {
		client := *webhook.New(discord.WebhookID(c.Int("id")), c.String("token"))

		if util.IsStdin() {
			s := strings.Join(util.ReadStdin(), "\n")
			data := webhook.ExecuteData{}
			if json.Valid([]byte(s)) {
				json.Unmarshal([]byte(s), &data)
			} else {
				data.Content = s
			}

			if c.Bool("wait") {
				message, err := client.ExecuteAndWait(data)
				if err != nil {
					return err
				}

				if c.Bool("json") {
					format := c.Bool("format")

					msg, err := util.StringifyObject(message, format)
					if err != nil {
						return err
					}

					fmt.Println(msg)
					return nil
				}
			} else if c.Bool("json") {
				return errors.New("must wait for message before output (use --wait)")
			}

			err := client.Execute(data)
			return err
		}

		if len(c.String("payload")) > 0 {
			payload := c.String("payload")
			data := webhook.ExecuteData{}
			json.Unmarshal([]byte(payload), &data)

			if c.Bool("wait") {
				message, err := client.ExecuteAndWait(data)
				if err != nil {
					return err
				}

				if c.Bool("json") {
					format := c.Bool("format")

					msg, err := util.StringifyObject(message, format)
					if err != nil {
						return err
					}

					fmt.Println(msg)
					return nil
				}
			} else if c.Bool("json") {
				return errors.New("must wait for message before output (use --wait)")
			}

			err := client.Execute(data)
			return err
		}

		if len(c.String("content")) == 0 &&
			len(c.StringSlice("file")) == 0 &&
			!c.Bool("embed") {
			return errors.New("no content, file, or embed supplied")
		}

		files, err := util.BuildFilesFromContext(c)
		if err != nil {
			return err
		}

		embeds, err := util.BuildEmbedsFromContext(c)
		if err != nil {
			return err
		}

		data := webhook.ExecuteData{
			Content:   c.String("content"),
			Username:  c.String("username"),
			AvatarURL: c.String("avatar-url"),
			TTS:       c.Bool("tts"),
			Files:     files,
		}

		if c.Bool("embed") {
			data.Embeds = embeds
		}

		if c.Bool("wait") {
			message, err := client.ExecuteAndWait(data)
			if err != nil {
				return err
			}

			if c.Bool("json") {
				format := c.Bool("format")

				msg, err := util.StringifyObject(message, format)
				if err != nil {
					return err
				}

				fmt.Println(msg)
				return nil
			}
		} else if c.Bool("json") {
			return errors.New("must wait for message before output (use --wait)")
		}

		err = client.Execute(data)
		if err != nil {
			return err
		}

		return nil
	},
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:    "content",
			Usage:   "plain text",
			Aliases: []string{"c"},
		},
		&cli.StringFlag{
			Name:    "username",
			Usage:   "webhook username",
			Aliases: []string{"u"},
		},
		&cli.StringFlag{
			Name:    "avatar-url",
			Usage:   "webhook avatar url",
			Aliases: []string{"a"},
		},
		&cli.BoolFlag{
			Name:    "tts",
			Usage:   "narrate message",
			Aliases: []string{"t"},
		},
		&cli.StringSliceFlag{
			Name:      "file",
			Usage:     "webhook attachment",
			Aliases:   []string{"f"},
			TakesFile: true,
		},
		&cli.BoolFlag{
			Name:    "embed",
			Usage:   "include embed",
			Aliases: []string{"e"},
		},
		&cli.StringFlag{Name: "embed-title", Usage: "embed title"},
		&cli.StringFlag{Name: "embed-description", Usage: "embed description"},
		&cli.StringFlag{Name: "embed-url", Usage: "embed url"},
		&cli.StringFlag{Name: "embed-timestamp", Usage: "embed timestamp (now|RFC3339 timestamp)"},
		&cli.IntFlag{Name: "embed-color", Usage: "embed color (hex)"},
		&cli.StringFlag{Name: "embed-footer-text", Usage: "embed footer text"},
		&cli.StringFlag{Name: "embed-footer-icon", Usage: "embed footer icon"},
		&cli.StringFlag{Name: "embed-image-url", Usage: "embed image url"},
		&cli.StringFlag{Name: "embed-thumbnail-url", Usage: "embed thumbnail url"},
		&cli.StringFlag{Name: "embed-video-url", Usage: "embed video url"},
		&cli.StringFlag{Name: "embed-provider-name", Usage: "embed provider name"},
		&cli.StringFlag{Name: "embed-provider-url", Usage: "embed provider url"},
		&cli.StringFlag{Name: "embed-author-name", Usage: "embed author name"},
		&cli.StringFlag{Name: "embed-author-url", Usage: "embed author url"},
		&cli.StringFlag{Name: "embed-author-icon", Usage: "embed author icon"},
		&cli.StringSliceFlag{Name: "embed-field", Usage: "embed field (name,value,inline)"},
		&cli.StringFlag{
			Name:    "payload",
			Usage:   "raw json payload",
			Aliases: []string{"p"}},
		&cli.BoolFlag{
			Name:    "wait",
			Usage:   "wait for message to be created",
			Aliases: []string{"w"}},
		&cli.BoolFlag{Name: "json", Usage: "output message object in json"},
		&cli.BoolFlag{Name: "format", Usage: "format output"},
	},
}

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL