Skip to content

Commit

Permalink
Merge branch 'release/0.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
matamegger committed May 28, 2017
2 parents db3d65f + 1a8686c commit a8867a8
Show file tree
Hide file tree
Showing 9 changed files with 930 additions and 250 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*.so

# Folders
.idea
download
settings
sounds
Expand Down
827 changes: 668 additions & 159 deletions LICENSE

Large diffs are not rendered by default.

25 changes: 20 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
# discordBot

It's yet not sure in which direction this project will go. Appreciate any constructive input.

## Dependencies

- [discordgo](https://github.com/bwmarrin/discordgo)
- [dca](https://github.com/bwmarrin/dca)
- ffmpeg
### Go
- [discordgo](https://github.com/bwmarrin/discordgo) v0.16.0
- [go-logging](https://github.com/op/go-logging) as by Mar 15, 2016
- [dca](https://github.com/bwmarrin/dca) as by Jan 3, 2017

### System
- [ffmpeg](https://ffmpeg.org/) / opus

[dca](https://github.com/bwmarrin/dca) (and its dependencies ffmpeg/opus) are used to dynamically encode and add new sounds to the "soundboard" function.

## Installing

Expand All @@ -14,16 +22,23 @@ This assumes you already have a working Go environment, if not please see
`go get` *will always pull the latest released version from the master branch.*

```sh
# Installing the bot
go get github.com/matamegger/discordBot
go install github.com/matamegger/discordBot
```

## Usage
[discordgo](https://github.com/bwmarrin/discordgo) and [go-logging](https://github.com/op/go-logging) should be installed automatically.
However, you have to install [dca](https://github.com/bwmarrin/dca) and its dependencies manually.

## Starting/Configurating

Assuming you have added your go/bin folder to your path and used go install for discordBot.

```sh
discordBot -t DiscordBot.Token -o discordIdOfTheOwner
```
To get the DiscordBot Token go to [this page](https://discordapp.com/developers/applications/me).
To get your id type \@yourname in a server channel, but use only the number (remove the <@>)
<br>To get your ID type \\@yourname in a server channel, but use only the number (remove the <@>).<sup>*1.</sup>

## Footnotes
1. You could also start the bot, without an owner ID, and ask it with `!get id` for your ID.
8 changes: 3 additions & 5 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/bwmarrin/discordgo"
"path/filepath"
)

func processCommand(s *discordgo.Session, m *discordgo.Message) {
Expand All @@ -18,17 +19,14 @@ func processCommand(s *discordgo.Session, m *discordgo.Message) {

//TODO
col := data.Soundcollections[parts[0]]
fmt.Println("part0", parts[0])
if col != nil {
fmt.Println("notnull")
var sound *Sound
if len(parts) > 1 {
sound = col.GetSound(parts[1])
}
if sound == nil {
sound = col.GetRandomSound()
}
fmt.Println("soundname", sound.Name)
channel, _ := s.State.Channel(m.ChannelID)
guild, err := s.State.Guild(channel.GuildID)
if guild == nil || err != nil {
Expand Down Expand Up @@ -163,7 +161,6 @@ func WhoIsOwner(s *discordgo.Session, m *discordgo.Message, parts []string) {
func Kill(s *discordgo.Session, m *discordgo.Message, parts []string) {
if m.Author.ID == OWNER {
s.ChannelMessageSend(m.ChannelID, "Yes, Sir!\nI will kill myself.")
saveSettings()
exit <- true
}
}
Expand Down Expand Up @@ -192,7 +189,8 @@ func AddSound(s *discordgo.Session, m *discordgo.Message, parts []string) {
}
if len(m.Attachments) > 0 {
if len(parts) > 2 {
file, err := GetSoundByURL(m.Attachments[0].URL, parts[1], parts[2])
path := filepath.Join(BASEPATH, RELATIVE_SOUNDS_PATH)
file, err := GetSoundByURL(m.Attachments[0].URL, path, parts[1]+"-"+parts[2])
if err != nil {
s.ChannelMessageSend(m.ChannelID, "Sorry, I got rekt up")
} else {
Expand Down
10 changes: 5 additions & 5 deletions discordHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func getCurrentVoiceChannel(s *discordgo.Session, user *discordgo.User, guild *d
func changeAvatar(s *discordgo.Session, localFile string) (err error) {
img, err := ioutil.ReadFile(localFile)
if err != nil {
fmt.Println(err)
log.Errorf("Error reading file > %s", err)
return err
}

Expand All @@ -33,7 +33,7 @@ func changeAvatar(s *discordgo.Session, localFile string) (err error) {
fmt.Println(s.State.User.Avatar)
u, err := s.UserUpdate("", "", BOT_NAME, avatar, "")
if err != nil {
fmt.Println(err)
log.Errorf("Error updating user avatar > %s", err)
} else {
if u != nil {
s.State.User = u
Expand All @@ -47,13 +47,13 @@ func changeAvatarByURL(s *discordgo.Session, url string) (err error) {
resp, err := http.Get(url)
defer resp.Body.Close()
if err != nil {
fmt.Println("Error retrieving the file, ", err)
log.Errorf("Error retrieving the http file > %s", err)
return
}

img, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading the response, ", err)
log.Errorf("Error reading the http response > %s", err)
return
}

Expand All @@ -63,7 +63,7 @@ func changeAvatarByURL(s *discordgo.Session, url string) (err error) {

s.State.User, err = s.UserUpdate("", "", BOT_NAME, avatar, "")
if err != nil {
fmt.Println("Error setting the avatar, ", err)
log.Errorf("Error updating user avatar > %s", err)
}
return
}
Expand Down
127 changes: 112 additions & 15 deletions fileHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,103 @@ package main

import (
"bytes"
"fmt"
"encoding/json"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
)

const (
OS_READ = 04
OS_WRITE = 02
OS_EX = 01
OS_USER_SHIFT = 6
OS_GROUP_SHIFT = 3
OS_OTH_SHIFT = 0

OS_USER_R = OS_READ << OS_USER_SHIFT
OS_USER_W = OS_WRITE << OS_USER_SHIFT
OS_USER_X = OS_EX << OS_USER_SHIFT
OS_USER_RW = OS_USER_R | OS_USER_W
OS_USER_RWX = OS_USER_RW | OS_USER_X

OS_GROUP_R = OS_READ << OS_GROUP_SHIFT
OS_GROUP_W = OS_WRITE << OS_GROUP_SHIFT
OS_GROUP_X = OS_EX << OS_GROUP_SHIFT
OS_GROUP_RW = OS_GROUP_R | OS_GROUP_W
OS_GROUP_RWX = OS_GROUP_RW | OS_GROUP_X

OS_OTH_R = OS_READ << OS_OTH_SHIFT
OS_OTH_W = OS_WRITE << OS_OTH_SHIFT
OS_OTH_X = OS_EX << OS_OTH_SHIFT
OS_OTH_RW = OS_OTH_R | OS_OTH_W
OS_OTH_RWX = OS_OTH_RW | OS_OTH_X

OS_ALL_R = OS_USER_R | OS_GROUP_R | OS_OTH_R
OS_ALL_W = OS_USER_W | OS_GROUP_W | OS_OTH_W
OS_ALL_X = OS_USER_X | OS_GROUP_X | OS_OTH_X
OS_ALL_RW = OS_ALL_R | OS_ALL_W
OS_ALL_RWX = OS_ALL_RW | OS_GROUP_X
)

func LoadObjectFromJsonFile(path string, object interface{}) (err error) {
file, err := os.Open(path)
defer file.Close()
if err != nil {
log.Errorf("Error opening file: &s > %s", path, err)
return
}
err = json.NewDecoder(file).Decode(object)
if err != nil {
log.Errorf("Error decoding json file stream: %s > %s", path, err)
return
}
return
}

func SaveObjectAsJsonToFile(path string, object interface{}) (err error) {
_, err = isFile(path)
if err != nil {
if os.IsExist(err) {
log.Errorf("Error accessing the file: %s > %s", path, err)
return
}
dirPath := filepath.Dir(path)
os.MkdirAll(dirPath, os.FileMode(OS_ALL_R|OS_USER_RW))
}
file, err := os.Create(path)
defer file.Close()
if err != nil {
log.Errorf("Error creating/truncating file: %s > %s", path, err)
return
}
err = json.NewEncoder(file).Encode(object)
if err != nil {
log.Errorf("Error encoding json to file stream: %s > %s", path, err)
return
}
return

}

func isFile(path string) (bool, error) {
fi, err := os.Stat(path)
if err != nil {
return false, err
}
return fi.Mode().IsRegular(), nil
}

func isDirectory(path string) (bool, error) {
fi, err := os.Stat(path)
if err != nil {
return false, err
}
return fi.Mode().IsDir(), nil
}

//returns whether the given file or directory exists or not
func exists(path string) (bool, error) {
_, err := os.Stat(path)
Expand All @@ -22,23 +111,29 @@ func exists(path string) (bool, error) {
return true, err
}

func GetSoundByURL(url string, collection string, name string) (file string, err error) {
ext := filepath.Ext(url)
func GetSoundByURL(url string, path string, fileName string) (file string, err error) {
//TODO check if it is a sound file (extention)
err, download := DownloadFile(url, collection+"-"+name, ext)
download, err := DownloadFile(url, path, fileName)
if err != nil {
fmt.Println("error", err)
log.Errorf("Error downloading file: %s", url)
}
file = BASEPATH + "sounds/" + collection + "-" + name + ".dca"
file = filepath.Join(path, fileName+".dca")
ext := filepath.Ext(download)
if ext != ".dca" {
err = convertToDCA(download, file)
os.Remove(download)
if err != nil {
log.Errorf("Error converting sound to .dca > %s", err)
return
}
err = os.Remove(download)
if err != nil {
log.Noticef("Couldn't remove file: %s", download)
}

} else {
err = os.Rename(download, file)
if err != nil {
log.Errorf("Error renaming file: %s", download)
return
}
}
Expand All @@ -47,10 +142,12 @@ func GetSoundByURL(url string, collection string, name string) (file string, err
}

func convertToDCA(inputFile string, outputFile string) (err error) {
dirPath := filepath.Dir(outputFile)
os.MkdirAll(dirPath, os.FileMode(OS_ALL_R|OS_USER_RW))
output, err := os.Create(outputFile)
defer output.Close()
if err != nil {
fmt.Println("Error creating file", err)
log.Errorf("Error creating file > %s", err)
return
}

Expand All @@ -59,27 +156,27 @@ func convertToDCA(inputFile string, outputFile string) (err error) {
dca.Stdout = &out
dca.Start()
if err != nil {
fmt.Println("StartDCA Error:", err)
log.Errorf("Error starting dca > ", err)
return
}

err = dca.Wait()
if err != nil {
fmt.Println("DCA Error:", err)
log.Errorf("Error from dca > ", err)
return
}
_, err = output.Write(out.Bytes())
if err != nil {
fmt.Println("Error writing", err)
log.Errorf("Error writing to file: %s > %s", outputFile, err)
}
return
}

func DownloadFile(url string, name string, ext string) (err error, file string) {
func DownloadFile(url string, dirPath string, name string) (file string, err error) {
ext := filepath.Ext(url)
os.MkdirAll(dirPath, os.FileMode(OS_ALL_R|OS_USER_RW))
file = filepath.Join(dirPath, name+ext)
//ddMMyyhhmmssff
file = BASEPATH + "download/"
os.MkdirAll(file, os.FileMode(int(0777)))
file += name + ext
//time.Now().Format("020106150405.00") + ext
out, err := os.Create(file)
defer out.Close()
Expand Down
Loading

0 comments on commit a8867a8

Please sign in to comment.