doukui4836 2018-04-08 07:21
浏览 1217
已采纳

如何使用discordgo获取所有频道的列表?

I would like to send a message on all text channels of my private discord server using a bot.

I have connected and can have a Session object but I'm not sure how to get a list of all available channels from the Session.

dg, err := discordgo.New("Bot " + Token)
if err != nil {
    fmt.Println("error creating Discord session,", err)
    return
}

// Open a websocket connection to Discord and begin listening.
err = dg.Open()
if err != nil {
    fmt.Println("error opening connection,", err)
    return
}

// Get all channel ID's from dg here

Is this even possible with the discord API?

  • 写回答

1条回答 默认 最新

  • doutan5798 2018-08-08 07:53
    关注

    Don't know if this is still relevant to you, but leaving this here for anyone else wondering.

    All you need is access to the discordgo.Session object, dg will in your case work just the same.

    It is possible, but you have to loop through each Guild (server) the bot has access to. Alternatively, if you have the relevant guild ID or object you can loop through the channels of only that guild.

    func spam(s *discordgo.Session) {
        // Loop through each guild in the session
        for _, guild := range s.State.Guilds {
    
            // Get channels for this guild
            channels, _ := s.GuildChannels(guild.ID)
    
            for _, c := range channels {
                // Check if channel is a guild text channel and not a voice or DM channel
                if c.Type != discordgo.ChannelTypeGuildText {
                    continue
                }
    
                // Send text message
                s.ChannelMessageSend(
                    c.ID,
                    fmt.Sprintf("testmsg (sorry for spam). Channel name is %q", c.Name),
                )
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?