【发布时间】: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