cmd

package
v0.0.0-...-5bff2fc Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2019 License: MIT Imports: 13 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AppId string
View Source
var CountDownCmd = Salamander.NewCommand("countdown").
	WithValidator(Salamander.RequiresMinArgs(1)).
	WithHandler(func(cmd *Salamander.Command, ctx context.Context, p *Salamander.Provider) error {
		countdown, err := strconv.Atoi(cmd.GetArgs()[0])
		if err != nil {
			return err
		}
		go func() {
			text := "%d seconds remaining!"
			edit, err := p.Session.ChannelMessageSend(p.Channel.ID, fmt.Sprintf(text, countdown))
			for countdown > 0 {
				if err != nil {
					return
				}
				countdown--
				<-time.After(1 * time.Second)
				edit, err = p.Session.ChannelMessageEdit(p.Channel.ID, edit.ID, fmt.Sprintf(text, countdown))
			}

			edit, err = p.Session.ChannelMessageEdit(p.Channel.ID, edit.ID, "Time is up!")
		}()

		return nil
	}).Build()
View Source
var EchoCmd = Salamander.NewCommand("echo").
	WithValidator(Salamander.RequiresMinArgs(1)).
	WithHandler(func(cmd *Salamander.Command, ctx context.Context, p *Salamander.Provider) error {
		_, err := p.Session.ChannelMessageSend(p.Channel.ID, strings.Join(cmd.GetArgs(), " "))
		return err
	}).Build()
View Source
var (
	EmojiCancel = "❌"
)
View Source
var (
	ErrCannotExecute = errors.New("cannot execute")
)
View Source
var GolangCmd = Salamander.NewCommand("golang").
	WithShortName("go").
	WithHandler(func(cmd *Salamander.Command, ctx context.Context, p *Salamander.Provider) error {
		code := strings.Join(cmd.GetArgs(), " ")
		if code == "" {
			code = "..."
		}
		_, err := p.Session.ChannelMessageSend(p.Channel.ID, "```if err != nil {\n\t"+code+"\n}```")
		return err
	}).Build()
View Source
var Plugin = &cmd{
	Base: plugins.Base{
		BaseName:        "cmd",
		BaseDescription: "Provides command processing",
	},
	cmdTree: Salamander.NewCommand("root").Build(),
	prefix:  "!",
}
View Source
var TrainCmd = Salamander.NewCommand("train").
	CapturesInput(true).
	WithValidator(Salamander.RequiresMulti(Salamander.RequiresMinArgs(1), func(args []string) error {
		_, err := strconv.Atoi(args[0])
		if err != nil {
			return errors.Wrap(err, "first arg must be an integer")
		}
		return nil
	})).WithHandler(func(cmd *Salamander.Command, ctx context.Context, p *Salamander.Provider) error {
	train := "🚂"
	caboose := "🚋"
	args := cmd.GetArgs()
	if len(args) > 1 {
		caboose = strings.Join(args[1:], " ")
	}
	edit, err := p.Session.ChannelMessageSend(p.Channel.ID, train)
	if err != nil {
		return err
	}
	err = p.Session.MessageReactionAdd(p.Channel.ID, edit.ID, "❌")
	if err != nil {
		return err
	}

	for n, _ := strconv.Atoi(args[0]); n > 0; n-- {
		select {
		case <-time.After(1 * time.Second):
			train = caboose + train
			edit, err = p.Session.ChannelMessageEdit(p.Channel.ID, edit.ID, train)
			if err != nil {
				return err
			}
		case <-ctx.Done():
			return nil
		}

	}

	return nil
}).Build()
View Source
var WolframAlphaCmd = Salamander.NewCommand("wa").
	WithValidator(Salamander.RequiresMulti(Salamander.RequiresMinArgs(1))).
	WithHandler(func(cmd *Salamander.Command, ctx context.Context, p *Salamander.Provider) error {

		url := "http://api.wolframalpha.com/v1/query"
		hc := http.Client{}
		req, err := http.NewRequest("GET", url, nil)
		if err != nil {
			return err
		}
		q := req.URL.Query()
		q.Add("output", "json")
		q.Add("appid", AppId)
		q.Add("input", strings.Join(cmd.GetArgs(), " "))
		req.URL.RawQuery = q.Encode()

		resp, err := hc.Do(req)
		if err != nil {
			return err
		}

		if resp.StatusCode == http.StatusOK {

			defer resp.Body.Close()
			data, err := ioutil.ReadAll(resp.Body)
			if err != nil {
				return err
			}

			var objmap map[string]interface{}
			err = json.Unmarshal(data, &objmap)
			if err != nil {
				return err
			}

			var queryResults QueryResults
			qr := objmap["queryresult"]
			err = mapstructure.Decode(qr, &queryResults)
			if err != nil {
				return err
			}

			Debug := true

			if Debug {

				_, err = p.Session.ChannelMessageSend(p.Channel.ID, req.URL.String())
			}

			for _, pod := range queryResults.Pods {
				if len(pod.SubPods) > 0 {
					for _, subpod := range pod.SubPods {
						if subpod.Image.Source != "" {

							img := discordgo.MessageEmbedImage{URL: subpod.Image.Source}
							embed := discordgo.MessageEmbed{Image: &img}
							_, err := p.Session.ChannelMessageSendEmbed(p.Channel.ID, &embed)
							if err != nil {
								return err
							}
						}
					}
				}
			}
		}
		return nil
	}).Build()

Functions

This section is empty.

Types

type Executor

type Executor struct {
	Cmd          *Salamander.Command
	CaptureInput bool
	Input        chan<- *discordgo.Message
	Done         <-chan struct{}
	End          context.CancelFunc
}

type Image

type Image struct {
	Source string `mapstructure:"src"`
}

type Pod

type Pod struct {
	Id      string `mapstructure:"id"`
	Title   string `mapstructure:"title"`
	SubPods []Pod  `mapstructure:"subpods"`
	Image   Image  `mapstructure:"img"`
}

type QueryResults

type QueryResults struct {
	Pods []Pod `mapstructure:"pods"`
}

type Store

type Store struct {
	Name            string
	Enabled         bool
	RequiredRoleIDs []string
}

Jump to

Keyboard shortcuts

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