/
cmd.go
110 lines (98 loc) · 3.2 KB
/
cmd.go
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package pomodoro
import (
"fmt"
"log"
"github.com/bwmarrin/discordgo"
)
var (
commands = []*discordgo.ApplicationCommand{
{
Name: "pomodoro",
Description: "pomodoro commands",
Options: []*discordgo.ApplicationCommandOption{
{
Name: "ping",
Description: "ping-pong",
Type: discordgo.ApplicationCommandOptionSubCommand,
},
{
Name: "start",
Description: "start pomodoro",
Type: discordgo.ApplicationCommandOptionSubCommand,
},
{
Name: "stop",
Description: "stop pomodoro",
Type: discordgo.ApplicationCommandOptionSubCommand,
},
},
},
}
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"pomodoro": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
log.Printf("pomodoro command: %+v", i.ApplicationCommandData())
options := i.ApplicationCommandData().Options
content := ""
switch options[0].Name {
case "ping":
content = "pong!"
case "start":
user := i.Member.User
content = fmt.Sprintf("Hi %s! See <#%s>!", user.Username, Info.GetChannelIDForNotification())
// user が VC にいるかどうかを確認する
var voiceState *discordgo.VoiceState
var err error
if voiceState, err = s.State.VoiceState(Info.GetGuildID(), user.ID); err != nil {
log.Printf("Failed to get %s's voice state: %v", user.Username, err)
}
if voiceState == nil {
log.Printf("User is not in voice channel")
content += "\n"
content += "You are not in voice channel."
break
}
log.Printf("User is in voice channel: %v", voiceState.ChannelID)
// VC にいる場合は pomodoro を開始する
if pomodoro, err := getPomodoroWithLock(s, i.GuildID, Info.GetChannelIDForNotification()); err != nil {
log.Println(err)
return
} else {
defer unlockPomodoro(Info.GetGuildID())
pomodoro.AddUser(*user)
}
case "stop":
user := i.Member.User
content = fmt.Sprintf("Bye %s! See <#%s>!", user.Username, Info.GetChannelIDForNotification())
// user が VC にいるかどうかを確認する
var voiceState *discordgo.VoiceState
var err error
if voiceState, err = s.State.VoiceState(Info.GetGuildID(), user.ID); err != nil {
log.Printf("Failed to get %s's voice state: %v", user.Username, err)
}
if voiceState == nil {
log.Printf("User is not in voice channel")
// いなければ忠告
content += "\n"
content += fmt.Sprintf("<@%s>! You should run `/pomodoro stop` command in some voice channel.", user.ID)
content += "\n"
content += "Otherwise, your server mute and deaf status may not be released."
}
// pomodoro を停止
if pomodoro, err := getPomodoroWithLock(s, i.GuildID, Info.GetChannelIDForNotification()); err != nil {
log.Println(err)
return
} else {
defer releaseOrUnlockPomodoro(pomodoro, i.GuildID)
pomodoro.RemoveMember(user.ID)
}
default:
}
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: content,
},
})
},
}
)