1
在您判斷我發佈ANOTHER NullReferenceException問題之前,請閱讀我的代碼。我一直在使用Google的NullReferenceException錯誤一段時間,並沒有解釋爲什麼這個特定的部分給我一個錯誤。e.After.VoiceChannel.Name Discord.NET - 找不到空錯誤
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.S.如果我在某個地方犯了一個簡單的錯誤,請懲罰。 :P
編輯:'e.After.VoiceChannel'爲空。我不知道這個事件足以說明原因。 –