start new

This commit is contained in:
lyx0 2022-04-15 23:11:42 +02:00
parent ddca49605c
commit a2a99b81eb
8 changed files with 0 additions and 217 deletions

View file

@ -1,26 +0,0 @@
package main
import (
"github.com/gempir/go-twitch-irc/v3"
"github.com/sirupsen/logrus"
)
func (nb *nourybot) connect() error {
nb.twitchClient.Join("nourybot")
logrus.Info("Connecting to Twitch...")
err := nb.twitchClient.Connect()
if err != nil {
panic(err)
}
return nil
}
func (nb *nourybot) onConnect() {
nb.twitchClient.Say("nourybot", "xd")
}
func (nb *nourybot) onPrivateMessage(message twitch.PrivateMessage) {
logrus.Info(message.Message)
}

View file

@ -1 +0,0 @@
package main

View file

@ -1,63 +0,0 @@
package main
import (
"database/sql"
"log"
"time"
"github.com/gempir/go-twitch-irc/v3"
"github.com/lyx0/nourybot/internal/config"
"github.com/lyx0/nourybot/pkg/models/mysql"
"github.com/sirupsen/logrus"
_ "github.com/go-sql-driver/mysql"
)
type nourybot struct {
twitchClient *twitch.Client
uptime time.Time
channels *mysql.ChannelModel
}
func main() {
cfg := config.LoadConfig()
logrus.Info(cfg.Username, cfg.Oauth)
twitchClient := twitch.NewClient(cfg.Username, cfg.Oauth)
now := time.Now()
db, err := openDB(cfg.MySQLURI)
if err != nil {
log.Fatal(err)
}
defer db.Close()
nb := &nourybot{
twitchClient: twitchClient,
uptime: now,
channels: &mysql.ChannelModel{DB: db},
}
nb.twitchClient.OnPrivateMessage(func(message twitch.PrivateMessage) {
nb.onPrivateMessage(message)
})
nb.twitchClient.OnConnect(nb.onConnect)
err = nb.connect()
if err != nil {
panic(err)
}
}
func openDB(dsn string) (*sql.DB, error) {
db, err := sql.Open("mysql", dsn)
if err != nil {
return nil, err
}
if err = db.Ping(); err != nil {
return nil, err
}
return db, nil
}

View file

@ -1 +0,0 @@
package bot

View file

@ -1,49 +0,0 @@
package config
import (
"os"
"github.com/joho/godotenv"
log "github.com/sirupsen/logrus"
)
type Config struct {
// Bot
Username string
BotUserId string
Oauth string
// Admin
AdminUsername string
AdminUserId string
// DB
MySQLURI string
MongoURI string
}
func LoadConfig() *Config {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env")
}
cfg := &Config{
// Bot
Username: os.Getenv("BOT_USERNAME"),
Oauth: os.Getenv("BOT_PASS"),
BotUserId: os.Getenv("BOT_USER_ID"),
// Admin
AdminUsername: os.Getenv("ADMIN_USER_NAME"),
AdminUserId: os.Getenv("ADMIN_USER_ID"),
// DB
MySQLURI: os.Getenv("MYSQL_URI"),
MongoURI: os.Getenv("MONGO_URI"),
}
log.Info("Config loaded succesfully")
return cfg
}

View file

@ -1,39 +0,0 @@
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';

View file

@ -1,15 +0,0 @@
package models
import (
"errors"
"time"
)
var ErrNoRecord = errors.New("models: no matching record found")
type Channel struct {
ID int
Username string
TwitchID string
Added time.Time
}

View file

@ -1,23 +0,0 @@
package mysql
import (
"database/sql"
"github.com/lyx0/nourybot/pkg/models"
)
type ChannelModel struct {
DB *sql.DB
}
func (m *ChannelModel) Insert(username string, twitchid string) (int, error) {
return 0, nil
}
func (m *ChannelModel) Get(id int) (*models.Channel, error) {
return nil, nil
}
func (m *ChannelModel) Latest() ([]*models.Channel, error) {
return nil, nil
}