Skip to content

Commit 6795ba7

Browse files
committed
Bug/#21 사용자 랜덤 선택 기능 오류 (#22)
* fix: prevent `PickRandomNumber` panic when `upperBound` is 1 - Add a test case for `PickRandomNumber` to cover edge cases. * chore: add log for channel member count validation * chore: add discord intents options
1 parent bfeb4fa commit 6795ba7

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

cmd/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func main() {
3232
log.Fatal("error creating Discord session,", err)
3333
}
3434

35-
discord.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsGuildMessageReactions | discordgo.IntentsGuilds
35+
discord.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsGuildMessageReactions | discordgo.IntentsGuilds | discordgo.IntentsGuildVoiceStates | discordgo.IntentsGuildMembers
3636

3737
discord.AddHandler(handler.EasterEggHandler)
3838
discord.AddHandler(handler.RegisterCommands)

internal/handler/handlers.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ func getRandomUserByChannel(session *discordgo.Session, interaction *discordgo.I
194194
sendMessage(session, interaction, "음성 채널에서만 사용할 수 있는 명령어입니다.")
195195
return
196196
}
197-
log.Printf("Channel type: %v", channel.Type)
198197
guild, err := session.State.Guild(channel.GuildID)
199198
// 서버 정보를 불러오는 중 오류가 발생하면 에러 메시지를 전송합니다.
200199
if err != nil {
@@ -215,6 +214,7 @@ func getRandomUserByChannel(session *discordgo.Session, interaction *discordgo.I
215214
}
216215
}
217216
}
217+
log.Printf("Members: %+v", members)
218218
// 음성 채널에 사용자가 없으면 에러 메시지를 전송합니다.
219219
if len(members) == 0 {
220220
sendMessage(session, interaction, "음성 채널에 사용자가 없습니다.")

internal/util/util.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,5 @@ func PickRandomNumber(upperBound int) int {
6464
}
6565
source := rand.NewSource(time.Now().UnixNano())
6666
r := rand.New(source)
67-
return r.Intn(upperBound - 1)
67+
return r.Intn(upperBound)
6868
}

internal/util/util_test.go

+26-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package util
22

33
import (
44
"os"
5+
"strconv"
56
"testing"
67
"time"
78
)
@@ -109,13 +110,30 @@ func TestLoadFile_InvalidFile(t *testing.T) {
109110
}
110111
}
111112

112-
func TestPickRandomNumber(t *testing.T) {
113-
// Arrange
114-
upperBound := 10
115-
// Act
116-
actual := PickRandomNumber(upperBound)
117-
// Assert
118-
if actual < 0 || actual >= upperBound {
119-
t.Errorf("expected 0 <= actual < %d, but got %d", upperBound, actual)
113+
func TestPickRandomNumber_ValidInput(t *testing.T) {
114+
testCases := []int{1, 10, 100, 1000, 10000}
115+
for _, tc := range testCases {
116+
t.Run("UpperBound: "+strconv.Itoa(tc), func(t *testing.T) {
117+
// Act
118+
actual := PickRandomNumber(tc)
119+
// Assert
120+
if actual < 0 || actual >= tc {
121+
t.Errorf("expected 0 <= actual < %d, but got %d", tc, actual)
122+
}
123+
})
124+
}
125+
}
126+
127+
func TestPickRandomNumber_InputLessThenOrEqualZero(t *testing.T) {
128+
testCases := []int{0, -1, -10, -100, -1000}
129+
for _, tc := range testCases {
130+
t.Run("UpperBound: "+strconv.Itoa(tc), func(t *testing.T) {
131+
// Act
132+
actual := PickRandomNumber(tc)
133+
// Assert
134+
if actual != 0 {
135+
t.Errorf("expected 0, but got %d", actual)
136+
}
137+
})
120138
}
121139
}

0 commit comments

Comments
 (0)