Compare commits

..

No commits in common. "33230a54d56309821ccb439c0011c7b89fdc76f0" and "ddf7ea2f15c22ed6f82be2db919a1412ad7f74e5" have entirely different histories.

10 changed files with 61 additions and 268 deletions

18
bot.go
View File

@ -9,8 +9,7 @@ import (
"syscall"
"time"
"github.com/diamondburned/arikawa/v3/state"
"github.com/diamondburned/arikawa/v3/voice"
"github.com/bwmarrin/discordgo"
"github.com/faiface/beep"
"github.com/fhs/gompd/v2/mpd"
"github.com/gohugoio/hugo/cache/filecache"
@ -52,8 +51,8 @@ func init() {
}
type App struct {
discord *state.State
voice *voice.Session
discord *discordgo.Session
voice *discordgo.VoiceConnection
youtube *youtube.Service
ambiance beep.Mixer
curamb Ambiance
@ -76,17 +75,16 @@ func main() {
ticker := time.NewTicker(300 * time.Millisecond)
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
defer cancel()
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
for {
select {
case <-ctx.Done():
app.db.Close(ctx)
case <-sc:
app.db.Close(context.Background())
app.mpdw.Close()
app.mpdc()
app.voice.Leave(ctx)
dgvc()
app.voice.Close()
app.discord.Close()
return
case <-ticker.C:

View File

@ -1,56 +1,42 @@
package main
import (
"context"
discordspeaker "dndmusicbot/speaker"
"log"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/arikawa/v3/state"
"github.com/diamondburned/arikawa/v3/voice"
"github.com/diamondburned/arikawa/v3/voice/voicegateway"
"github.com/bwmarrin/discordgo"
)
var dgvc context.CancelFunc
func init() {
log.Println("discord.go loading..")
var err error
token := config.GetString("discord.token")
guild := config.GetString("discord.guild")
channel := config.GetString("discord.channel")
ctx, cancel := context.WithCancel(context.Background())
dgvc = cancel
s := state.New("Bot " + token)
// This is required for bots.
voice.AddIntents(s)
if err := s.Open(ctx); err != nil {
log.Fatalln("failed to open gateway:", err)
}
v, err := voice.NewSession(s)
app.discord, err = discordgo.New("Bot " + token)
if err != nil {
log.Fatalln("failed to create voice session:", err)
return
}
chsf, err := discord.ParseSnowflake(channel)
// app.discord.LogLevel = discordgo.LogDebug
err = app.discord.Open()
if err != nil {
log.Fatalln("failed to create snowflake:", err)
return
}
if err := v.JoinChannelAndSpeak(ctx, discord.ChannelID(chsf), false, true); err != nil {
log.Fatalln("failed to join voice channel:", err)
app.voice, err = app.discord.ChannelVoiceJoin(guild, channel, false, true)
if err != nil {
return
}
v.Speaking(ctx, voicegateway.NotSpeaking)
app.discord = s
app.voice = v
discordspeaker.Init(v)
// app.voice.LogLevel = discordgo.LogDebug
err = discordspeaker.Init(app.voice)
if err != nil {
log.Fatal(err)
}
log.Println("discord.go done.")
}

101
events.go
View File

@ -54,112 +54,11 @@ func init() {
app.events.On("next", app.nextSong)
app.events.On("prev", app.prevSong)
app.events.On("vol_up", app.volup)
app.events.On("vol_down", app.voldown)
app.events.On("vol_set", app.volset)
//app.events.On("tick", app.checkQueue)
app.events.On("tick", app.songPosition)
//app.events.On("tick", app.checkTimeleft)
}
func (app *App) volup(payload ...interface{}) {
if !(len(payload) > 0) {
log.Println("volup called without a payload.")
return
}
var t string
switch data := payload[0].(type) {
case json.RawMessage:
var err error
err = json.Unmarshal(data, &t)
if err != nil {
log.Println(err)
return
}
default:
log.Println("volup called with invalid payload.")
return
}
switch t {
case "playlist":
pl_volume.Volume = pl_volume.Volume + 0.1
case "ambiance":
amb_volume.Volume = amb_volume.Volume + 0.1
}
}
func (app *App) voldown(payload ...interface{}) {
if !(len(payload) > 0) {
log.Println("voldown called without a payload.")
return
}
var t string
switch data := payload[0].(type) {
case json.RawMessage:
var err error
err = json.Unmarshal(data, &t)
if err != nil {
log.Println(err)
return
}
default:
log.Println("voldown called with invalid payload.")
return
}
switch t {
case "playlist":
pl_volume.Volume = pl_volume.Volume - 0.1
case "ambiance":
amb_volume.Volume = amb_volume.Volume - 0.1
}
}
func (app *App) volset(payload ...interface{}) {
if !(len(payload) > 0) {
log.Println("volset called without a payload.")
return
}
var data map[string]string
switch js := payload[0].(type) {
case json.RawMessage:
json.Unmarshal(js, &data)
default:
log.Println("volset called with invalid payload.")
return
}
vol, err := strconv.ParseFloat(data["vol"], 64)
if err != nil {
log.Println(err)
return
}
switch data["type"] {
case "playlist":
pl_volume.Volume = vol
case "ambiance":
amb_volume.Volume = vol
}
app.sendVolume()
}
func (app *App) sendVolume() {
msg := make(map[string]interface{})
out := make(map[string]float64)
msg["event"] = "volume"
out["playlist"] = pl_volume.Volume
out["ambiance"] = amb_volume.Volume
msg["payload"] = out
ws_msg <- msg
}
func (app *App) songInfoEvent(event string) map[string]interface{} {
msg := make(map[string]interface{})
msg["event"] = event

14
go.mod
View File

@ -5,8 +5,8 @@ replace github.com/fhs/gompd/v2 => /home/steino/dev/go/gompd/
go 1.19
require (
github.com/bwmarrin/discordgo v0.26.1
github.com/davecheney/xattr v0.0.0-20151008032638-dc6dbbe49f0b
github.com/diamondburned/arikawa/v3 v3.1.1-0.20221210140357-3b98cde06f9e
github.com/faiface/beep v1.1.0
github.com/fhs/gompd/v2 v2.3.0
github.com/gohugoio/hugo v0.106.0
@ -22,10 +22,11 @@ require (
github.com/spf13/afero v1.9.3
github.com/spf13/viper v1.14.0
github.com/tidwall/gjson v1.14.3
golang.org/x/net v0.3.0
golang.org/x/time v0.3.0
golang.org/x/net v0.2.0
golang.org/x/time v0.2.0
google.golang.org/api v0.103.0
gopkg.in/hraban/opus.v2 v2.0.0-20220302220929-eeacdbcb92d0
layeh.com/gopus v0.0.0-20210501142526-1ee02d434e32
)
require (
@ -52,7 +53,6 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.0 // indirect
github.com/googleapis/gax-go/v2 v2.7.0 // indirect
github.com/gorilla/schema v1.2.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
@ -77,10 +77,10 @@ require (
github.com/tidwall/pretty v1.2.1 // indirect
github.com/yuin/goldmark v1.5.3 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.4.0 // indirect
golang.org/x/crypto v0.2.0 // indirect
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
golang.org/x/sys v0.2.0 // indirect
golang.org/x/text v0.4.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20221027153422-115e99e71e1c // indirect
google.golang.org/grpc v1.50.1 // indirect

31
go.sum
View File

@ -72,6 +72,8 @@ github.com/bep/overlayfs v0.6.0/go.mod h1:NFjSmn3kCqG7KX2Lmz8qT8VhPPCwZap3UNogXa
github.com/bep/tmc v0.5.1 h1:CsQnSC6MsomH64gw0cT5f+EwQDcvZz4AazKunFwTpuI=
github.com/bep/workers v1.0.0 h1:U+H8YmEaBCEaFZBst7GcRVEoqeRC9dzH2dWOwGmOchg=
github.com/bep/workers v1.0.0/go.mod h1:7kIESOB86HfR2379pwoMWNy8B50D7r99fRLUyPSNyCs=
github.com/bwmarrin/discordgo v0.26.1 h1:AIrM+g3cl+iYBr4yBxCBp9tD9jR3K7upEjl0d89FRkE=
github.com/bwmarrin/discordgo v0.26.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
@ -91,8 +93,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecheney/xattr v0.0.0-20151008032638-dc6dbbe49f0b h1:a/CjIrvEH2NkUUIo4sqWIw+h3E63ttmS8L8Vx3ZaLS0=
github.com/davecheney/xattr v0.0.0-20151008032638-dc6dbbe49f0b/go.mod h1:Gc/R1HBRJIEElnD4PGXGQZQYMb14oPbvTovm6WeuAvk=
github.com/diamondburned/arikawa/v3 v3.1.1-0.20221210140357-3b98cde06f9e h1:vP/3xB9FOXh+rj86fQdlz/LFBmSeGmFHnmpDbF7QTCw=
github.com/diamondburned/arikawa/v3 v3.1.1-0.20221210140357-3b98cde06f9e/go.mod h1:5jBSNnp82Z/EhsKa6Wk9FsOqSxfVkNZDTDBPOj47LpY=
github.com/disintegration/gift v1.2.1 h1:Y005a1X4Z7Uc+0gLpSAsKhWi4qLtsdEcMIbbdvdZ6pc=
github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
@ -202,8 +202,6 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m
github.com/googleapis/gax-go/v2 v2.7.0 h1:IcsPKeInNvYi7eqSaDjiZqDDKu5rsmunY0Y1YupQSSQ=
github.com/googleapis/gax-go/v2 v2.7.0/go.mod h1:TEop28CZZQ2y+c0VxMUmu1lV+fQx57QpBWsYpwqHJx8=
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
github.com/gorilla/schema v1.2.0 h1:YufUaxZYCKGFuAq3c96BOhjgd5nmXiOY9NGzF247Tsc=
github.com/gorilla/schema v1.2.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
@ -355,10 +353,9 @@ golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.4.0 h1:UVQgzMY87xqpKNgb+kDsll2Igd33HszWHFLmpaRMq/8=
golang.org/x/crypto v0.4.0/go.mod h1:3quD/ATkf6oY+rnes5c3ExXTbLc8mueNue5/DoinL80=
golang.org/x/crypto v0.2.0 h1:BRXPfhNivWL5Yq0BGQ39a2sW6t44aODpfxkWjYdzewE=
golang.org/x/crypto v0.2.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
@ -429,8 +426,8 @@ golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.3.0 h1:VWL6FNY2bEEmsGVKabSlHu5Irp34xmMRoqb/9lF9lxk=
golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
golang.org/x/net v0.2.0 h1:sZfSu1wtKLGlWI4ZZayP0ck9Y73K1ynO6gqzTdBVdPU=
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@ -492,11 +489,10 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211001092434-39dca1131b70/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@ -505,14 +501,13 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.2.0 h1:52I/1L54xyEQAYdtcSuxtiT84KGYTBGXwayxmIpNJhE=
golang.org/x/time v0.2.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
@ -690,6 +685,8 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
layeh.com/gopus v0.0.0-20210501142526-1ee02d434e32 h1:/S1gOotFo2sADAIdSGk1sDq1VxetoCWr6f5nxOG0dpY=
layeh.com/gopus v0.0.0-20210501142526-1ee02d434e32/go.mod h1:yDtyzWZDFCVnva8NGtg38eH2Ns4J0D/6hD+MMeUGdF0=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=

View File

@ -13,35 +13,25 @@ import (
discordspeaker "dndmusicbot/speaker"
)
var pl_volume *effects.Volume
var amb_volume *effects.Volume
func init() {
log.Println("queue.go loading..")
app.ambiance = beep.Mixer{}
amb_volume = &effects.Volume{
Streamer: &app.ambiance,
Base: 2,
Volume: 2,
Silent: false,
}
discordspeaker.Play(amb_volume)
discordspeaker.Play(&app.ambiance)
mpdstream, err := NewMPD()
if err != nil {
log.Fatal(err)
}
pl_volume = &effects.Volume{
volume := &effects.Volume{
Streamer: mpdstream,
Base: 2,
Volume: -2,
Silent: false,
}
discordspeaker.Play(pl_volume)
discordspeaker.Play(volume)
/*
app.queue = new(Queue)

View File

@ -1,35 +1,31 @@
package discordspeaker
import (
"context"
"log"
"sync"
"time"
"github.com/diamondburned/arikawa/v3/voice"
"github.com/diamondburned/arikawa/v3/voice/voicegateway"
"github.com/bwmarrin/discordgo"
"github.com/faiface/beep"
"github.com/pkg/errors"
"gopkg.in/hraban/opus.v2"
)
var (
mu sync.Mutex
mixer beep.Mixer
samples [][2]float64
done chan struct{}
encoder *opus.Encoder
//voice *discordgo.VoiceConnection
mu sync.Mutex
mixer beep.Mixer
samples [][2]float64
done chan struct{}
encoder *opus.Encoder
voice *discordgo.VoiceConnection
frameSize int = 960
channels int = 2
sampleRate int = 48000
maxBytes int = (frameSize * 2) * 2
buf []byte
session *voice.Session
spk bool
)
func Init(dgv *voice.Session) error {
func Init(dgv *discordgo.VoiceConnection) error {
var err error
mu.Lock()
@ -40,7 +36,7 @@ func Init(dgv *voice.Session) error {
buf = make([]byte, maxBytes)
samples = make([][2]float64, frameSize)
session = dgv
voice = dgv
encoder, err = opus.NewEncoder(sampleRate, channels, opus.AppVoIP)
encoder.SetBitrateToMax()
@ -101,21 +97,10 @@ func update() {
}
if Silence(f32) {
if spk {
log.Println("Notspeaking")
session.Speaking(context.Background(), voicegateway.NotSpeaking)
spk = false
}
time.Sleep(100 * time.Millisecond)
return
}
if !spk {
log.Println("Speaking")
session.Speaking(context.Background(), voicegateway.Microphone)
spk = true
}
n, err := encoder.EncodeFloat32(f32, buf)
if err != nil {
log.Println(err)
@ -123,12 +108,11 @@ func update() {
return
}
_, err = session.Write(buf[:n])
if err != nil {
log.Println(err)
time.Sleep(100 * time.Millisecond)
if !voice.Ready || voice.OpusSend == nil {
return
}
voice.OpusSend <- buf[:n]
}
func Silence(in []float32) bool {

View File

@ -35,41 +35,7 @@ window.onload = function () {
const link = document.querySelector("#link")
const time = document.querySelector("#time")
const waiting = document.querySelector("#waiting")
const pvol = document.querySelector("#playlist-volume")
const avol = document.querySelector("#ambiance-volume")
pvol.addEventListener("change", (e) => {
e.target.nextElementSibling.value = e.target.value
ws.send(JSON.stringify({
"event": "vol_set",
"payload": {
"type": "playlist",
"vol": e.target.value
}
}))
})
pvol.nextElementSibling.addEventListener("change", (e) => {
e.target.previousElementSibling.value = e.target.value
e.target.previousElementSibling.dispatchEvent(new Event('change'));
})
avol.addEventListener("change", (e) => {
e.target.nextElementSibling.value = e.target.value
ws.send(JSON.stringify({
"event": "vol_set",
"payload": {
"type": "ambiance",
"vol": e.target.value
}
}))
})
avol.nextElementSibling.addEventListener("change", (e) => {
e.target.previousElementSibling.value = e.target.value
e.target.previousElementSibling.dispatchEvent(new Event('change'));
})
addInteractHandler(
document.querySelector("input#next"), (e) => {
ws.send(JSON.stringify({
@ -141,12 +107,6 @@ window.onload = function () {
ws.onmessage = (e) => {
data = JSON.parse(e.data)
switch (data.event) {
case "volume":
pvol.value = data.payload.playlist
pvol.nextElementSibling.value = data.payload.playlist
avol.value = data.payload.ambiance
avol.nextElementSibling.value = data.payload.ambiance
break
case "ambiance_add":
const container = document.querySelector("#ambiance")
var newdiv = document.createElement('div');

View File

@ -67,19 +67,6 @@
</div>
</section>
<section>
<div id="volume_playlist" class="input-container">
<label for="playlist-volume">Playlist</label>
<input type="range" id="playlist-volume" min="-6" max="1" step="0.1">
<input id="playlist-volume-number" type="number" min="-6" max="1" step="0.1" style="width:50px" />
</div>
<div id="volume_ambiance" class="input-container">
<label for="ambiance-volume">Ambiance</label>
<input type="range" id="ambiance-volume" min="-4" max="4" step="0.1">
<input id="ambiance-volume-number" type="number" min="-4" max="4" step="0.1" style="width:50px" />
</div>
</section>
<!-- The Modal -->
<div id="waiting" class="modal">
<!-- Modal content -->

8
ws.go
View File

@ -66,16 +66,8 @@ func handleWS(c *websocket.Conn) error {
msg := app.songInfoEvent("song_info")
vol := make(map[string]interface{})
volout := make(map[string]float64)
vol["event"] = "volume"
volout["playlist"] = pl_volume.Volume
volout["ambiance"] = amb_volume.Volume
vol["payload"] = volout
c.SetWriteDeadline(time.Now().Add(10 * time.Second))
c.WriteJSON(msg)
c.WriteJSON(vol)
if app.ambiance.Len() > 0 {
msg := make(map[string]interface{})