/
minecraft_register.go
112 lines (94 loc) · 3.08 KB
/
minecraft_register.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
package cmd
import (
"context"
"encoding/hex"
"fmt"
"slices"
"time"
discordClient "github.com/0supa/degen/client/discord"
"github.com/0supa/degen/client/pwd"
"github.com/0supa/degen/client/sql"
"github.com/0supa/degen/client/store"
"github.com/diamondburned/arikawa/v3/api"
"github.com/diamondburned/arikawa/v3/api/cmdroute"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/arikawa/v3/utils/json/option"
)
var userKeys = make(map[discord.UserID]*store.Key)
func init() {
guilds := []discord.GuildID{
1200915706661843074, // dulas
761682825439084544, // sus
776226518086451230, // xd
}
bannedUsers := []discord.UserID{
486605322924982284, // eduart.pxx
}
channelIDs := []discord.ChannelID{
1206783236387250266, // #mc-registration
797616247918821436, // #testing
}
RegisterCommand(Command{
Name: "minecraft",
Guilds: guilds,
DiscordData: api.CreateCommandData{
Name: "minecraft",
Description: "Minecraft SMP command",
Options: []discord.CommandOption{
&discord.SubcommandOption{
OptionName: "register",
Description: "Register a new account on the SMP server!",
},
},
},
DiscordHandler: func(ctx context.Context, cmd cmdroute.CommandData) *api.InteractionResponseData {
if !slices.Contains(guilds, cmd.Event.GuildID) {
return &api.InteractionResponseData{
Content: option.NewNullableString("You aren't allowed to run this command in this server"),
Flags: discord.EphemeralMessage,
}
}
discordUID := cmd.Event.SenderID()
if slices.Contains(bannedUsers, discordUID) {
return Response("Your account is prohibited from using this command")
}
if !slices.Contains(channelIDs, cmd.Event.ChannelID) {
return &api.InteractionResponseData{
Content: option.NewNullableString("Please use this command in <#1206783236387250266>"),
Flags: discord.EphemeralMessage,
}
}
if userKeys[discordUID] != nil && time.Now().Before(userKeys[discordUID].Expiry) {
return &api.InteractionResponseData{
Content: option.NewNullableString("Please wait some time before making a request again"),
Flags: discord.EphemeralMessage,
}
}
player, err := sql.GetPlayer("", discordUID.String())
if err == nil {
return Response("%s, you are already registered on the server", player.Name)
}
if err != sql.ErrNil {
return ErrorResponse(err)
}
salt := pwd.GenerateSalt(16)
hash := hex.EncodeToString(salt)
key := &store.Key{
Hash: hash,
User: *cmd.Event.Sender(),
Expiry: time.Now().Add(30 * time.Minute),
}
store.RegisterKeys[hash] = key
userKeys[discordUID] = key
dm, err := discordClient.Handler.CreatePrivateChannel(discordUID)
if err != nil {
return ErrorResponse(err)
}
_, err = discordClient.Handler.State.SendMessage(dm.ID, fmt.Sprintf("Please [**click here**](https://dulas.supa.sh/mc/register/?key=%s) in order to register.\nDo not share this link with anyone!", hash))
if err != nil {
return ErrorResponse(err)
}
return Response("You've been messaged a registration link!")
},
})
}