-
I have been trying to get this to work for a few days now, without any luck. I get that we are supposed to use some State situation, but I can't seem to figure out how. My simplified example below: package main
import (
"fmt"
"time"
"github.com/bwmarrin/discordgo"
)
func testCommandHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
channels, err := s.GuildChannels(i.GuildID)
if err != nil {
panic(err)
}
members := []*discordgo.ThreadMember{}
for _, channel := range channels {
if channel.Type == discordgo.ChannelTypeGuildVoice {
members = append(members, channel.Members...)
}
}
fmt.Print(members)
if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral,
Content: fmt.Sprintf("just a test: %d", len(members)),
},
}); err != nil {
panic(err)
}
}
func startBot() {
fmt.Println("starting new discord bot")
b, err := discordgo.New("Bot secret-token")
if err != nil {
panic(err)
}
b.Identify.Intents = discordgo.IntentGuildMembers
b.AddHandler(testCommandHandler)
if err := b.Open(); err != nil {
panic(err)
}
if _, err := b.ApplicationCommandCreate("1234567890", "1234567890", &discordgo.ApplicationCommand{
Name: "test",
Description: "Get the members in all voice channels",
}); err != nil {
panic(err)
}
}
func main() {
go startBot()
for {
fmt.Println("running")
time.Sleep(10 * time.Second)
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
koo04
Nov 7, 2022
Replies: 1 comment
-
I finally answered my own question. Will put the code here and will likely make a snippet to reference, later: package main
import (
"fmt"
"time"
"github.com/bwmarrin/discordgo"
)
func testCommandHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
guild, err := s.State.Guild(i.GuildID)
if err != nil {
panic(err)
}
if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral,
Content: fmt.Sprintf("just a test: %d", len(guild.VoiceStates)),
},
}); err != nil {
panic(err)
}
}
func startBot() {
fmt.Println("starting new discord bot")
b, err := discordgo.New("Bot secret-token")
if err != nil {
panic(err)
}
b.Identify.Intents = discordgo.IntentsAll
b.AddHandler(testCommandHandler)
if err := b.Open(); err != nil {
panic(err)
}
if _, err := b.ApplicationCommandCreate("app-id", "guild-id", &discordgo.ApplicationCommand{
Name: "test",
Description: "Get the number of members in all voice channel",
}); err != nil {
panic(err)
}
}
func main() {
go startBot()
for {
fmt.Println("running")
time.Sleep(10 * time.Second)
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
koo04
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I finally answered my own question. Will put the code here and will likely make a snippet to reference, later: