Skip to content

Commit b373ba6

Browse files
committed
improve message format
1 parent f7e8dd5 commit b373ba6

File tree

6 files changed

+21
-40
lines changed

6 files changed

+21
-40
lines changed

botservice/messages/messageCreate.go botservice/messages/message_create.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package messages
22

33
import (
44
"database/sql"
5+
"log"
56
"strings"
67

78
"github.com/bwmarrin/discordgo"
@@ -40,9 +41,12 @@ func MessageCmd(s *discordgo.Session, m *discordgo.MessageCreate) {
4041
-**Description:**
4142
` + msg.Description
4243

43-
_, _ = s.ChannelMessageSend(m.ChannelID, message)
44-
}
44+
_, err = s.ChannelMessageSend(m.ChannelID, message)
4545

46+
if err != nil {
47+
log.Println("cannot send message")
48+
log.Println(err.Error())
49+
}
50+
}
4651
}
47-
4852
}

botservice/storage/models.go

+4-20
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
package storage
22

3-
import (
4-
"log"
5-
"strings"
6-
)
7-
83
type ResponseCMD struct {
94
ID int
105
CMD string
@@ -17,21 +12,10 @@ type ChallengeResponse struct {
1712
ChallengeType string
1813
}
1914

20-
func BuildResponse(res string) *ChallengeResponse {
21-
values := strings.Split(res, ",")
22-
23-
// the response from the lib is
24-
// type,description,level
25-
26-
if len(values) != 3 {
27-
log.Println("response broken from lib")
28-
return &ChallengeResponse{}
29-
}
30-
15+
func BuildResponse(desc, level, challengeType string) *ChallengeResponse {
3116
return &ChallengeResponse{
32-
ChallengeType: values[0],
33-
Description: values[1],
34-
Level: values[2],
17+
ChallengeType: challengeType,
18+
Description: desc,
19+
Level: level,
3520
}
36-
3721
}

botservice/storage/querys.go botservice/storage/queries.go

+6-13
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package storage
33
import (
44
"github.com/tomiok/challenge-lib"
55
"log"
6-
"math/rand"
76
"strings"
8-
"time"
97
)
108

119
func (res *ResponseCMD) GetCmd(cmd string) (*ResponseCMD, error) {
@@ -24,18 +22,13 @@ func (res *ResponseCMD) GetCmd(cmd string) (*ResponseCMD, error) {
2422
func (res *ChallengeResponse) GetChallenge(cmd string) (*ChallengeResponse, error) {
2523
values := strings.Split(cmd, " ")
2624

27-
if len(values) == 0 || len(values) == 1 {
25+
if len(values) == 0 || len(values) == 1 || len(values) == 3 {
2826
log.Println("using default values for find a challenge")
2927
message := challengelib.FindChallenge("easy", "backend")
30-
return BuildResponse(message), nil
28+
return BuildResponse(message, "easy", "backend"), nil
3129
}
32-
33-
message := challengelib.FindChallenge(values[0], values[1])
34-
return BuildResponse(message), nil
35-
}
36-
37-
func randomId(count int) int {
38-
rand.Seed(time.Now().UTC().UnixNano())
39-
// valor entre 1 y count
40-
return rand.Intn(count-1) + 1
30+
level := values[2]
31+
challengeType := values[3]
32+
message := challengelib.FindChallenge(level, challengeType)
33+
return BuildResponse(message, level, challengeType), nil
4134
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/bwmarrin/discordgo v0.23.2
77
github.com/go-sql-driver/mysql v1.6.0
88
github.com/gofiber/fiber/v2 v2.17.0
9-
github.com/tomiok/challenge-lib v0.0.2
9+
github.com/tomiok/challenge-lib v0.0.4
1010
gorm.io/driver/mysql v1.1.2
1111
gorm.io/gorm v1.21.13
1212
)

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ github.com/jinzhu/now v1.1.2 h1:eVKgfIdy9b6zbWBMgFpfDPoAMifwSZagU9HmEU6zgiI=
1717
github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
1818
github.com/klauspost/compress v1.12.2 h1:2KCfW3I9M7nSc5wOqXAlW2v2U6v+w6cbjvbfp+OykW8=
1919
github.com/klauspost/compress v1.12.2/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
20-
github.com/tomiok/challenge-lib v0.0.2 h1:rqtZRekEVussXQxV80dHJZHBYLmEAcXsTPAL1qqvUbE=
21-
github.com/tomiok/challenge-lib v0.0.2/go.mod h1:cqcMANTp4P8KnhiOy6EgcryjupblrlzPYECah2V/Sp4=
20+
github.com/tomiok/challenge-lib v0.0.4 h1:IVZVHzpdVZXnNxcWKq9ZMtMxC14gPtIIxaTt93r0y0k=
21+
github.com/tomiok/challenge-lib v0.0.4/go.mod h1:cqcMANTp4P8KnhiOy6EgcryjupblrlzPYECah2V/Sp4=
2222
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
2323
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
2424
github.com/valyala/fasthttp v1.26.0 h1:k5Tooi31zPG/g8yS6o2RffRO2C9B9Kah9SY8j/S7058=

vendor/modules.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ github.com/jinzhu/now
2929
github.com/klauspost/compress/flate
3030
github.com/klauspost/compress/gzip
3131
github.com/klauspost/compress/zlib
32-
# github.com/tomiok/challenge-lib v0.0.1
32+
# github.com/tomiok/challenge-lib v0.0.4
3333
## explicit
3434
github.com/tomiok/challenge-lib
3535
# github.com/valyala/bytebufferpool v1.0.0

0 commit comments

Comments
 (0)