【问题标题】:Discord Bot (C#) Problems Joining Voice ChannelDiscord Bot (C#) 问题加入语音频道
【发布时间】:2017-06-22 10:41:03
【问题描述】:

我的机器人加入语音频道时遇到问题。

代码:

    using Discord;
    using Discord.Commands;
    using Discord.Audio;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace DodoBot
    {
        class MyBot
        {
            DiscordClient discord;
            CommandService commands;
            Random rand;
            string[] cats = new string[]
            {
                "cate.jpg",
                "gut.jpg",
                "meh.jpg",
                "ugly.jpg",
                "wow.jpg",
            };

            public MyBot()
            {
                rand = new Random();
                discord = new DiscordClient(x =>
                {
                    x.LogLevel = LogSeverity.Info;
                    x.LogHandler = Log;
                });

                discord.UsingCommands(x =>
                {
                    x.PrefixChar = '!';
                    x.AllowMentionPrefix = true;
                });

                commands = discord.GetService<CommandService>();

                RegisterHiCommand();
                RegisterCatdCommand();
                RegisterCatCommand();

                OnJoin();
                OnLeave();

                discord.UsingAudio(x =>
                {
                    x.Mode = AudioMode.Outgoing;
                    RegisterJoinVoiceCommand();
                });

                discord.ExecuteAndWait(async () =>
                {
                    await discord.Connect("MyToken", TokenType.Bot);
                });
            }

            private void RegisterJoinVoiceCommand()
            {
                commands.CreateCommand("summon")
                    .Do(async (e) =>
                    {
                        await e.Channel.SendMessage("```Joining masta!```");
                        await discord.GetService<AudioService>().Join(discord.FindServers("VoiceChannel").FirstOrDefault().VoiceChannels.FirstOrDefault());
                    });
            }

            private void RegisterCatdCommand()
            {
                commands.CreateCommand("catd")
                    .Do(async (e) =>
                    {
                        Message[] msg2Del;
                        msg2Del = await e.Channel.DownloadMessages(1);
                        await e.Channel.DeleteMessages(msg2Del);
                        int imgIndex = rand.Next(cats.Length);
                        await e.Channel.SendFile("Cats/"+cats[imgIndex]);
                    });
            }
            private void RegisterCatCommand()
            {
                commands.CreateCommand("cat")
                    .Do(async (e) =>
                    {
                        int imgIndex = rand.Next(cats.Length);
                        await e.Channel.SendFile("Cats/" + cats[imgIndex]);
                    });
            }
            private void RegisterHiCommand()
            {
                commands.CreateCommand("hi")
                    .Do(async (e) =>
                    {
                        await e.Channel.SendMessage("HelloWorld!");
                    });
            }

            private void OnJoin()
            {
                discord.UserJoined += async (s, e) =>
                {
                    var channel = e.Server.FindChannels("general").FirstOrDefault();
                    var user = e.User.Name;
                    await channel.SendMessage(string.Format("@"+user + " has joined!"));
                };
            }

            private void OnLeave()
            {
                discord.UserLeft += async (s, e) =>
                {
                    var channel = e.Server.FindChannels("general").FirstOrDefault();
                    var user = e.User.Name;
                    await channel.SendMessage(string.Format("@"+user + " has left!"));
                };
            }

            private void Log(object sender, LogMessageEventArgs e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }

我已经按照文档here 中的内容完成了所有操作。

它执行 SendMessange 命令,但不加入语音通道。

但是 Visual Studio 说: here

我做错了吗?

感谢您的回答。

【问题讨论】:

  • 您使用的是 discord.NET 0.9.4 吗?该文档适用于该版本。
  • 不,我在 0.9.6。但我找不到它的文档。
  • 说实话我对 Discord.NET 0.9.6 中的音频系统不是很熟悉,你可以在这里寻找 API 包的开发者:discord.gg/JBp6gSN
  • 谢谢,我会检查一下。

标签: c# discord.net


【解决方案1】:

你的服务器叫“VoiceChannel”吗?

如果不是,那么通过调用 discord.FindServers("VoiceChannel")

您的客户端很可能没有找到 (null) 而不是服务器集合,并尝试从其中的 First() 中获取语音通道

例如,如果您的服务器名为“我的服务器”并且有名为“VoiceChannel”的语音通道,您可以使用此构造来获取您的语音通道: discord.Servers.Single(s =&gt; s.Name == "My server").VoiceChannels.Single(v =&gt; v.Name == "VoiceChannel")

【讨论】:

    【解决方案2】:

    在您的class myBot 下拨打此电话, IVoiceChannel channel; IAudioClient client; 尝试在您的音乐命令中使用它,如果您需要更多信息,我可以分享我的整个音乐模块,但这应该可以解决问题。

    IVoiceChannel channel = (Context.User as IVoiceState).VoiceChannel;
    IAudioClient client = await channel.ConnectAsync();
    

    【讨论】:

      猜你喜欢
      • 2020-11-04
      • 2020-11-03
      • 1970-01-01
      • 2022-01-23
      • 2021-01-05
      • 2020-06-26
      • 2020-12-21
      • 1970-01-01
      • 2020-10-07
      相关资源
      最近更新 更多