-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_command.go
65 lines (61 loc) · 1.49 KB
/
run_command.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
package haccerinteractions
import (
"errors"
"net/http"
"strings"
"time"
"github.com/bwmarrin/discordgo"
)
// Run a command in a channel
func (hir *haccerInteractionsRunner) GuildChannelRunCommand(c Command, args *[]CommandRunOption, gc GuildChannel) (*discordgo.Message, error) {
if args == nil {
args = &[]CommandRunOption{}
}
reqData := commandRunRequest{
Type: 2,
BotID: c.ApplicationID,
GuildID: gc.GuildID,
ChannelID: gc.ChannelID,
SessionID: hir.Session.State.SessionID,
Data: commandRunData{
ID: c.ID,
Type: c.Type,
Name: c.Name,
Version: c.Version,
Options: *args,
},
}
cmdMutex := hir.getCommandMutex(c.Name)
cmdMutex.Lock()
commandRespChan := make(chan discordgo.Message)
deleteHand := hir.Session.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Interaction == nil {
return
}
words := strings.Split(m.Interaction.Name, " ")
if len(words) < 1 {
return
}
if m.Interaction.User.ID == s.State.User.ID && words[0] == c.Name && m.Author.ID == c.ApplicationID {
commandRespChan <- *m.Message
cmdMutex.Unlock()
}
})
_, err := hir.Session.Request(http.MethodPost, "https://discord.com/api/v9/interactions", reqData)
if err != nil {
return nil, err
}
timer := time.NewTimer(time.Second * 30)
select {
case cmdRespMsg := <-commandRespChan:
{
deleteHand()
return &cmdRespMsg, nil
}
case <-timer.C:
{
deleteHand()
return nil, errors.New("timeout looking for response")
}
}
}