Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add UserVoiceState #1590

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

raa0121
Copy link

@raa0121 raa0121 commented Jan 5, 2025

close: #1375

By getting the user's VoiceState from *Session, the bot can log into that channel.

example

package main

import (
    "context"
    "log"
    "os"
    "os/signal"
    "syscall"

    "github.com/bwmarrin/discordgo"
    "github.com/joho/godotenv"
)

var (
    ctx context.Context

    voiceConnections []*discordgo.VoiceConnection
    textChannels []string

    commands = []*discordgo.ApplicationCommand{
        {
            Name: "vs-connect",
            Description: "add channel to text2speach",
        },
        {
            Name: "vs-disconnect",
            Description: "stop text2speach",
        },
    }

    commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate) {
        "vs-connect": connect,
        "vs-disconnect": disconnect,
    }
)

func main() {
    botToken := os.Getenv("DISCORD_BOT_TOKEN")
    if botToken == "" {
        log.Fatal("DISCORD_BOT_TOKEN is not set.")
    }

    dg, err := discordgo.New("Bot " + botToken)
    if err != nil {
        log.Fatal(err)
    }
    defer dg.Close()
    dg.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
        if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
            h(s, i)
        }
    })

    dg.Identify.Intents = discordgo.IntentsGuilds | discordgo.IntentsGuildMessages | discordgo.IntentsGuildVoiceStates
    err = dg.Open()
    if err != nil {
        log.Fatal(err)
    }

    log.Println("Adding commands...")
    registeredCommands := make([]*discordgo.ApplicationCommand, len(commands))
    for i, v := range commands {
        cmd, err := dg.ApplicationCommandCreate(dg.State.User.ID, "", v)
        if err != nil {
            log.Panicf("Cannot create '%v' command: %v", v.Name, err)
        }
        registeredCommands[i] = cmd
    }

    fmt.Println("Bot is now running.  Press CTRL-C to exit.")
    sc := make(chan os.Signal, 1)
    signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
    <-sc
}

func connect(s *discordgo.Session, i *discordgo.InteractionCreate) {
    var channelId string
    voiceState, err := s.UserVoiceState(i.GuildID, i.Interaction.Member.User.ID)
    if err != nil {
        s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
            Type: discordgo.InteractionResponseChannelMessageWithSource,
            Data: &discordgo.InteractionResponseData{
                Content: "Please Join VoiceChannel.",
            },
        })
        return
    }
    channelId = voiceState.ChannelID
    j, err := s.ChannelVoiceJoin(i.GuildID, channelId, false, false)
    if err != nil {
        s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
            Type: discordgo.InteractionResponseChannelMessageWithSource,
            Data: &discordgo.InteractionResponseData{
                Content: "Failed to join VoiceChannel.",
            },
        })
        return
    }
    s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
        Type: discordgo.InteractionResponseChannelMessageWithSource,
        Data: &discordgo.InteractionResponseData{
            Content: "Start Text2Speach.",
        },
    })
    voiceConnections = append(voiceConnections, j)
    textChannels = append(textChannels, i.ChannelID)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't detect speaking state
1 participant