aboutsummaryrefslogtreecommitdiff
path: root/pkg/discord/commands.go
blob: 5d337862f8eac381896bd51c2198903f0c62d7f3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright (C) 2021-2021 The Whirlsplash Collective
// SPDX-License-Identifier: GPL-3.0-only

package discord

import (
	"github.com/bwmarrin/discordgo"
	"github.com/spf13/viper"
	"log"
)

var (
	commands = []*discordgo.ApplicationCommand{
		{
			Name:        "ping",
			Description: "Pong!",
		},
	}
	commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
		"ping": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
			s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
				Type: discordgo.InteractionResponseChannelMessageWithSource,
				Data: &discordgo.InteractionResponseData{
					Content: "Pong!",
				},
			})
		},
	}
)

func CleanupCommands() {
	cmds, err := s.ApplicationCommands(s.State.User.ID, "")
	if err != nil {
		log.Panicln("Unable to obtain applications commands:", err)
	}

	for _, e := range cmds {
		if err := s.ApplicationCommandDelete(s.State.User.ID, "", e.ID); err != nil {
			log.Panicf("Cannot delete command '%v': %v", e.Name, err)
		} else {
			log.Printf("Deleted command '%v'", e.Name)
		}
	}
}

func SetupCommands() {
	for _, v := range commands {
		if _, err := s.ApplicationCommandCreate(s.State.User.ID, "", v); err != nil {
			log.Panicf("Cannot create command '%v': %v", v.Name, err)
		} else {
			log.Printf("Created command '%v'", v.Name)
		}
	}

	if viper.GetBool("discord.account.cleanup_app_commands") {
		CleanupCommands()
	}
}