~shunter/cordcgi

3bcdeb65f2435ee3149bba41fa87396a9871c43f — Samuel Hunter 3 years ago 0706e9f
Add a bunch of env vars that look good
4 files changed, 32 insertions(+), 1 deletions(-)

M discordcgi.go
A example-cgi-bin/fortune
A example-cgi-bin/help
A example-cgi-bin/ping
M discordcgi.go => discordcgi.go +15 -1
@@ 90,10 90,24 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
	cmd := exec.Command(command, args[1:]...)
	var out bytes.Buffer

	// Parse timestamp parameter. Might fail, set timestamp to 0.
	var nanotime int64 = 0
	mtime, err := m.Timestamp.Parse()
	if err == nil {
		nanotime = mtime.UnixNano()
	}

	cmd.Stdout = &out
	cmd.Env = append(os.Environ(),
		"DISCORD_MESSAGE_AUTHOR="+m.Author.ID,
		"DISCORD_MESSAGE="+m.ID,
		"DISCORD_MESSAGE_AUTHOR="+m.Author.ID,
		"DISCORD_MESSAGE_AUTHOR_AVATAR="+m.Author.Avatar,
		"DISCORD_MESSAGE_AUTHOR_LOCALE="+m.Author.Locale,
		"DISCORD_MESSAGE_AUTHOR_USERNAME="+m.Author.Username,
		"DISCORD_MESSAGE_CHANNEL="+m.ChannelID,
		"DISCORD_MESSAGE_GUILD="+m.GuildID,
		"DISCORD_MESSAGE_UNIXNANOS="+fmt.Sprint(nanotime),

		// Yadda yadda put literally everything here
	)
	if err := cmd.Run(); err != nil {

A example-cgi-bin/fortune => example-cgi-bin/fortune +6 -0
@@ 0,0 1,6 @@
#!/bin/sh -eu
# Fortune wrapped in a cowsay

echo '```'
fortune | cowsay
echo '```'

A example-cgi-bin/help => example-cgi-bin/help +4 -0
@@ 0,0 1,4 @@
#!/bin/sh -eu
# List all executable files in the directory.
cd "$(dirname "$0")"
find -type f -perm -111 | sed 's#^./##'

A example-cgi-bin/ping => example-cgi-bin/ping +7 -0
@@ 0,0 1,7 @@
#!/bin/sh -eu

# milliseconds since message timestamp
delay="$(( ($(date +%s%N) - DISCORD_MESSAGE_UNIXNANOS) / 1000000))"

# ping the recipient with the delay
echo "<@${DISCORD_MESSAGE_AUTHOR}> Pong! (${delay}ms)"