Created initial discord bot that announces when someone joins a voice chat.
This commit is contained in:
parent
6aad0cf6c3
commit
0db4546530
11
go.mod
Normal file
11
go.mod
Normal file
@ -0,0 +1,11 @@
|
||||
module dev.justinjudd.com/discord_bots
|
||||
|
||||
go 1.24.1
|
||||
|
||||
require (
|
||||
github.com/bwmarrin/discordgo v0.29.0 // indirect
|
||||
github.com/gorilla/websocket v1.4.2 // indirect
|
||||
github.com/joho/godotenv v1.5.1 // indirect
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
|
||||
)
|
14
go.sum
Normal file
14
go.sum
Normal file
@ -0,0 +1,14 @@
|
||||
github.com/bwmarrin/discordgo v0.29.0 h1:FmWeXFaKUwrcL3Cx65c20bTRW+vOb6k8AnaP+EgjDno=
|
||||
github.com/bwmarrin/discordgo v0.29.0/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
|
||||
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
98
voice_announce/main.go
Normal file
98
voice_announce/main.go
Normal file
@ -0,0 +1,98 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand/v2"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
var botToken string
|
||||
var announceChannelName string
|
||||
var cleanDelay = 30
|
||||
|
||||
var joinMessages = []string{
|
||||
"<@%s> has joined <#%s>",
|
||||
"<@%s> has joined <#%s>; Join in for some nerd talk",
|
||||
"<#%[2]s> is the place to be, <@%[1]s> just joined",
|
||||
"<#%[2]s> just go a bit cooler, <@%[1]s> is now in",
|
||||
"<@%s> is hanging out in <#%s>",
|
||||
}
|
||||
|
||||
func main() {
|
||||
envs, err := godotenv.Read()
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to get environment variables: %v", err)
|
||||
}
|
||||
|
||||
// set values from env
|
||||
botToken = envs["BOT_TOKEN"]
|
||||
announceChannelName = envs["ANNOUNCE_CHANNEL"]
|
||||
if delay, ok := envs["CLEAN_DELAY"]; ok {
|
||||
if d2, err := strconv.Atoi(delay); err == nil {
|
||||
cleanDelay = d2
|
||||
}
|
||||
}
|
||||
|
||||
s, err := discordgo.New("Bot " + botToken)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
|
||||
for _, g := range r.Guilds {
|
||||
g2, _ := s.Guild(g.ID)
|
||||
log.Printf("Bot is connected to %q.", g2.Name)
|
||||
}
|
||||
})
|
||||
s.AddHandler(voiceStatus)
|
||||
|
||||
err = s.Open()
|
||||
if err != nil {
|
||||
log.Fatalf("Cannot open the session: %v", err)
|
||||
}
|
||||
defer s.Close()
|
||||
|
||||
stop := make(chan os.Signal, 1)
|
||||
signal.Notify(stop, os.Interrupt)
|
||||
<-stop
|
||||
}
|
||||
|
||||
// Handle for when someone joins a voice channel - send notification about them joining.
|
||||
func voiceStatus(s *discordgo.Session, m *discordgo.VoiceStateUpdate) {
|
||||
if m.BeforeUpdate != nil || m.VoiceState == nil {
|
||||
return
|
||||
}
|
||||
channels, err := s.GuildChannels(m.VoiceState.GuildID)
|
||||
if err != nil {
|
||||
log.Printf("Unable to get channels for Guild: %s", m.VoiceState.GuildID)
|
||||
return
|
||||
}
|
||||
var announceChannel *discordgo.Channel
|
||||
for _, c := range channels {
|
||||
if c.Name == announceChannelName {
|
||||
announceChannel = c
|
||||
}
|
||||
}
|
||||
if announceChannel == nil {
|
||||
log.Printf("Unable to get announce channel for Guild: %s", m.VoiceState.GuildID)
|
||||
return
|
||||
}
|
||||
msg := joinMessages[rand.IntN(len(joinMessages))]
|
||||
|
||||
sent, err := s.ChannelMessageSend(announceChannel.ID, fmt.Sprintf(msg, m.VoiceState.UserID, m.VoiceState.ChannelID))
|
||||
g, _ := s.Guild(m.VoiceState.GuildID)
|
||||
log.Default().Printf("%q has joined %q in %s", m.VoiceState.Member.DisplayName(), m.VoiceState.ChannelID, g.Name)
|
||||
if err != nil {
|
||||
log.Printf("unable to send message: %s", err)
|
||||
}
|
||||
time.AfterFunc(time.Second*time.Duration(cleanDelay), func() {
|
||||
s.ChannelMessageDelete(sent.ChannelID, sent.ID)
|
||||
})
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user