mirror of
https://github.com/mgerb/go-discord-bot
synced 2026-01-09 08:32:48 +00:00
play sounds from web ui - store uploaded sounds in database
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -15,3 +15,5 @@ tmp
|
||||
|
||||
/data
|
||||
/data.db
|
||||
|
||||
/logrus.log
|
||||
|
||||
@@ -4,6 +4,8 @@ import './sound-list.scss';
|
||||
interface Props {
|
||||
soundList: SoundType[];
|
||||
type: string;
|
||||
onPlayDiscord?: (sound: SoundType) => void;
|
||||
showDiscordPlay?: boolean;
|
||||
}
|
||||
|
||||
interface State {
|
||||
@@ -37,17 +39,14 @@ export class SoundList extends React.Component<Props, State> {
|
||||
}
|
||||
}
|
||||
|
||||
handleShowAudio(index: any) {
|
||||
let temp = this.state.showAudioControls;
|
||||
temp[index] = true;
|
||||
|
||||
this.setState({
|
||||
showAudioControls: temp,
|
||||
});
|
||||
handlePlayAudioInBrowser(sound: SoundType, type: string) {
|
||||
const url = `/public/${type.toLowerCase()}/` + sound.name + '.' + sound.extension;
|
||||
const audio = new Audio(url);
|
||||
audio.play();
|
||||
}
|
||||
|
||||
render() {
|
||||
const { soundList, type } = this.props;
|
||||
const { onPlayDiscord, showDiscordPlay, soundList, type } = this.props;
|
||||
|
||||
return (
|
||||
<div className="card">
|
||||
@@ -74,7 +73,32 @@ export class SoundList extends React.Component<Props, State> {
|
||||
style={{ width: '100px' }}
|
||||
/>
|
||||
) : (
|
||||
<i className="fa fa-play link" aria-hidden="true" onClick={() => this.handleShowAudio(index)} />
|
||||
<div>
|
||||
<a
|
||||
href={`/public/${type.toLowerCase()}/` + sound.name + '.' + sound.extension}
|
||||
download
|
||||
title="Download"
|
||||
className="fa fa-download link"
|
||||
aria-hidden="true"
|
||||
/>
|
||||
<i
|
||||
title="Play in browser"
|
||||
className="fa fa-play link"
|
||||
aria-hidden="true"
|
||||
style={{ paddingLeft: '15px' }}
|
||||
onClick={() => this.handlePlayAudioInBrowser(sound, type)}
|
||||
/>
|
||||
{showDiscordPlay &&
|
||||
onPlayDiscord && (
|
||||
<i
|
||||
title="Play in discord"
|
||||
className="fa fa-play-circle link fa-lg"
|
||||
aria-hidden="true"
|
||||
style={{ paddingLeft: '10px' }}
|
||||
onClick={() => onPlayDiscord!(sound)}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -46,7 +46,7 @@ export class Uploader extends React.Component<IProps, IState> {
|
||||
formData.append('file', file);
|
||||
|
||||
axios
|
||||
.post('/api/upload', formData, this.config)
|
||||
.post('/api/sound', formData, this.config)
|
||||
.then(() => {
|
||||
this.setState({
|
||||
percentCompleted: 0,
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
import { inject, observer } from 'mobx-react';
|
||||
import React from 'react';
|
||||
import { SoundList, SoundType, Uploader } from '../../components';
|
||||
import { axios } from '../../services';
|
||||
import { Permissions } from '../../model';
|
||||
import { axios, SoundService } from '../../services';
|
||||
import { AppStore } from '../../stores';
|
||||
import './soundboard.scss';
|
||||
|
||||
interface Props {}
|
||||
interface Props {
|
||||
appStore?: AppStore;
|
||||
}
|
||||
|
||||
interface State {
|
||||
percentCompleted: number;
|
||||
@@ -12,6 +17,8 @@ interface State {
|
||||
soundList: SoundType[];
|
||||
}
|
||||
|
||||
@inject('appStore')
|
||||
@observer
|
||||
export class Soundboard extends React.Component<Props, State> {
|
||||
private soundListCache: any;
|
||||
|
||||
@@ -54,12 +61,22 @@ export class Soundboard extends React.Component<Props, State> {
|
||||
this.getSoundList();
|
||||
};
|
||||
|
||||
onPlayDiscord = (sound: SoundType) => {
|
||||
SoundService.playSound(sound);
|
||||
};
|
||||
|
||||
render() {
|
||||
const { soundList } = this.state;
|
||||
const { appStore } = this.props;
|
||||
return (
|
||||
<div className="content">
|
||||
<Uploader onComplete={this.onUploadComplete} />
|
||||
<SoundList soundList={soundList} type="Sounds" />
|
||||
<SoundList
|
||||
soundList={soundList}
|
||||
type="Sounds"
|
||||
onPlayDiscord={this.onPlayDiscord}
|
||||
showDiscordPlay={appStore!.claims && appStore!.claims!.permissions >= Permissions.Mod}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from './axios.service';
|
||||
export * from './oauth.service';
|
||||
export * from './sound.service';
|
||||
export * from './storage.service';
|
||||
|
||||
10
client/app/services/sound.service.ts
Normal file
10
client/app/services/sound.service.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { SoundType } from '../components/sound-list/sound-list';
|
||||
import { axios } from './axios.service';
|
||||
|
||||
const playSound = (sound: SoundType): Promise<any> => {
|
||||
return axios.post('/api/sound/play', { name: sound.name });
|
||||
};
|
||||
|
||||
export const SoundService = {
|
||||
playSound,
|
||||
};
|
||||
@@ -4,7 +4,8 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/mgerb/go-discord-bot/server/logger"
|
||||
"github.com/mgerb/go-discord-bot/server/db"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/model"
|
||||
)
|
||||
|
||||
// LoggerHandler -
|
||||
@@ -12,14 +13,14 @@ func LoggerHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
|
||||
// upsert user
|
||||
user := getUser(m.Author)
|
||||
user.Save()
|
||||
model.UserSave(db.GetConn(), user)
|
||||
|
||||
// create and save message
|
||||
timestamp, _ := m.Message.Timestamp.Parse()
|
||||
editedTimestamp, _ := m.Message.EditedTimestamp.Parse()
|
||||
attachments := getAttachments(m.Message.Attachments)
|
||||
|
||||
message := &logger.Message{
|
||||
message := &model.Message{
|
||||
ID: m.Message.ID,
|
||||
ChannelID: m.Message.ChannelID,
|
||||
Content: m.Message.Content,
|
||||
@@ -32,13 +33,13 @@ func LoggerHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
Attachments: attachments,
|
||||
}
|
||||
|
||||
message.Save()
|
||||
model.MessageSave(db.GetConn(), message)
|
||||
}
|
||||
|
||||
func getAttachments(att []*discordgo.MessageAttachment) []logger.Attachment {
|
||||
var attachments []logger.Attachment
|
||||
func getAttachments(att []*discordgo.MessageAttachment) []model.Attachment {
|
||||
var attachments []model.Attachment
|
||||
for _, a := range att {
|
||||
newAttachment := logger.Attachment{
|
||||
newAttachment := model.Attachment{
|
||||
MessageID: a.ID,
|
||||
Filename: a.Filename,
|
||||
Height: a.Height,
|
||||
@@ -52,8 +53,8 @@ func getAttachments(att []*discordgo.MessageAttachment) []logger.Attachment {
|
||||
return attachments
|
||||
}
|
||||
|
||||
func getUser(u *discordgo.User) *logger.User {
|
||||
return &logger.User{
|
||||
func getUser(u *discordgo.User) *model.User {
|
||||
return &model.User{
|
||||
ID: u.ID,
|
||||
Email: u.Email,
|
||||
Username: u.Username,
|
||||
|
||||
@@ -30,8 +30,9 @@ const (
|
||||
voiceClipQueuePacketSize int = 2000 // this packet size equates to roughly 40 seconds of audio
|
||||
)
|
||||
|
||||
// ActiveConnections - current active bot connections
|
||||
// store our connection objects in a map tied to a guild id
|
||||
var activeConnections = make(map[string]*AudioConnection)
|
||||
var ActiveConnections = make(map[string]*AudioConnection)
|
||||
var speakers = make(map[uint32]*gopus.Decoder)
|
||||
|
||||
// AudioConnection -
|
||||
@@ -66,7 +67,7 @@ func SoundsHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
}
|
||||
|
||||
// check to see if active connection object exists
|
||||
if _, ok := activeConnections[c.GuildID]; !ok {
|
||||
if _, ok := ActiveConnections[c.GuildID]; !ok {
|
||||
|
||||
// Find the guild for that channel.
|
||||
newGuild, err := s.State.Guild(c.GuildID)
|
||||
@@ -77,7 +78,7 @@ func SoundsHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
}
|
||||
|
||||
// create new connection instance
|
||||
activeConnections[c.GuildID] = &AudioConnection{
|
||||
ActiveConnections[c.GuildID] = &AudioConnection{
|
||||
Guild: newGuild,
|
||||
Session: s,
|
||||
Sounds: make(map[string]*AudioClip, 0),
|
||||
@@ -87,7 +88,7 @@ func SoundsHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
}
|
||||
}
|
||||
|
||||
activeConnections[c.GuildID].handleMessage(m)
|
||||
ActiveConnections[c.GuildID].handleMessage(m)
|
||||
}
|
||||
|
||||
func (conn *AudioConnection) handleMessage(m *discordgo.MessageCreate) {
|
||||
@@ -109,7 +110,7 @@ func (conn *AudioConnection) handleMessage(m *discordgo.MessageCreate) {
|
||||
conn.clipAudio(m)
|
||||
|
||||
default:
|
||||
conn.playAudio(command, m)
|
||||
conn.PlayAudio(command, m)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -168,8 +169,19 @@ func (conn *AudioConnection) summon(m *discordgo.MessageCreate) {
|
||||
}
|
||||
}
|
||||
|
||||
// play audio in channel that user is in
|
||||
func (conn *AudioConnection) playAudio(soundName string, m *discordgo.MessageCreate) {
|
||||
func (conn *AudioConnection) queueAudio(soundName string) {
|
||||
}
|
||||
|
||||
// PlayAudio - play audio in channel that user is in
|
||||
// if MessageCreate is null play in current channel
|
||||
func (conn *AudioConnection) PlayAudio(soundName string, m *discordgo.MessageCreate) {
|
||||
|
||||
// summon bot to channel if new message passed in
|
||||
if m != nil {
|
||||
conn.summon(m)
|
||||
} else if !conn.VoiceConnection.Ready {
|
||||
return
|
||||
}
|
||||
|
||||
// check if sound exists in memory
|
||||
if _, ok := conn.Sounds[soundName]; !ok {
|
||||
@@ -182,9 +194,6 @@ func (conn *AudioConnection) playAudio(soundName string, m *discordgo.MessageCre
|
||||
}
|
||||
}
|
||||
|
||||
// summon bot to channel
|
||||
conn.summon(m)
|
||||
|
||||
// add sound to queue if queue isn't full
|
||||
select {
|
||||
case conn.SoundQueue <- soundName:
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/mgerb/go-discord-bot/server/db"
|
||||
)
|
||||
|
||||
// Message - discord message
|
||||
type Message struct {
|
||||
ID string `gorm:"primary_key" json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt *time.Time `json:"deleted_at"`
|
||||
ChannelID string `json:"channel_id"`
|
||||
Content string `json:"content"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
EditedTimestamp time.Time `json:"edited_timestamp"`
|
||||
MentionRoles string `json:"mention_roles"`
|
||||
Tts bool `json:"tts"`
|
||||
MentionEveryone bool `json:"mention_everyone"`
|
||||
User User `json:"user"`
|
||||
UserID string `json:"user_id"`
|
||||
Attachments []Attachment `json:"attachments"`
|
||||
}
|
||||
|
||||
// Save -
|
||||
func (m *Message) Save() error {
|
||||
return db.GetConn().Save(m).Error
|
||||
}
|
||||
|
||||
// Attachment - discord message attachment
|
||||
type Attachment struct {
|
||||
MessageID string `gorm:"primary_key" json:"id"`
|
||||
URL string `json:"url"`
|
||||
ProxyURL string `json:"proxy_url"`
|
||||
Filename string `json:"filename"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
Size int `json:"size"`
|
||||
}
|
||||
|
||||
// User -
|
||||
type User struct {
|
||||
ID string `gorm:"primary_key" json:"id"`
|
||||
Email string `json:"email"`
|
||||
Username string `json:"username"`
|
||||
Avatar string `json:"avatar"`
|
||||
Discriminator string `json:"discriminator"`
|
||||
Token string `json:"token"`
|
||||
Verified bool `json:"verified"`
|
||||
MFAEnabled bool `json:"mfa_enabled"`
|
||||
Bot bool `json:"bot"`
|
||||
}
|
||||
|
||||
// Save -
|
||||
func (u *User) Save() error {
|
||||
return db.GetConn().Save(u).Error
|
||||
}
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"github.com/mgerb/go-discord-bot/server/bot"
|
||||
"github.com/mgerb/go-discord-bot/server/config"
|
||||
"github.com/mgerb/go-discord-bot/server/db"
|
||||
"github.com/mgerb/go-discord-bot/server/logger"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/model"
|
||||
log "github.com/sirupsen/logrus"
|
||||
@@ -14,21 +13,18 @@ import (
|
||||
|
||||
func init() {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
log.SetFormatter(&log.JSONFormatter{})
|
||||
log.SetOutput(os.Stdout)
|
||||
|
||||
file, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
|
||||
if err == nil {
|
||||
log.SetOutput(file)
|
||||
} else {
|
||||
log.Info("Failed to log to file, using default stderr")
|
||||
}
|
||||
|
||||
//read config file
|
||||
config.Init()
|
||||
|
||||
if config.Config.Logger {
|
||||
migrations := []interface{}{
|
||||
&logger.Message{},
|
||||
&logger.Attachment{},
|
||||
&logger.User{},
|
||||
&model.VideoArchive{},
|
||||
}
|
||||
db.Init(migrations...)
|
||||
}
|
||||
db.Init(model.Migrations...)
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -8,6 +8,8 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/mgerb/go-discord-bot/server/config"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/model"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const discordAPI = "https://discordapp.com/api/v6"
|
||||
@@ -64,3 +66,43 @@ func Oauth(code string) (OauthResp, error) {
|
||||
|
||||
return oauth, nil
|
||||
}
|
||||
|
||||
// GetUserInfo - get user info
|
||||
func GetUserInfo(accessToken string) (model.User, error) {
|
||||
req, err := http.NewRequest("GET", discordAPI+"/users/@me", nil)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return model.User{}, err
|
||||
}
|
||||
|
||||
req.Header.Add("Authorization", "Bearer "+accessToken)
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return model.User{}, err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
data, err := ioutil.ReadAll(resp.Body)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return model.User{}, err
|
||||
}
|
||||
|
||||
var userInfo model.User
|
||||
|
||||
err = json.Unmarshal(data, &userInfo)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return model.User{}, err
|
||||
}
|
||||
|
||||
// filter guild based on id
|
||||
return userInfo, nil
|
||||
}
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
package discord
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// User -
|
||||
type User struct {
|
||||
Username string `json:"username"`
|
||||
Verified bool `json:"verified"`
|
||||
MFAEnabled bool `json:"mfa_enabled"`
|
||||
ID string `json:"id"`
|
||||
Avatar string `json:"avatar"`
|
||||
Discriminator string `json:"discriminator"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
// GetUserInfo - get user info
|
||||
func GetUserInfo(accessToken string) (User, error) {
|
||||
req, err := http.NewRequest("GET", discordAPI+"/users/@me", nil)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return User{}, err
|
||||
}
|
||||
|
||||
req.Header.Add("Authorization", "Bearer "+accessToken)
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return User{}, err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
data, err := ioutil.ReadAll(resp.Body)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return User{}, err
|
||||
}
|
||||
|
||||
var userInfo User
|
||||
|
||||
err = json.Unmarshal(data, &userInfo)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return User{}, err
|
||||
}
|
||||
|
||||
// filter guild based on id
|
||||
return userInfo, nil
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mgerb/go-discord-bot/server/config"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/discord"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/model"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gopkg.in/dgrijalva/jwt-go.v3"
|
||||
)
|
||||
@@ -29,7 +29,7 @@ type CustomClaims struct {
|
||||
}
|
||||
|
||||
// GetJWT - get json web token
|
||||
func GetJWT(user discord.User) (string, error) {
|
||||
func GetJWT(user model.User) (string, error) {
|
||||
|
||||
permissions := PermUser
|
||||
|
||||
|
||||
12
server/webserver/model/attachment.go
Normal file
12
server/webserver/model/attachment.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package model
|
||||
|
||||
// Attachment - discord message attachment
|
||||
type Attachment struct {
|
||||
MessageID string `gorm:"primary_key" json:"id"`
|
||||
URL string `json:"url"`
|
||||
ProxyURL string `json:"proxy_url"`
|
||||
Filename string `json:"filename"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
Size int `json:"size"`
|
||||
}
|
||||
9
server/webserver/model/index.go
Normal file
9
server/webserver/model/index.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package model
|
||||
|
||||
var Migrations []interface{} = []interface{}{
|
||||
&Message{},
|
||||
&Attachment{},
|
||||
&User{},
|
||||
&VideoArchive{},
|
||||
&Sound{},
|
||||
}
|
||||
@@ -1,34 +1,57 @@
|
||||
package logger
|
||||
package model
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"github.com/mgerb/go-discord-bot/server/db"
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
// Message - discord message
|
||||
type Message struct {
|
||||
ID string `gorm:"primary_key" json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt *time.Time `json:"deleted_at"`
|
||||
ChannelID string `json:"channel_id"`
|
||||
Content string `json:"content"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
EditedTimestamp time.Time `json:"edited_timestamp"`
|
||||
MentionRoles string `json:"mention_roles"`
|
||||
Tts bool `json:"tts"`
|
||||
MentionEveryone bool `json:"mention_everyone"`
|
||||
User User `json:"user"`
|
||||
UserID string `json:"user_id"`
|
||||
Attachments []Attachment `json:"attachments"`
|
||||
}
|
||||
|
||||
// MessageSave -
|
||||
func MessageSave(conn *gorm.DB, m *Message) error {
|
||||
return conn.Save(m).Error
|
||||
}
|
||||
|
||||
const urlRegexp = `https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)`
|
||||
|
||||
var linkedPostsCacheTimeout time.Time
|
||||
var linkedPostsCache map[string]int
|
||||
|
||||
// GetMessages - returns all messages - must use paging
|
||||
func GetMessages(page int) ([]Message, error) {
|
||||
// MessageGet - returns all messages - must use paging
|
||||
func MessageGet(conn *gorm.DB, page int) ([]Message, error) {
|
||||
messages := []Message{}
|
||||
err := db.GetConn().Offset(page*100).Limit(100).Order("timestamp desc", true).Preload("User").Find(&messages).Error
|
||||
err := conn.Offset(page*100).Limit(100).Order("timestamp desc", true).Preload("User").Find(&messages).Error
|
||||
return messages, err
|
||||
}
|
||||
|
||||
// GetLinkedMessages - get count of discord comments that contain URL's - per user
|
||||
// MessageGetLinked - get count of discord comments that contain URL's - per user
|
||||
// cached for 10 minutes because there is a lot of data filtering
|
||||
func GetLinkedMessages() (map[string]int, error) {
|
||||
func MessageGetLinked(conn *gorm.DB) (map[string]int, error) {
|
||||
|
||||
if linkedPostsCacheTimeout.After(time.Now().Add(-10 * time.Minute)) {
|
||||
return linkedPostsCache, nil
|
||||
}
|
||||
|
||||
result := []map[string]interface{}{}
|
||||
rows, err := db.GetConn().Table("messages").
|
||||
rows, err := conn.Table("messages").
|
||||
Select("users.username, messages.content").
|
||||
Joins("join users on messages.user_id = users.id").
|
||||
Rows()
|
||||
28
server/webserver/model/sound.go
Normal file
28
server/webserver/model/sound.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
type Sound struct {
|
||||
ID uint `gorm:"primary_key" json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt *time.Time `json:"deleted_at"`
|
||||
Name string `gorm:"unique" json:"name"`
|
||||
Extension string `json:"extension"`
|
||||
UserID string `json:"user_id"`
|
||||
User User `json:"user"`
|
||||
}
|
||||
|
||||
func SoundCreate(conn *gorm.DB, sound *Sound) error {
|
||||
return conn.Create(sound).Error
|
||||
}
|
||||
|
||||
func SoundList(conn *gorm.DB) ([]Sound, error) {
|
||||
sound := []Sound{}
|
||||
err := conn.Set("gorm:auto_preload", true).Find(&sound).Error
|
||||
return sound, err
|
||||
}
|
||||
28
server/webserver/model/user.go
Normal file
28
server/webserver/model/user.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
// User -
|
||||
type User struct {
|
||||
ID string `gorm:"primary_key" json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt *time.Time `json:"deleted_at"`
|
||||
Email string `json:"email"`
|
||||
Username string `json:"username"`
|
||||
Avatar string `json:"avatar"`
|
||||
Discriminator string `json:"discriminator"`
|
||||
Token string `gorm:"-" json:"token"`
|
||||
Verified bool `json:"verified"`
|
||||
MFAEnabled bool `json:"mfa_enabled"`
|
||||
Bot bool `json:"bot"`
|
||||
}
|
||||
|
||||
// UserSave -
|
||||
func UserSave(conn *gorm.DB, u *User) error {
|
||||
return conn.Save(u).Error
|
||||
}
|
||||
@@ -7,9 +7,9 @@ import (
|
||||
|
||||
// AddConfigRoutes -
|
||||
func AddConfigRoutes(group *gin.RouterGroup) {
|
||||
group.GET("/config/client_id", getClientIDHandler)
|
||||
group.GET("/config/client_id", getConfigHandler)
|
||||
}
|
||||
|
||||
func getClientIDHandler(c *gin.Context) {
|
||||
func getConfigHandler(c *gin.Context) {
|
||||
c.JSON(200, map[string]string{"id": config.Config.ClientID})
|
||||
}
|
||||
|
||||
@@ -13,10 +13,10 @@ import (
|
||||
|
||||
// AddDownloaderRoutes -
|
||||
func AddDownloaderRoutes(group *gin.RouterGroup) {
|
||||
group.GET("/ytdownloader", downloaderHandler)
|
||||
group.GET("/ytdownloader", getDownloaderHandler)
|
||||
}
|
||||
|
||||
func downloaderHandler(c *gin.Context) {
|
||||
func getDownloaderHandler(c *gin.Context) {
|
||||
url := c.Query("url")
|
||||
fileType := c.Query("fileType")
|
||||
|
||||
|
||||
@@ -4,7 +4,8 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mgerb/go-discord-bot/server/logger"
|
||||
"github.com/mgerb/go-discord-bot/server/db"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/model"
|
||||
)
|
||||
|
||||
// AddLoggerRoutes -
|
||||
@@ -20,7 +21,7 @@ func getMessagesHandler(c *gin.Context) {
|
||||
page = 0
|
||||
}
|
||||
|
||||
messages, err := logger.GetMessages(page)
|
||||
messages, err := model.MessageGet(db.GetConn(), page)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(500, err)
|
||||
@@ -31,7 +32,7 @@ func getMessagesHandler(c *gin.Context) {
|
||||
}
|
||||
|
||||
func getLinkedMessagesHandler(c *gin.Context) {
|
||||
posts, err := logger.GetLinkedMessages()
|
||||
posts, err := model.MessageGetLinked(db.GetConn())
|
||||
|
||||
if err != nil {
|
||||
c.JSON(500, err.Error())
|
||||
|
||||
@@ -2,8 +2,10 @@ package routes
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mgerb/go-discord-bot/server/db"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/discord"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/middleware"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/model"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -57,5 +59,12 @@ func oauthHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// save/update user in database
|
||||
err = model.UserSave(db.GetConn(), &user)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
|
||||
c.JSON(200, token)
|
||||
}
|
||||
|
||||
110
server/webserver/routes/sound.go
Normal file
110
server/webserver/routes/sound.go
Normal file
@@ -0,0 +1,110 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mgerb/go-discord-bot/server/bothandlers"
|
||||
"github.com/mgerb/go-discord-bot/server/config"
|
||||
"github.com/mgerb/go-discord-bot/server/db"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/middleware"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/model"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/response"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// AddSoundRoutes -
|
||||
func AddSoundRoutes(group *gin.RouterGroup) {
|
||||
group.GET("/sound", listSoundHandler)
|
||||
group.POST("/sound", middleware.AuthorizedJWT(), postSoundHandler)
|
||||
group.POST("/sound/play", middleware.AuthorizedJWT(), middleware.AuthPermissions(middleware.PermMod), postSoundPlayHandler)
|
||||
}
|
||||
|
||||
func listSoundHandler(c *gin.Context) {
|
||||
archives, err := model.SoundList(db.GetConn())
|
||||
|
||||
if err != nil {
|
||||
response.InternalError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, archives)
|
||||
}
|
||||
|
||||
func postSoundPlayHandler(c *gin.Context) {
|
||||
connections := bothandlers.ActiveConnections
|
||||
|
||||
params := struct {
|
||||
Name string `json:"name"`
|
||||
}{}
|
||||
c.BindJSON(¶ms)
|
||||
|
||||
// loop through all connections and play audio
|
||||
// currently only used with one server
|
||||
// will need selector on UI if used for multiple servers
|
||||
if len(connections) == 1 && params.Name != "" {
|
||||
for _, con := range connections {
|
||||
con.PlayAudio(params.Name, nil)
|
||||
}
|
||||
}
|
||||
|
||||
response.Success(c, "test")
|
||||
}
|
||||
|
||||
func postSoundHandler(c *gin.Context) {
|
||||
|
||||
oc, _ := c.Get("claims")
|
||||
claims, _ := oc.(*middleware.CustomClaims)
|
||||
|
||||
// TODO: verify user for upload
|
||||
|
||||
file, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
c.JSON(http.StatusInternalServerError, "Error reading file.")
|
||||
return
|
||||
}
|
||||
|
||||
// create uploads folder if it does not exist
|
||||
if _, err := os.Stat(config.Config.SoundsPath); os.IsNotExist(err) {
|
||||
os.Mkdir(config.Config.SoundsPath, os.ModePerm)
|
||||
}
|
||||
|
||||
// convert file name to lower case and trim spaces
|
||||
file.Filename = strings.Replace(strings.ToLower(file.Filename), " ", "", -1)
|
||||
|
||||
// check if file already exists
|
||||
if _, err := os.Stat(config.Config.SoundsPath + "/" + file.Filename); err == nil {
|
||||
c.JSON(http.StatusInternalServerError, "File already exists.")
|
||||
return
|
||||
}
|
||||
|
||||
err = c.SaveUploadedFile(file, config.Config.SoundsPath+"/"+file.Filename)
|
||||
log.Info(claims.Username, "uploaded", config.Config.SoundsPath+"/"+file.Filename)
|
||||
|
||||
// save who uploaded the clip into the database
|
||||
uploadSaveDB(claims.ID, file.Filename)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
c.JSON(http.StatusInternalServerError, "Error creating file.")
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, "Success")
|
||||
}
|
||||
|
||||
// save new sound to database
|
||||
func uploadSaveDB(userID, filename string) {
|
||||
splitFilename := strings.Split(filename, ".")
|
||||
extension := splitFilename[len(splitFilename)-1]
|
||||
name := strings.Join(splitFilename[:len(splitFilename)-1], ".")
|
||||
|
||||
model.SoundCreate(db.GetConn(), &model.Sound{
|
||||
UserID: userID,
|
||||
Name: name,
|
||||
Extension: extension,
|
||||
})
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mgerb/go-discord-bot/server/config"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/middleware"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// AddUploadRoutes - add file upload routes
|
||||
func AddUploadRoutes(group *gin.RouterGroup) {
|
||||
group.POST("/upload", middleware.AuthorizedJWT(), fileUploadHandler)
|
||||
}
|
||||
|
||||
func fileUploadHandler(c *gin.Context) {
|
||||
|
||||
// originalClaims, _ := c.Get("claims")
|
||||
// claims, _ := originalClaims.(*middleware.CustomClaims)
|
||||
// TODO: verify user for upload
|
||||
|
||||
file, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
c.JSON(http.StatusInternalServerError, "Error reading file.")
|
||||
return
|
||||
}
|
||||
|
||||
// create uploads folder if it does not exist
|
||||
if _, err := os.Stat(config.Config.SoundsPath); os.IsNotExist(err) {
|
||||
os.Mkdir(config.Config.SoundsPath, os.ModePerm)
|
||||
}
|
||||
|
||||
// convert file name to lower case and trim spaces
|
||||
file.Filename = strings.ToLower(file.Filename)
|
||||
file.Filename = strings.Replace(file.Filename, " ", "", -1)
|
||||
|
||||
// check if file already exists
|
||||
if _, err := os.Stat(config.Config.SoundsPath + "/" + file.Filename); err == nil {
|
||||
c.JSON(http.StatusInternalServerError, "File already exists.")
|
||||
return
|
||||
}
|
||||
|
||||
err = c.SaveUploadedFile(file, config.Config.SoundsPath+"/"+file.Filename)
|
||||
log.Debug("Saving file", config.Config.SoundsPath+"/"+file.Filename)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
c.JSON(http.StatusInternalServerError, "Error creating file.")
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, "Success")
|
||||
}
|
||||
@@ -14,14 +14,14 @@ import (
|
||||
|
||||
// AddVideoArchiveRoutes -
|
||||
func AddVideoArchiveRoutes(group *gin.RouterGroup) {
|
||||
group.GET("/video-archive", listVideoArchivesHandler)
|
||||
group.GET("/video-archive", listVideoArchiveHandler)
|
||||
|
||||
authGroup := group.Group("", middleware.AuthorizedJWT())
|
||||
authGroup.POST("/video-archive", middleware.AuthPermissions(middleware.PermMod), postVideoArchivesHandler)
|
||||
authGroup.DELETE("/video-archive/:id", middleware.AuthPermissions(middleware.PermAdmin), deleteVideoArchivesHandler)
|
||||
authGroup.POST("/video-archive", middleware.AuthPermissions(middleware.PermMod), postVideoArchiveHandler)
|
||||
authGroup.DELETE("/video-archive/:id", middleware.AuthPermissions(middleware.PermAdmin), deleteVideoArchiveHandler)
|
||||
}
|
||||
|
||||
func listVideoArchivesHandler(c *gin.Context) {
|
||||
func listVideoArchiveHandler(c *gin.Context) {
|
||||
archives, err := model.VideoArchiveList(db.GetConn())
|
||||
|
||||
if err != nil {
|
||||
@@ -32,7 +32,7 @@ func listVideoArchivesHandler(c *gin.Context) {
|
||||
response.Success(c, archives)
|
||||
}
|
||||
|
||||
func deleteVideoArchivesHandler(c *gin.Context) {
|
||||
func deleteVideoArchiveHandler(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
if id == "" {
|
||||
@@ -50,7 +50,7 @@ func deleteVideoArchivesHandler(c *gin.Context) {
|
||||
response.Success(c, "deleted")
|
||||
}
|
||||
|
||||
func postVideoArchivesHandler(c *gin.Context) {
|
||||
func postVideoArchiveHandler(c *gin.Context) {
|
||||
params := struct {
|
||||
URL string `json:"url"`
|
||||
}{}
|
||||
|
||||
@@ -29,7 +29,7 @@ func getRouter() *gin.Engine {
|
||||
routes.AddLoggerRoutes(api)
|
||||
routes.AddDownloaderRoutes(api)
|
||||
routes.AddConfigRoutes(api)
|
||||
routes.AddUploadRoutes(api)
|
||||
routes.AddSoundRoutes(api)
|
||||
routes.AddVideoArchiveRoutes(api)
|
||||
|
||||
router.NoRoute(func(c *gin.Context) {
|
||||
|
||||
Reference in New Issue
Block a user