1
0
mirror of https://github.com/mgerb/go-discord-bot synced 2026-01-14 10:42:48 +00:00

fix: channel join sound bug

This commit is contained in:
2022-03-23 22:13:28 -05:00
parent 6b8db59e98
commit 149ba48802
7 changed files with 64 additions and 74 deletions

View File

@@ -1,73 +1,73 @@
package bothandlers
/**
* DEPRECATED
* this was used to fetch a giphy image
* no longer used
*/
// /**
// * DEPRECATED
// * this was used to fetch a giphy image
// * no longer used
// */
import (
"io/ioutil"
"math/rand"
"net/http"
"strings"
"time"
// import (
// "io/ioutil"
// "math/rand"
// "net/http"
// "strings"
// "time"
"github.com/bwmarrin/discordgo"
"github.com/tidwall/gjson"
)
// "github.com/bwmarrin/discordgo"
// "github.com/tidwall/gjson"
// )
const (
gifPrefix string = "!gif "
userAgent string = "go-discord-bot"
giphyURL string = "http://api.giphy.com/v1/gifs/search?&api_key=dc6zaTOxFJmzC&limit=10&q="
)
// const (
// gifPrefix string = "!gif "
// userAgent string = "go-discord-bot"
// giphyURL string = "http://api.giphy.com/v1/gifs/search?&api_key=dc6zaTOxFJmzC&limit=10&q="
// )
// GifHandler - handler for giphy api
func GifHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
// // GifHandler - handler for giphy api
// func GifHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
// check if valid command
if strings.HasPrefix(m.Content, gifPrefix) {
// // check if valid command
// if strings.HasPrefix(m.Content, gifPrefix) {
searchText := strings.TrimPrefix(m.Content, gifPrefix)
// searchText := strings.TrimPrefix(m.Content, gifPrefix)
gifLink := getGiphy(searchText)
// gifLink := getGiphy(searchText)
if gifLink == "null" {
gifLink = "No gif found."
}
// if gifLink == "null" {
// gifLink = "No gif found."
// }
s.ChannelMessageSend(m.ChannelID, gifLink)
// s.ChannelMessageSend(m.ChannelID, gifLink)
}
}
// }
// }
// send http request to reddit
func getGiphy(searchTerm string) string {
// // send http request to reddit
// func getGiphy(searchTerm string) string {
rand.Seed(time.Now().UnixNano())
// rand.Seed(time.Now().UnixNano())
client := &http.Client{}
// client := &http.Client{}
req, err := http.NewRequest("GET", giphyURL+searchTerm, nil)
// req, err := http.NewRequest("GET", giphyURL+searchTerm, nil)
response, err := client.Do(req)
if err != nil {
return err.Error()
}
// response, err := client.Do(req)
// if err != nil {
// return err.Error()
// }
defer response.Body.Close()
// defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return err.Error()
}
// body, err := ioutil.ReadAll(response.Body)
// if err != nil {
// return err.Error()
// }
data := gjson.Get(string(body), "data").Array()
// data := gjson.Get(string(body), "data").Array()
if len(data) < 1 {
return "null"
}
// if len(data) < 1 {
// return "null"
// }
return data[rand.Intn(len(data))].Get("images.fixed_height.url").String()
}
// return data[rand.Intn(len(data))].Get("images.fixed_height.url").String()
// }

View File

@@ -2,6 +2,7 @@ package bothandlers
import (
"strings"
"time"
"github.com/bwmarrin/discordgo"
"github.com/mgerb/go-discord-bot/server/db"
@@ -16,8 +17,11 @@ func LoggerHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
model.UserSave(db.GetConn(), user)
// create and save message
timestamp, _ := m.Message.Timestamp.Parse()
editedTimestamp, _ := m.Message.EditedTimestamp.Parse()
timestamp := m.Message.Timestamp
var editedTimestamp time.Time
if m.Message.EditedTimestamp != nil {
editedTimestamp = *m.Message.EditedTimestamp
}
attachments := getAttachments(m.Message.Attachments)
message := &model.Message{

View File

@@ -57,7 +57,8 @@ func VoiceStateHandler(s *discordgo.Session, v *discordgo.VoiceStateUpdate) {
voiceConnection := conn.getVoiceConnection()
if voiceConnection != nil && voiceConnection.Ready && voiceConnection.ChannelID == v.VoiceState.ChannelID {
if voiceConnection != nil && voiceConnection.Ready && voiceConnection.ChannelID == v.VoiceState.ChannelID &&
(v.BeforeUpdate == nil || v.BeforeUpdate.ChannelID != voiceConnection.ChannelID) {
user, err := model.UserGet(db.GetConn(), v.VoiceState.UserID)