restructure

This commit is contained in:
lyx0 2022-03-10 23:00:37 +01:00
parent 61946d6402
commit b7daac2619
8 changed files with 99 additions and 102 deletions

View file

@ -1,12 +1,14 @@
build:
cd cmd && go build -o Nourybot
cd cmd/bot && go build -o Nourybot
run:
cd cmd && ./Nourybot
cd cmd/bot && ./Nourybot
dev:
cd cmd && go build -o Nourybot && ./Nourybot -mode dev
cd cmd/bot && go build -o Nourybot && ./Nourybot -mode dev
prod:
cd cmd && go build -o Nourybot && ./Nourybot -mode production
cd cmd/bot && go build -o Nourybot && ./Nourybot -mode production
xd:
cd cmd/bot && go build -o Nourybot && ./Nourybot

17
cmd/bot/bot.go Normal file
View file

@ -0,0 +1,17 @@
package main
func (nb *nourybot) connect() error {
nb.twitchClient.Join("nourybot")
err := nb.twitchClient.Connect()
if err != nil {
panic(err)
}
nb.twitchClient.Say("nourybot", "xd")
return nil
}
func (nb *nourybot) onConnect() {
nb.twitchClient.Say("nourybot", "xd")
}

35
cmd/bot/main.go Normal file
View file

@ -0,0 +1,35 @@
package main
import (
"time"
"github.com/gempir/go-twitch-irc/v3"
"github.com/lyx0/nourybot/internal/config"
"github.com/sirupsen/logrus"
)
type nourybot struct {
twitchClient *twitch.Client
uptime time.Time
}
func main() {
cfg := config.LoadConfig()
logrus.Info(cfg.Username, cfg.Oauth)
twitchClient := twitch.NewClient(cfg.Username, cfg.Oauth)
now := time.Now()
nb := &nourybot{
twitchClient: twitchClient,
uptime: now,
}
nb.twitchClient.OnConnect(nb.onConnect)
err := nb.connect()
if err != nil {
panic(err)
}
}

View file

@ -1,38 +0,0 @@
package main
import (
"time"
"github.com/gempir/go-twitch-irc/v3"
"github.com/lyx0/nourybot/internal/bot"
"github.com/lyx0/nourybot/internal/config"
"github.com/lyx0/nourybot/internal/db"
"github.com/sirupsen/logrus"
)
func main() {
cfg := config.LoadConfig()
logrus.Info(cfg.Username, cfg.Oauth)
twitchClient := twitch.NewClient(cfg.Username, cfg.Oauth)
now := time.Now()
mongoClient := db.Connect(cfg)
nb := &bot.Bot{
TwitchClient: twitchClient,
MongoClient: mongoClient,
Uptime: now,
}
nb.TwitchClient.Join("nourybot")
nb.TwitchClient.OnConnect(func() {
nb.TwitchClient.Say("nourybot", "xd")
})
err := nb.TwitchClient.Connect()
if err != nil {
panic(err)
}
}

View file

@ -1,14 +1 @@
package bot
import (
"time"
twitch "github.com/gempir/go-twitch-irc/v3"
"go.mongodb.org/mongo-driver/mongo"
)
type Bot struct {
TwitchClient *twitch.Client
MongoClient *mongo.Client
Uptime time.Time
}

View file

@ -12,12 +12,12 @@ type Config struct {
Username string
BotUserId string
Oauth string
// Admin
AdminUsername string
AdminUserId string
// DB
MySQLURI string
MongoURI string
}
@ -39,6 +39,7 @@ func LoadConfig() *Config {
AdminUserId: os.Getenv("ADMIN_USER_ID"),
// DB
MySQLURI: os.Getenv("MYSQL_URI"),
MongoURI: os.Getenv("MONGO_URI"),
}

View file

@ -1,46 +0,0 @@
package db
import (
"context"
"time"
"github.com/lyx0/nourybot/internal/config"
log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Connect connects the the MongoDB database through a supplied cfg
// and returns a *mongo.Client
func Connect(cfg *config.Config) *mongo.Client {
client, err := mongo.NewClient(options.Client().ApplyURI(cfg.MongoURI))
if err != nil {
log.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
log.Fatal(err)
}
// defer client.Disconnect(ctx)
err = client.Ping(context.TODO(), nil)
if err != nil {
log.Fatal(err)
}
log.Info("Connected to MongoDB!")
databases, err := client.ListDatabaseNames(ctx, bson.M{})
if err != nil {
log.Fatal(err)
}
_ = databases
// log.Info(databases)
return client
}

39
migrations/nourybot.sql Normal file
View file

@ -0,0 +1,39 @@
CREATE DATABASE nourybot CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE nourybot;
-- create a 'channel' table.
CREATE TABLE channel (
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL,
twitchid VARCHAR(50) NOT NULL,
added DATETIME NOT NULL
);
-- add an index on the created column.
CREATE INDEX idx_channel_created ON channel(added);
-- add dummy records
INSERT INTO channel (username, twitchid, added) VALUES (
'whereismymnd',
'31437432',
UTC_TIMESTAMP()
);
INSERT INTO channel (username, twitchid, added) VALUES (
'nourybot',
'596581605',
UTC_TIMESTAMP()
);
INSERT INTO channel (username, twitchid, added) VALUES (
'xnoury',
'197780373',
UTC_TIMESTAMP()
);
-- Important: Make sure to swap 'pass' and 'username' with a password/user of your own choosing.
CREATE USER 'username'@'localhost';
GRANT SELECT, INSERT ON nourybot.* TO 'username'@'localhost';
ALTER USER 'username'@'localhost' IDENTIFIED BY 'pass';