【问题标题】:e.After.VoiceChannel.Name Discord.NET - Can't figure out null errore.After.VoiceChannel.Name Discord.NET - 无法找出空错误
【发布时间】:2017-09-19 03:32:15
【问题描述】:

在你判断我发布另一个 NullReferenceException 问题之前,请阅读我的代码。我已经在谷歌上搜索 NullReferenceException 错误有一段时间了,但它没有解释为什么这个特定部分会给我一个错误。

    if (e.After.VoiceChannel.Name != null)

我还查看了 Discord.NET 的文档,但是也没有任何结果。这是整个文件。

using Discord;
using Discord.Commands;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DiscBot
{
    class myBot
    {
        DiscordClient discord;

        public myBot()
        {

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

            discord.UsingCommands(x => 
            {
                x.PrefixChar = '~';
                x.AllowMentionPrefix = true;
                x.HelpMode = HelpMode.Public;
            });

            var commands = discord.GetService<CommandService>();

            commands.CreateCommand("greet")
                .Description("Does a thing")
                .Parameter("theGreeted", ParameterType.Unparsed)
                .Do(async (e)=> 
                {
                    var msgtoRead = e.Channel.DownloadMessages(1);

                    await e.Channel.SendTTSMessage("Hello " + e.GetArg("theGreeted"));
                });

            discord.UserUpdated += async (s, e) => {
                var channel = e.Server.FindChannels("general", ChannelType.Text).FirstOrDefault();
               string usrnm = e.After.Name;

                // This shouldn't get an error
                // But when I run the code I
                // get a Null Reference Exception

                if (e.After.VoiceChannel.Name != null)
                {
                    await channel.SendMessage(usrnm + " to " + e.After.VoiceChannel.Name);
                }
                else
                {

                    await channel.SendMessage(usrnm + " exited");
                }
            };


            discord.UserJoined += async (s, e) =>
            {
                var channel = e.Server.FindChannels("mainbois", ChannelType.Text).FirstOrDefault();
                var user = e.User;
                await channel.SendTTSMessage(string.Format("Another human has entered..."));
            };

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

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

非常感谢任何帮助。提前致谢!

附:如果我在某处犯了一个简单的错误,请责备。 :P

【问题讨论】:

  • 编辑:e.After.VoiceChannel 为空。我对这个事件的了解不够多,无法说明原因。

标签: c# nullreferenceexception discord discord.net


【解决方案1】:

发现无法查看

if (e.After.VoiceChannel.Name != null)

如果 VoiceChannel 为空。换句话说,我无法检查基于 null 的值(如果这有意义的话)。我的新代码看起来像

if (e.After.VoiceChannel != null)

感谢任何花时间查看我的代码的人。 :D

【讨论】:

    猜你喜欢
    • 2022-07-08
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多