Skip to content

Commit

Permalink
Merge pull request #11 from Tech13-08/main
Browse files Browse the repository at this point in the history
Enemy spawn code
  • Loading branch information
Nota30 committed Jul 8, 2023
2 parents 6c03402 + f24e120 commit 2c00df1
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 3 deletions.
7 changes: 4 additions & 3 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ func DiscordConnect() {
logrus.Error("There was an error creating the discord session,", err)
return
}

Dg.AddHandler(events.MSGCreateHandler)
Dg.AddHandler(events.InteractionHandler)
Dg.AddHandler(events.ComponentHandler)
Dg.AddHandler(events.ActivityHandler)

Dg.Identify.Intents |= discordgo.IntentMessageContent
Dg.Identify.Intents |= discordgo.IntentsGuildMessages
Expand All @@ -51,7 +52,7 @@ func DiscordConnect() {
config.RegisterCommand = "</register:" + val.ID + ">"
}
}

env := os.Getenv("env")
if env == "development" {
logrus.Info("Registering Slash Commands in dev mode...")
Expand All @@ -60,4 +61,4 @@ func DiscordConnect() {
logrus.Info("Registering Slash Commands in prod mode...")
tools.RegisterCommands(Dg, "")
}
}
}
88 changes: 88 additions & 0 deletions bot/events/activity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package events

import (
"fmt"
"time"

"github.com/Nota30/Kiko/config"
"github.com/bwmarrin/discordgo"
"github.com/sirupsen/logrus"
)

type ChannelActivity struct {
MessageCount int
LastUpdate time.Time
LastAuthor string
LastSpawn time.Time
}

// Mess with the variables and constants to adjust intervals of spawning and activity thresholds
var (
channelActivityMap = make(map[string]*ChannelActivity)
spawnCooldown = 1 * time.Minute
)

const (
activityThreshold = 3
activityTimeWindow = 2 * time.Minute
)

func ActivityHandler(session *discordgo.Session, message *discordgo.MessageCreate) {
if message.Author.Bot || message.Author.ID == session.State.User.ID {
return
}

channelID := message.ChannelID
activity, exists := channelActivityMap[channelID]

if !exists {
fmt.Printf("Created channel activity map!!!\n") // Debug line
activity = &ChannelActivity{
MessageCount: 1,
LastUpdate: time.Now(),
LastAuthor: message.Author.ID,
LastSpawn: time.Now(),
}
channelActivityMap[channelID] = activity
return
}

if time.Since(activity.LastUpdate) > activityTimeWindow {
fmt.Printf("Time exceeded, reset activity\n") // Debug line
activity.MessageCount = 1
activity.LastUpdate = time.Now()
activity.LastAuthor = message.Author.ID
return
}

// Comment this if statement if you want to test the system alone (otherwise it'll think you are spamming and won't work)
//if session.State.User.ID != activity.LastAuthor {
activity.MessageCount++
activity.LastUpdate = time.Now()
activity.LastAuthor = message.Author.ID
//}

// Debug lines
fmt.Printf("Message count: " + fmt.Sprint(activity.MessageCount) + "\n")
fmt.Printf("Last Update: " + fmt.Sprint(activity.LastUpdate) + "\n")
fmt.Printf("Last Author: " + fmt.Sprint(activity.LastAuthor) + "\n")
fmt.Printf("Last Spawn: " + fmt.Sprint(activity.LastSpawn) + "\n")
// Debug lines

if activity.MessageCount >= activityThreshold {
fmt.Printf("Activity threshold reached!" + "\n") // Debug line
if time.Since(activity.LastSpawn) > spawnCooldown {
fmt.Printf("Spawning enemy..." + "\n") // Debug line
enemy := config.GetRandomEnemy()
if enemy != nil {
activity.LastSpawn = time.Now()
enemyEmbed := config.GetEnemyEmbed(enemy)
_, err := session.ChannelMessageSendEmbed(channelID, enemyEmbed)
if err != nil {
logrus.Error("Error with sending enemy embed")
return
}
}
}
}
}
59 changes: 59 additions & 0 deletions config/enemy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package config

import (
"fmt"
"math/rand"
"time"

"github.com/bwmarrin/discordgo"
)

const (
min = 1
max = 10
)

type EnemyStruct struct {
Name string
Strength int
Mana int
Health int
Defence int
}

var hollow = EnemyStruct{
Name: "Hollow",
Strength: 100,
Mana: 500,
Health: 1500,
Defence: 300,
}

var angel = EnemyStruct{
Name: "Angel",
Strength: 300,
Mana: 800,
Health: 2000,
Defence: 500,
}

var enemies = []EnemyStruct{hollow, angel}

func GetRandomEnemy() *EnemyStruct {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
spawnChance := (r.Uint32()%(max-min) + min)
// 1 out of 10 chance for enemy to spawn
if spawnChance == 5 {
return &enemies[rand.Intn(len(enemies))]
}
return nil

}

func GetEnemyEmbed(enemy *EnemyStruct) *discordgo.MessageEmbed {
return &discordgo.MessageEmbed{
Title: "Enemy Spawned!",
Color: Color.Default,
Description: "**" + enemy.Name + "**:\n\tStength:" + fmt.Sprint(enemy.Strength) + "\n\tHealth:" + fmt.Sprint(enemy.Health) + "\n\tMana:" + fmt.Sprint(enemy.Mana) + "\n\tDefence:" + fmt.Sprint(enemy.Defence),
}
}

0 comments on commit 2c00df1

Please sign in to comment.