mirror of
https://github.com/mgerb/go-discord-bot
synced 2026-01-11 01:22:48 +00:00
play sounds from web ui - store uploaded sounds in database
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user