Discord Bot(C#)加入语音通道的问题

时间:2017-02-05 11:49:49

标签: c# discord.net

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

代码:

    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

我犯了错误吗?

感谢您的回答。

2 个答案:

答案 0 :(得分:1)

您的服务器是否呼叫&#34; VoiceChannel&#34;?

如果没有,请通过电话 discord.FindServers("VoiceChannel")

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

例如,如果您的服务器名为&#34;我的服务器&#34;并且语音频道名为&#34; VoiceChannel&#34;你可以使用这种结构来获得你的语音频道: discord.Servers.Single(s => s.Name == "My server").VoiceChannels.Single(v => v.Name == "VoiceChannel")

答案 1 :(得分:0)

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

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