This repository has been archived by the owner on Aug 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
154 lines (138 loc) · 4.16 KB
/
main.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"bytes"
"context"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"github.com/disgoorg/disgo"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/disgo/gateway"
"github.com/disgoorg/log"
"github.com/disgoorg/snowflake/v2"
_ "github.com/joho/godotenv/autoload"
)
var (
token = os.Getenv("ff_token")
guildID = snowflake.GetEnv("ff_guild")
logLevel = os.Getenv("ff_log")
commands = []discord.ApplicationCommandCreate{
discord.SlashCommandCreate{
Name: "echo",
Description: "/ commands testing",
Options: []discord.ApplicationCommandOption{
discord.ApplicationCommandOptionString{
Name: "message",
Description: "What to say",
Required: true,
},
discord.ApplicationCommandOptionBool{
Name: "ephemeral",
Description: "If the response should only be visible to you",
Required: true,
},
},
},
discord.SlashCommandCreate{
Name: "verify-hash",
Description: "Verify hash from a string",
Options: []discord.ApplicationCommandOption{
discord.ApplicationCommandOptionString{
Name: "string",
Description: "The hashed string goes here",
Required: true,
},
},
},
}
)
func init() {
switch logLevel {
case "0":
log.SetLevel(log.LevelError)
case "1":
log.SetLevel(log.LevelWarn)
case "2":
log.SetLevel(log.LevelInfo)
case "3":
log.SetLevel(log.LevelDebug)
}
//Check if package is installed
out, err := exec.Command("npm", "list", "-g", "hash-detector-cli", "--depth", "1", "--parseable").Output()
if err != nil {
log.Fatal("Failed to run the command. Check if NPM is installed.\nDetails: ", err)
}
if !strings.Contains(string(out), "hash-detector-cli") {
log.Fatal("hash-detector-cli is not installed. First, install that package with npm")
}
}
func main() {
log.Info("Starting FastForward Bot...")
log.Info("Library (disgo) version: ", disgo.Version)
client, err := disgo.New(token,
bot.WithDefaultGateway(),
bot.WithEventListenerFunc(commandListener),
bot.WithGatewayConfigOpts(
gateway.WithAutoReconnect(true),
gateway.WithDevice("FastForward"),
gateway.WithOS("Android"),
gateway.WithPresence(gateway.NewPresence(discord.ActivityTypeGame, "with link shortners!", "https://fastforward.team", discord.OnlineStatusOnline, false)),
),
)
if err != nil {
log.Fatal("Error while building disgo instance!\nDetails: ", err)
return
}
defer client.Close(context.TODO())
if _, err = client.Rest().SetGuildCommands(client.ApplicationID(), guildID, commands); err != nil {
log.Fatal("Error while registering commands!\nDetails: ", err)
}
if err = client.OpenGateway(context.TODO()); err != nil {
log.Fatal("Error while connecting to gateway!\nDetails: ", err)
}
//print the bot's user name
log.Info("Bot ID: ", client.ApplicationID())
log.Infof("Fast Forward Bot is now running. Press CTRL-C to exit.")
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-s
}
func commandListener(event *events.ApplicationCommandInteractionCreate) {
data := event.SlashCommandInteractionData()
switch data.CommandName() {
case "echo":
err := event.CreateMessage(discord.NewMessageCreateBuilder().
SetContent(data.String("message")).
SetEphemeral(data.Bool("ephemeral")).
Build(),
)
if err != nil {
event.Client().Logger().Error("error on sending response: ", err)
}
case "verify-hash":
cmd := exec.Command("hash-detect", data.String("string"), "-p")
var result bytes.Buffer
cmd.Stdout = &result
err := cmd.Run()
if err != nil {
event.CreateMessage(discord.NewMessageCreateBuilder().
SetContent("Failed to verify the hash!").
SetEphemeral(data.Bool("ephemeral")).
Build())
event.Client().Logger().Error("Error on verifying hash: ", err)
}
log.Info(result.String())
err = event.CreateMessage(discord.NewMessageCreateBuilder().
SetContent(string("Input: `" + data.String("string") + "`\nPossible hashes: " + result.String())).
SetEphemeral(true).
Build(),
)
if err != nil {
event.Client().Logger().Error("error on sending response: ", err)
}
}
}