119 lines
2.7 KiB
Go
119 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
"strings"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
)
|
|
|
|
type optionMap = map[string]*discordgo.ApplicationCommandInteractionDataOption
|
|
|
|
func parseOptions(options []*discordgo.ApplicationCommandInteractionDataOption) (om optionMap) {
|
|
om = make(optionMap)
|
|
for _, opt := range options {
|
|
om[opt.Name] = opt
|
|
}
|
|
return
|
|
}
|
|
|
|
func interactionAuthor(i *discordgo.Interaction) *discordgo.User {
|
|
if i.Member != nil {
|
|
return i.Member.User
|
|
}
|
|
return i.User
|
|
}
|
|
|
|
func handleEcho(s *discordgo.Session, i *discordgo.InteractionCreate, opts optionMap) {
|
|
builder := new(strings.Builder)
|
|
if v, ok := opts["author"]; ok && v.BoolValue() {
|
|
author := interactionAuthor(i.Interaction)
|
|
builder.WriteString("**" + author.String() + "** says: ")
|
|
}
|
|
builder.WriteString(opts["message"].StringValue())
|
|
|
|
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
Data: &discordgo.InteractionResponseData{
|
|
Content: builder.String(),
|
|
},
|
|
})
|
|
|
|
if err != nil {
|
|
log.Panicf("could not respond to interaction: %s", err)
|
|
}
|
|
}
|
|
|
|
var commands = []*discordgo.ApplicationCommand{
|
|
{
|
|
Name: "echo",
|
|
Description: "Say something through a bot",
|
|
Options: []*discordgo.ApplicationCommandOption{
|
|
{
|
|
Name: "message",
|
|
Description: "Contents of the message",
|
|
Type: discordgo.ApplicationCommandOptionString,
|
|
Required: true,
|
|
},
|
|
{
|
|
Name: "author",
|
|
Description: "Whether to prepend message's author",
|
|
Type: discordgo.ApplicationCommandOptionBoolean,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
var (
|
|
Token = flag.String("token", "", "Bot authentication token")
|
|
App = flag.String("app", "", "Application ID")
|
|
Guild = flag.String("guild", "", "Guild ID")
|
|
)
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
if *App == "" {
|
|
log.Fatal("application id is not set")
|
|
}
|
|
|
|
session, _ := discordgo.New("Bot " + *Token)
|
|
|
|
session.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
if i.Type != discordgo.InteractionApplicationCommand {
|
|
return
|
|
}
|
|
|
|
data := i.ApplicationCommandData()
|
|
if data.Name != "echo" {
|
|
return
|
|
}
|
|
|
|
handleEcho(s, i, parseOptions(data.Options))
|
|
})
|
|
|
|
session.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
|
|
log.Printf("Logged in as %s", r.User.String())
|
|
})
|
|
|
|
_, err := session.ApplicationCommandBulkOverwrite(*App, *Guild, commands)
|
|
if err != nil {
|
|
log.Fatalf("could not register commands: %s", err)
|
|
}
|
|
|
|
err = session.Open()
|
|
if err != nil {
|
|
log.Fatalf("could not open session: %s", err)
|
|
}
|
|
|
|
sigch := make(chan os.Signal, 1)
|
|
signal.Notify(sigch, os.Interrupt)
|
|
<-sigch
|
|
|
|
err = session.Close()
|
|
if err != nil {
|
|
log.Printf("could not close session gracefully: %s", err)
|
|
}
|
|
}
|