Skip to content

Commit

Permalink
Merge pull request #10 from Nota30/dev
Browse files Browse the repository at this point in the history
Database update and handling errors
  • Loading branch information
Nota30 committed Jul 3, 2023
2 parents f3dea20 + 0e2f49d commit 05537b1
Show file tree
Hide file tree
Showing 28 changed files with 564 additions and 265 deletions.
3 changes: 1 addition & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ env="development"
DISCORD_TOKEN="Bot TOKEN HERE" # Make sure you dont remove the "Bot" at the start. The token should go after it.
DB_URL="host=localhost user=kiko password=1234 dbname=kiko port=5432"
LOG_CHANNEL="channel id"
STATUS_CHANNEL="channel id"
OpenAIApiKey="api key"
STATUS_CHANNEL="channel id"
9 changes: 9 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# https://taskfile.dev

version: '3'

tasks:
run:
cmds:
- go run cmd/kiko/main.go
silent: true
4 changes: 2 additions & 2 deletions bot/events/components.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package events

import (
modules "github.com/Nota30/Kiko/modules/Information"
information "github.com/Nota30/Kiko/modules/Information"
"github.com/bwmarrin/discordgo"
)

Expand All @@ -18,7 +18,7 @@ func ComponentHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
if i.MessageComponentData().ComponentType == 3 {
switch i.MessageComponentData().CustomID {
case "select_class":
modules.RegisterSelector(s, i)
information.RegisterSelector(s, i)
}
}
}
12 changes: 6 additions & 6 deletions bot/events/interaction.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package events

import (
info "github.com/Nota30/Kiko/modules/Information"
information "github.com/Nota30/Kiko/modules/Information"
tower "github.com/Nota30/Kiko/modules/Tower"
"github.com/bwmarrin/discordgo"
)
Expand All @@ -17,14 +17,14 @@ func InteractionHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {

switch i.ApplicationCommandData().Name {
case "ping":
info.Ping(s, i)
information.PingCMD(s, i)
case "register":
info.Register(s, i)
information.RegisterCMD(s, i)
case "profile":
info.Profile(s, i)
information.ProfileCMD(s, i)
case "move":
tower.Move(s, i)
tower.MoveCMD(s, i)
case "explore":
tower.Explore(s, i)
tower.ExploreCMD(s, i)
}
}
3 changes: 3 additions & 0 deletions bot/events/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"

"github.com/bwmarrin/discordgo"
"github.com/sirupsen/logrus"
)

func MSGCreateHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
Expand Down Expand Up @@ -36,6 +37,8 @@ func MSGCreateHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
if !exists {
return
}

logrus.Info(exists)
}

func getCommand(str string, n int) string {
Expand Down
8 changes: 8 additions & 0 deletions cache/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/redis/go-redis/v9"
"github.com/sirupsen/logrus"
)

var (
Expand All @@ -17,4 +18,11 @@ func Connect() {
Password: "",
DB: 0,
})

pong, err := Client.Ping(Ctx).Result()
if err != nil {
logrus.Fatal(err)
} else {
logrus.Info("Connected to Redis ", pong)
}
}
13 changes: 9 additions & 4 deletions cmd/kiko/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,27 @@ import (

"github.com/Nota30/Kiko/bot"
"github.com/Nota30/Kiko/cache"
database "github.com/Nota30/Kiko/db"
"github.com/Nota30/Kiko/database"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
)

func main() {
err := godotenv.Load(".env")

if err != nil {
logrus.Fatalf("Error loading .env file")
logrus.Fatal("Error loading .env file")
} else {
logrus.Info("Loaded the environment variables!!")
}

cache.Connect()
database.Connect()
dbUrl := os.Getenv("DB_URL")
if len(dbUrl) == 0 {
logrus.Fatal("Database connection string was not found")
}

database.ConnDB(dbUrl)

bot.DiscordConnect()

sc := make(chan os.Signal, 1)
Expand Down
19 changes: 19 additions & 0 deletions config/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ var Commands = []*discordgo.ApplicationCommand{
Description: "Explore the floor you are on",
DMPermission: &dmPermission,
},
{
Name: "inventory",
Description: "View the items you have",
DMPermission: &dmPermission,
},
{
Name: "move",
Description: "Move to a different floor in the tower",
Expand All @@ -42,4 +47,18 @@ var Commands = []*discordgo.ApplicationCommand{
},
},
},
{
Name: "battle",
Description: "Start a battle using a battle card",
DMPermission: &dmPermission,
Options: []*discordgo.ApplicationCommandOption{
{
Name: "id",
Type: discordgo.ApplicationCommandOptionInteger,
MinValue: &towerMinFloor,
Description: "The id of the battle card (You can find the id using the /inventory command)",
Required: true,
},
},
},
}
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (

type color struct {
Default int
Blue int
Error int
}

var ApplicationCommands []*discordgo.ApplicationCommand
var RegisterCommand string
var Color color = color{Default: 0xf62dcd, Error: 0xf32917}
var Color color = color{Default: 0xf62dcd, Error: 0xf32917, Blue: 0x17caf3}
var DevGuild = "983931249456455720"
var Classes = store.Classes
var Weapons = store.Weapons
39 changes: 39 additions & 0 deletions database/conn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package database

import (
"github.com/Nota30/Kiko/models"
"github.com/sirupsen/logrus"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)

var (
Db *gorm.DB
err error
)

func ConnDB(dbConURL string) {
Db, err = gorm.Open(postgres.Open(dbConURL))
if err != nil {
logrus.Fatal(err)
}

psql, err := Db.DB()
if err != nil {
logrus.Fatal(err)
}
// pong the database
err = psql.Ping()
if err != nil {
logrus.Fatal(err)
}
// migration
err = Db.AutoMigrate(&models.TUser{}, &models.TFloor{}, &models.TInventory{})
if err != nil {
logrus.Fatal(err)
}

models.User = models.NewUserModel(Db)
models.Floor = models.NewFloorModel(Db)
models.Inventory = models.NewInventoryModel(Db)
}
2 changes: 1 addition & 1 deletion db/tables.sql → database/tables.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CREATE TABLE IF NOT EXISTS USERS (
id BIGSERIAL PRIMARY KEY,
discord_id VARCHAR NOT NULL,
coins INTEGER NOT NULL DEFAULT 0 CHECK(coins >= 0),
coins INTEGER NOT NULL DEFAULT 0,
level INTEGER NOT NULL DEFAULT 1,
xp INTEGER NOT NULL DEFAULT 0,
kills INTEGER NOT NULL DEFAULT 0,
Expand Down
39 changes: 0 additions & 39 deletions db/database.go

This file was deleted.

44 changes: 0 additions & 44 deletions db/models.go

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/Nota30/Kiko

go 1.19
go 1.20

require (
github.com/bwmarrin/discordgo v0.27.1
Expand Down
5 changes: 5 additions & 0 deletions lib/api/classes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package api

func FetchAllClasses() {}

func FetchClass() {}
Loading

0 comments on commit 05537b1

Please sign in to comment.