Added YML config and More

- Discord Bot: Added YML config and moved bot settings from DB to this file
- Error logs names has been renames
- Discord Bot: Added upload from multi attachments
- Update README, Makefile, go.mod and .gitignore
This commit is contained in:
Medzik 2021-07-15 14:33:17 +02:00
parent b30d28ac0e
commit 87c7dc4f8f
15 changed files with 126 additions and 144 deletions

12
.gitignore vendored
View File

@ -12,13 +12,9 @@
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
# Output
*.out
cdn*
# Sums
MD5SUM
SHA256SUM
VERSION
dist/
# Config
config.yml

View File

@ -6,9 +6,6 @@ build:
run:
go run .
deps:
go get -v
clean:
go clean
rm cdn* dist
rm ${OUT_FILE} dist

View File

@ -1,13 +1,13 @@
# Image CDN - API and Discord Bot
![Pipeline Badge](https://gitlab.com/gaming0skar123/go/cdn/badges/main/pipeline.svg)
## Install Pre-Compile binary
* Linux
* Downoad `curl -L https://gitlab.com/gaming0skar123/go/cdn/-/jobs/artifacts/main/raw/cdn-linux-amd64?job=build --output cdn`
* Add permissions `chmod +rwx cdn`
* Run binary `./cdn`
* Linux amd64
* [Download](https://github.com/MedzikUser/go-cdn/releases) latest version
* Unpack file `tar xzf cdn_{VERSION}_linux_amd64.tar.gz`
* Create an .env and config.yml file and complete according to .env.schema and config.schema.yml
* Add permissions `chmod +rwx cdn.out`
* Run binary `./cdn.out`
## Disable Automatic Updates

5
config.schema.yml Normal file
View File

@ -0,0 +1,5 @@
discord:
api_channel: 234567890123456789
channels:
- 123456789012345678
- 987654321098765433

View File

@ -4,5 +4,5 @@ import (
"time"
)
var GH_Repo = "MagicJuszer/go-cdn"
var GH_Repo = "MedzikUser/go-cdn"
var Latest_Version_Check = 2 * time.Minute

View File

@ -1,3 +1,3 @@
package config
var Version = "1.0.0"
var Version = "1.1.0"

36
config/yml.go Normal file
View File

@ -0,0 +1,36 @@
package config
import (
"io/ioutil"
"gitlab.com/gaming0skar123/go/cdn/common"
"gopkg.in/yaml.v2"
)
type YmlConfig struct {
Discord YmlConfigDiscord
}
type YmlConfigDiscord struct {
API_Channel string
Channels []string
}
var API_Channel string
var Channels []string
func init() {
f, err := ioutil.ReadFile("config.yml")
if common.CheckErr(err, "read config file") {
return
}
var data YmlConfig
err = yaml.Unmarshal(f, &data)
if common.CheckErr(err, "unmarshal config file") {
return
}
API_Channel = data.Discord.API_Channel
Channels = data.Discord.Channels
}

View File

@ -1,61 +0,0 @@
package database
import (
"context"
"time"
"gitlab.com/gaming0skar123/go/cdn/common"
"gitlab.com/gaming0skar123/go/cdn/config"
"go.mongodb.org/mongo-driver/bson"
)
type Channel struct {
ID string `bson:"_id"`
GuildID string `bson:"gid"`
}
type ChannelAPI struct {
ID string `bson:"_id"`
}
func InsertChannel(channel *Channel) {
collection := DB.Collection(config.Mongo_Collection_Channel)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, err := collection.InsertOne(ctx, channel)
common.CheckErr(err, "inserting to DB")
}
func InsertAPIChannel(channel *ChannelAPI) {
collection := DB.Collection(config.Mongo_Collection_Channel_API)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, err := collection.InsertOne(ctx, channel)
common.CheckErr(err, "inserting to DB")
}
func FindChannel(channel *Channel) bool {
collection := DB.Collection(config.Mongo_Collection_Channel)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err := collection.FindOne(ctx, channel).Decode(&channel)
return err == nil
}
func FindAPIChannel() (*ChannelAPI, error) {
collection := DB.Collection(config.Mongo_Collection_Channel_API)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
var channel *ChannelAPI
err := collection.FindOne(ctx, bson.D{}).Decode(&channel)
return channel, err
}

View File

@ -7,7 +7,6 @@ import (
"github.com/andersfylling/snowflake/v5"
"gitlab.com/gaming0skar123/go/cdn/common"
"gitlab.com/gaming0skar123/go/cdn/config"
"gitlab.com/gaming0skar123/go/cdn/database"
)
var c *disgord.Client
@ -17,13 +16,8 @@ func apiInit(client *disgord.Client) {
}
func API(link string) {
channel, err := database.FindAPIChannel()
if common.CheckErr(err, "FindAPIChannel()") {
return
}
s, err := snowflake.GetSnowflake(channel.ID)
if common.CheckErr(err, "GetSnowflake("+channel.ID+")") {
s, err := snowflake.GetSnowflake(config.API_Channel)
if common.CheckErr(err, "get snowflake") {
return
}

View File

@ -11,6 +11,6 @@ func deleteMsg(s disgord.Session, m *disgord.Message, t time.Duration) {
go func() {
time.Sleep(t)
err := s.Channel(m.ChannelID).Message(m.ID).Delete()
common.CheckErr(err, "delete message")
common.CheckErr(err, "delete msg")
}()
}

View File

@ -6,69 +6,35 @@ import (
"github.com/andersfylling/disgord"
"gitlab.com/gaming0skar123/go/cdn/common"
"gitlab.com/gaming0skar123/go/cdn/config"
"gitlab.com/gaming0skar123/go/cdn/database"
"gitlab.com/gaming0skar123/go/cdn/imgur"
)
func findChannel(channel string) bool {
for _, c := range config.Channels {
if c == channel {
return true
}
}
return false
}
func handleMsg(s disgord.Session, data *disgord.MessageCreate) {
m := data.Message
findVaue := &database.Channel{
ID: m.ChannelID.String(),
GuildID: m.GuildID.String(),
}
if !database.FindChannel(findVaue) {
if findChannel(m.ChannelID.String()) {
return
}
err := s.Channel(m.ChannelID).TriggerTypingIndicator()
common.CheckErr(err, "TriggerTypingIndicator()")
avatarUrl, _ := m.Author.AvatarURL(64, true)
var url string
common.CheckErr(err, "trigger typing")
if len(m.Attachments) > 0 {
url = m.Attachments[0].URL
} else {
url = m.Content
}
i, err := imgur.UploadFromURL(url)
if common.CheckErr(err, "UploadFromURL("+url+")") {
mBot, err := m.Reply(ctx, s, disgord.Embed{
Title: "Error",
Description: err.Error(),
Timestamp: m.Timestamp,
Color: config.Embed_Color,
Footer: &disgord.EmbedFooter{
Text: m.Author.Tag(),
IconURL: avatarUrl,
},
})
if !common.CheckErr(err, "send message") {
deleteMsg(s, mBot, 5*time.Second)
for _, a := range m.Attachments {
uploadImg(s, m, a.URL)
}
deleteMsg(s, m, 3*time.Second)
return
} else {
uploadImg(s, m, m.Content)
}
_, err = m.Reply(ctx, s, disgord.Embed{
Title: i.Link,
Timestamp: m.Timestamp,
Color: config.Embed_Color,
Footer: &disgord.EmbedFooter{
Text: m.Author.Tag(),
IconURL: avatarUrl,
},
Image: &disgord.EmbedImage{
URL: i.Link,
},
})
common.CheckErr(err, "send message")
deleteMsg(s, m, 2*time.Second)
}

49
discord/uploadImg.go Normal file
View File

@ -0,0 +1,49 @@
package discord
import (
"time"
"github.com/andersfylling/disgord"
"gitlab.com/gaming0skar123/go/cdn/common"
"gitlab.com/gaming0skar123/go/cdn/config"
"gitlab.com/gaming0skar123/go/cdn/imgur"
)
func uploadImg(s disgord.Session, m *disgord.Message, url string) {
avatarUrl, _ := m.Author.AvatarURL(64, true)
i, err := imgur.UploadFromURL(url)
if common.CheckErr(err, "upload img") {
mBot, err := m.Reply(ctx, s, disgord.Embed{
Title: "Error",
Description: err.Error(),
Timestamp: m.Timestamp,
Color: config.Embed_Color,
Footer: &disgord.EmbedFooter{
Text: m.Author.Tag(),
IconURL: avatarUrl,
},
})
if !common.CheckErr(err, "send msg") {
deleteMsg(s, mBot, 5*time.Second)
}
deleteMsg(s, m, 3*time.Second)
return
}
_, err = m.Reply(ctx, s, disgord.Embed{
Title: i.Link,
Timestamp: m.Timestamp,
Color: config.Embed_Color,
Footer: &disgord.EmbedFooter{
Text: m.Author.Tag(),
IconURL: avatarUrl,
},
Image: &disgord.EmbedImage{
URL: i.Link,
},
})
common.CheckErr(err, "send msg")
}

2
go.mod
View File

@ -33,5 +33,5 @@ require (
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v2 v2.4.0
)

View File

@ -15,7 +15,7 @@ func Update() {
repo := config.GH_Repo
latest, found, err := selfupdate.DetectLatest(repo)
if common.CheckErr(err, "detecting letest version") {
if common.CheckErr(err, "detect latest version") {
return
}

View File

@ -23,13 +23,13 @@ func Upload(c *gin.Context) {
}
file, err := c.FormFile("file")
if common.CheckErr(err, "error form file") {
if common.CheckErr(err, "form file") {
c.String(http.StatusBadRequest, "Error Form File")
return
}
f, err := file.Open()
if common.CheckErr(err, "error opening file") {
if common.CheckErr(err, "open file") {
c.String(http.StatusBadRequest, "Error Opening File")
return
}