Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.exe
*.ps1
*.bash
Dockerfile
.dockerignore
.gitignore
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/start.bat
*.exe
/start.ps1
/start.ps1
*.code-workspace
24 changes: 24 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Build
FROM golang:1.21-alpine AS build
WORKDIR /app

# Copy and download dependencies
COPY go.mod ./
COPY go.sum ./
RUN go mod download

# Copy source code
COPY . ./

# Build the Go application
RUN go build -o /Discrodbot

## Deploy
FROM alpine:latest

WORKDIR /

# Copy the binary from the build stage
COPY --from=build /Discrodbot /Discrodbot
# Set the entrypoint
CMD [ "/Discrodbot" ]
37 changes: 22 additions & 15 deletions actions/DataActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"log"

"github.com/bwmarrin/discordgo"
"github.com/pkwiatek6/DiscrodBot/data"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
Expand All @@ -17,16 +18,16 @@ const (
Collection = "Sheets"
)

//SaveCharacter saves player data to a noSQL db
// SaveCharacter saves player data to a noSQL db
func SaveCharacter(character data.Character, client *mongo.Client) error {
collection := client.Database(Database).Collection(Collection)
//will get readded when I figure out why discordgo isn't giving be user discriminator
//filter := bson.D{{Key: "name", Value: character.Name}}
filter := bson.M{"name": character.Name, "user": character.User}
update := bson.M{"$set": character}
updateResult, err1 := collection.UpdateOne(context.TODO(), filter, update)
if err1 != nil {
return err1
updateResult, err := collection.UpdateOne(context.TODO(), filter, update)
if err != nil {
return err
//checks if there was a document that was updated and if so finish saving
} else if updateResult.MatchedCount == 0 {
log.Println("Failed to find matching document, making a new one")
Expand All @@ -35,15 +36,15 @@ func SaveCharacter(character data.Character, client *mongo.Client) error {
return nil
}
//Creates a new document if there wasn't one already
insertResult, err2 := collection.InsertOne(context.TODO(), character)
if err2 != nil {
return err2
insertResult, err := collection.InsertOne(context.TODO(), character)
if err != nil {
return err
}
log.Println("Inserted post with ID:", insertResult.InsertedID)
return nil
}

//LoadCharacter loads a given character by name, I'm probably also gonna require it to look up User ID
// LoadCharacter loads a given character by name, I'm probably also gonna require it to look up User ID
func LoadCharacter(name string, user string, client *mongo.Client) (*data.Character, error) {
filter := bson.M{"name": name, "user": user}
collection := client.Database(Database).Collection(Collection)
Expand All @@ -56,7 +57,7 @@ func LoadCharacter(name string, user string, client *mongo.Client) (*data.Charac
return &character, nil
}

//LoadAllCharacters loads all the characters into memory
// LoadAllCharacters loads all the characters into memory
func LoadAllCharacters(client *mongo.Client) (map[string]*data.Character, error) {
var results []data.Character
var toReturn = make(map[string]*data.Character)
Expand All @@ -75,10 +76,16 @@ func LoadAllCharacters(client *mongo.Client) (map[string]*data.Character, error)
return toReturn, nil
}

//SaveAllCharacters saves all the characters to the DB
func SaveAllCharacters(Characters map[string]*data.Character, client *mongo.Client) error {
var err error
// SaveAllCharacters saves all the characters to the DB
func SaveAllCharacters(Characters map[string]*data.Character, client *mongo.Client, discord *discordgo.Session, guildID string) error {
for _, character := range Characters {
//Before saving the character get the current nick name so the bot correctly adress users
member, err := discord.GuildMember(guildID, character.User)
if err != nil {
log.Printf("Failed to retrieve member data for user %s: %v\n", character.User, err)
return err
}
character.Name = member.Nick
err = SaveCharacter(*character, client)
if err != nil {
return err
Expand All @@ -87,10 +94,10 @@ func SaveAllCharacters(Characters map[string]*data.Character, client *mongo.Clie
return nil
}

//ConnectDB makes a client that can be called again and again to reference the database, call this first to create a Client
func ConnectDB() (*mongo.Client, error) {
// ConnectDB makes a client that can be called again and again to reference the database, call this first to create a Client
func ConnectDB(URI string) (*mongo.Client, error) {
// Set client options
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
clientOptions := options.Client().ApplyURI(URI)

// Connect to MongoDB
client, err := mongo.Connect(context.TODO(), clientOptions)
Expand Down
72 changes: 48 additions & 24 deletions actions/botActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,20 @@ import (
"github.com/pkwiatek6/DiscrodBot/data"
)

//RollD10 rolls a single d10 and returns the outcome
// RollD10 rolls a single d10 and returns the outcome
func RollD10() int {
return rand.Intn(10) + 1
}

//Rolls fudge dice go from min to ten
// Rolls fudge dice go from min to ten
func RollDF(minFudge int) int {
if minFudge <= 10 && minFudge > 0 {
return rand.Intn(10-minFudge+1) + minFudge
} else {
return rand.Intn(10-9+1) + 9
if minFudge >= 1 && minFudge <= 10 {
return rand.Intn(11-minFudge) + minFudge
}
return 10
}

//FlipCoin flips a coin and returns outcome
// FlipCoin flips a coin and returns outcome
func FlipCoin(nick string) string {
var Coin = "Tails"
if rand.Intn(2) == 0 {
Expand All @@ -37,22 +36,31 @@ func FlipCoin(nick string) string {
return fmt.Sprintf("```%s flipped a coin and it came up %s```", nick, Coin)
}

//CountSuc counts the number of successes contained in diceReults
// CountSuc counts the number of successes contained in diceReults
func CountSuc(diceResults []int, DC int) int {
var successes = 0
for i := 0; i < len(diceResults); i++ {
if diceResults[i] == 10 {
successes += 2
} else if diceResults[i] >= DC {
successes++
} else if diceResults[i] == 1 && diceResults[i] < DC {
for _, roll := range diceResults {
if roll == 10 {
//DC 10s only give a success if you you roll a ten but, not 2 successes
if DC < 10 {
successes += 2
} else {
successes += 1
}
continue // Skip further checks for this roll
}

if roll >= DC {
successes += 1
}
if roll == 1 {
successes--
}
}
return successes
}

//RerollDice rerolls the 3 lowest dice that are not successes from a result
// RerollDice rerolls the 3 lowest dice that are not successes from a result
func RerollDice(character *data.Character) string {
sort.Ints(character.LastRoll.Rolls)
var failedRolls [3]int
Expand Down Expand Up @@ -82,7 +90,7 @@ func RerollDice(character *data.Character) string {
return toPost
}

//RollDice rolls the dice for a check. DC is expected. Legacy function.
// RollDice rolls the dice for a check. DC is expected. Legacy function.
func RollDice(c string, channel string, session *discordgo.Session, character *data.Character) {
toRoll := strings.Split(c, ",")
if len(toRoll) < 2 {
Expand Down Expand Up @@ -114,15 +122,25 @@ func RollDice(c string, channel string, session *discordgo.Session, character *d
func RollDiceCommand(dicepool int, dc int, reason string, character *data.Character) string {
numDice := dicepool
character.LastRoll.DC = dc
//makes an integer array the size of the number of dice rolled and populates it
character.LastRoll.Rolls = make([]int, numDice)
if character.FudgeRoll > 0 && character.FudgeRoll <= dicepool {

if character.FudgeRoll > 0 {
if character.FudgeRoll > numDice {
character.FudgeRoll = numDice //Can't fudge more than you have dice
}

log.Println("Fudging the roll")
// Fudges the first N rolls
for i := 0; i < character.FudgeRoll; i++ {
character.LastRoll.Rolls[i] = RollDF(dc)
}
// Roll the remaining dice normally
for i := character.FudgeRoll; i < numDice; i++ {
character.LastRoll.Rolls[i] = RollD10()
}
log.Printf("Fudged roll to: [%v]", character.LastRoll.Rolls)
shuffle(character.LastRoll.Rolls)
log.Println("Clearing fudge")
character.FudgeRoll = 0
} else {
for i := 0; i < numDice; i++ {
Expand All @@ -145,6 +163,7 @@ func RollDiceCommand(dicepool int, dc int, reason string, character *data.Charac
toPost = fmt.Sprintf("```%s got a Botch%s\nRolled %v```", character.Name, character.LastRoll.Reason, character.LastRoll.Rolls)
}
} else {
character.LastRoll.Reason = ""
if successes >= 1 {
toPost = fmt.Sprintf("```%s got %d Successes\nRolled %v```", character.Name, successes, character.LastRoll.Rolls)
} else if successes == 0 {
Expand All @@ -156,12 +175,17 @@ func RollDiceCommand(dicepool int, dc int, reason string, character *data.Charac
return toPost
}

//Sets the minimum results for the next roll the invokee makes
// Sets the minimum results for the next roll the invokee makes
func WouldYouKindly(minResults int, character *data.Character) string {
if character.DiscordUser == "Dublin07#9139" {
character.FudgeRoll = minResults
return fmt.Sprintf("Fudge set to %d", minResults)
} else {
return "No, piss off"
character.FudgeRoll = minResults
return fmt.Sprintf("Fudge set to minimum %d successes", minResults)
}

// go passes slices by reference so it should shuffle in place
func shuffle(nums []int) []int {
for i := len(nums) - 1; i > 0; i-- {
j := rand.Intn(i + 1)
nums[i], nums[j] = nums[j], nums[i]
}
return nums
}
17 changes: 13 additions & 4 deletions data/Character.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,24 @@ type Character struct {
}

type attributes struct {
//physical attributes
Stength uint8 `bson:"stength" json:"stength"`
Physical physicalAttributes `bson:"physical" json:"physical"`
Social socialAttributes `bson:"social" json:"social"`
Mental mentalAttributes `bson:"mental" json:"mental"`
}

type physicalAttributes struct {
Strength uint8 `bson:"strength" json:"strength"`
Dexterity uint8 `bson:"dexterity" json:"dexterity"`
Stamina uint8 `bson:"stamina" json:"stamina"`
//social attributes
}

type socialAttributes struct {
Charisma uint8 `bson:"charisma" json:"charisma"`
Manipulation uint8 `bson:"manipulation" json:"manipulation"`
Appearance uint8 `bson:"appearance" json:"appearance"`
//mental atributes
}

type mentalAttributes struct {
Perception uint8 `bson:"perception" json:"perception"`
Intelligence uint8 `bson:"intelligence" json:"intelligence"`
Wits uint8 `bson:"wits" json:"wits"`
Expand Down
30 changes: 13 additions & 17 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
module github.com/pkwiatek6/DiscrodBot

go 1.17
go 1.21

require (
github.com/bwmarrin/discordgo v0.26.1
github.com/bwmarrin/discordgo v0.28.1
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/klauspost/compress v1.15.12 // indirect
github.com/stretchr/testify v1.8.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
go.mongodb.org/mongo-driver v1.11.0
golang.org/x/crypto v0.2.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.2.0 // indirect
golang.org/x/text v0.4.0 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/klauspost/compress v1.17.10 // indirect
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
go.mongodb.org/mongo-driver v1.17.0
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
)

require (
github.com/montanaflynn/stats v0.6.6 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.1 // indirect
github.com/xdg-go/stringprep v1.0.3 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
)
Loading