有人知道为什么我在尝试处理 Reaction 时会收到此 NullReferenceException 错误
Does somebody know why i get this NullReferenceException error when trying to handle a Reaction
所以我正在为我的 Discord 服务器创建一个 Discord Bot,但我不知道为什么它不起作用。我认为问题出在我的 ReactionHandler 中,但我不确定是什么原因造成的?
我试图修改一些代码..但我无法弄清楚所以我想问问是否有人知道我的代码有什么问题......我的代码可能很乱,但我希望它不介意你。我不会展示我的 "Commands" 脚本,因为它与这个问题无关!
private async Task Client_CheckTicket(SocketMessage arg)
{
try
{
var Message = arg as SocketUserMessage;
var Context = new SocketCommandContext(Client, Message);
if (Context.Channel.Id == debugchannel)
{
Console.WriteLine("Detected a debug message i wont be taking any actions against it");
return;
}
if (Context.User.IsBot)
{
String s = "";
String path = @"C:\Users\Alex\source\repos\discbot\ids.txt";
using (StreamReader sr = File.OpenText(path))
{
try
{
while ((s = sr.ReadLine()) != null)
{
await DebugMessage("Checking this message: " + Message.Content);
if (Context.Message.Content.Contains(s))
{
messageid = Message.Id;
await DebugMessage("added");
}
}
}
catch (Exception e)
{
await DebugMessage("Something went wrong... Exception was sent to console!");
Console.WriteLine(e);
return;
}
sr.Close();
}
if (messageid != 0)
{
path = @"C:\Users\Alex\source\repos\discbot\messageid.txt";
using (StreamWriter sr = File.AppendText(path))
{
sr.WriteLine(messageid);
sr.Close();
}
}
else
{
await DebugMessage("MessageID was 0 so i will not add it to my database");
return;
}
}
else
{
await DebugMessage("No pass");
}
} catch(Exception e)
{
await DebugMessage(e.ToString());
}
}
完整代码:
https://pastebin.com/RrcBTdr0
我从输出调试中得到的错误:
Exception thrown: 'System.NullReferenceException' in discbot.dll
Exception thrown: 'System.NullReferenceException' in System.Private.CoreLib.dll
在控制台上我得到:
26.6.2019 17.20.10 at Gateway A ReactionAdded handler has thrown an unhandled exception.
我希望当将特定反应添加到 "Ticket"
时,机器人会向特定频道发送消息
没有 "Client.MessageReceived += Client_CheckTicket" 和 "Client.ReactionAdded += OnReactionAdded;" 其他代码工作正常。但是当我把它们加回去时,东西就刹车了……
注意:我没有在 OnReactionAdded 上收到 DebugMessages,所以执行是否有问题?
private async Task OnReactionAdded(Cacheable<IUserMessage, ulong> cache, ISocketMessageChannel channel, SocketReaction thereaction)
{
await DebugMessage("React func started");
难道当我assign/create一个新变量时它会因为内存占用而覆盖另一个变量?
MessageRecever(我打错了):
private async Task Client_MessageReceived(SocketMessage arg)
{
var Message = arg as SocketUserMessage;
await ReactAsync(Message);
var Context = new SocketCommandContext(Client, Message);
if (Context.Message == null || Context.Message.Content == "") return;
if (Context.User.IsBot) return;
int ArgPos = 0;
if (!(Message.HasStringPrefix("nuked!", ref ArgPos) || Message.HasMentionPrefix(Client.CurrentUser, ref ArgPos))) return;
var Result = await Commands.ExecuteAsync(Context, ArgPos, null);
if (!Result.IsSuccess)
{
Console.WriteLine($"{DateTime.Now} at Commands Something went wrong with executing a nuke launch. Text: {Context.Message.Content} | Error: {Result.ErrorReason}");
await Context.Channel.SendMessageAsync($"{DateTime.Now} at Commands Something went wrong with executing a nuke launch. Text: {Context.Message.Content} | Error: {Result.ErrorReason}");
}
}
我已经尝试更多地定位问题,我认为它就在这里:
using (StreamReader sr = File.OpenText(path))
{
try
{
while ((s = sr.ReadLine()) != null)
{
DebugMessage("Checking this message: " + Message.Content);
if (Context.Message.Content.Contains(s))
{
messageid = Message.Id;
DebugMessage("added");
}
}
}
catch (Exception e)
{
DebugMessage("Something went wrong... Exception was sent to console!");
Console.WriteLine(e);
return;
}
sr.Close();
}
最后我得到了一些更具体的错误:
System.NullReferenceException: Object reference not set to an instance of an object.
at discbot.Program.DebugMessage(String a) in C:\Users\Alex\source\repos\discbot\discbot\Program.cs:line 103
at discbot.Program.Client_CheckTicket(SocketMessage arg) in C:\Users\Alex\source\repos\discbot\discbot\Program.cs:line 222
第 103 行:await Client.GetGuild(593114638770634763).GetTextChannel(593430696874213384).SendMessageAsync(a);
第 222 行:await DebugMessage("No pass");
是的,这是一个 nullreference 错误,有很多答案,但我无法弄清楚..
Console.WriteLine(a);
await Client.GetGuild(593114638770634763).GetTextChannel(593430696874213384).SendMessageAsync(a);
WriteLine 有效,但 SenmessageAsync returns NullReferenceException.. 我检查了我的服务器 ID 和我的频道 ID 是正确的...
所以问题出在:
public async Task DebugMessage(string a)
{
Console.WriteLine(a);
await Client.GetGuild(593114638770634763).GetTextChannel(593430696874213384).SendMessageAsync(a);
}
空错误发生在:
Client.GetGuild(593114638770634763).GetTextChannel(593430696874213384).SendMessageAsync(a);
因为当我执行 await DebugMessage("No pass");
时 Console.WriteLine
有效但第二个无效?知道为什么它不起作用吗?
字符串 "a" 不是原因,因为我用这个替换了 a 变量:
await Client.GetGuild(593114638770634763).GetTextChannel(593430696874213384).SendMessageAsync("ok");
还是不行...
如果您仍然不明白哪个部分坏了,那就是:
await Client.GetGuild(593114638770634763).GetTextChannel(593430696874213384).SendMessageAsync(The variable is not causing this...);
现在我这样设置:
public async Task DebugMessage(string a)
{
Console.WriteLine("Getting server");
var server = Client.GetGuild(593114638770634763);
Console.WriteLine("Getting chat");
var chat = server.GetTextChannel(593430696874213384);
Console.WriteLine("Writing to Chat");
await chat.SendMessageAsync(a);
Console.WriteLine("Succeeded");
}
它失败了:
await chat.SendMessageAsync(a);
"Because chat is null... most likely because your ID is incorrect and no channel with a matching ID can be found in that guild." -Anu6is
这有效....
所以我正在为我的 Discord 服务器创建一个 Discord Bot,但我不知道为什么它不起作用。我认为问题出在我的 ReactionHandler 中,但我不确定是什么原因造成的?
我试图修改一些代码..但我无法弄清楚所以我想问问是否有人知道我的代码有什么问题......我的代码可能很乱,但我希望它不介意你。我不会展示我的 "Commands" 脚本,因为它与这个问题无关!
private async Task Client_CheckTicket(SocketMessage arg)
{
try
{
var Message = arg as SocketUserMessage;
var Context = new SocketCommandContext(Client, Message);
if (Context.Channel.Id == debugchannel)
{
Console.WriteLine("Detected a debug message i wont be taking any actions against it");
return;
}
if (Context.User.IsBot)
{
String s = "";
String path = @"C:\Users\Alex\source\repos\discbot\ids.txt";
using (StreamReader sr = File.OpenText(path))
{
try
{
while ((s = sr.ReadLine()) != null)
{
await DebugMessage("Checking this message: " + Message.Content);
if (Context.Message.Content.Contains(s))
{
messageid = Message.Id;
await DebugMessage("added");
}
}
}
catch (Exception e)
{
await DebugMessage("Something went wrong... Exception was sent to console!");
Console.WriteLine(e);
return;
}
sr.Close();
}
if (messageid != 0)
{
path = @"C:\Users\Alex\source\repos\discbot\messageid.txt";
using (StreamWriter sr = File.AppendText(path))
{
sr.WriteLine(messageid);
sr.Close();
}
}
else
{
await DebugMessage("MessageID was 0 so i will not add it to my database");
return;
}
}
else
{
await DebugMessage("No pass");
}
} catch(Exception e)
{
await DebugMessage(e.ToString());
}
}
完整代码: https://pastebin.com/RrcBTdr0
我从输出调试中得到的错误:
Exception thrown: 'System.NullReferenceException' in discbot.dll
Exception thrown: 'System.NullReferenceException' in System.Private.CoreLib.dll
在控制台上我得到:
26.6.2019 17.20.10 at Gateway A ReactionAdded handler has thrown an unhandled exception.
我希望当将特定反应添加到 "Ticket"
时,机器人会向特定频道发送消息没有 "Client.MessageReceived += Client_CheckTicket" 和 "Client.ReactionAdded += OnReactionAdded;" 其他代码工作正常。但是当我把它们加回去时,东西就刹车了……
注意:我没有在 OnReactionAdded 上收到 DebugMessages,所以执行是否有问题?
private async Task OnReactionAdded(Cacheable<IUserMessage, ulong> cache, ISocketMessageChannel channel, SocketReaction thereaction)
{
await DebugMessage("React func started");
难道当我assign/create一个新变量时它会因为内存占用而覆盖另一个变量?
MessageRecever(我打错了):
private async Task Client_MessageReceived(SocketMessage arg)
{
var Message = arg as SocketUserMessage;
await ReactAsync(Message);
var Context = new SocketCommandContext(Client, Message);
if (Context.Message == null || Context.Message.Content == "") return;
if (Context.User.IsBot) return;
int ArgPos = 0;
if (!(Message.HasStringPrefix("nuked!", ref ArgPos) || Message.HasMentionPrefix(Client.CurrentUser, ref ArgPos))) return;
var Result = await Commands.ExecuteAsync(Context, ArgPos, null);
if (!Result.IsSuccess)
{
Console.WriteLine($"{DateTime.Now} at Commands Something went wrong with executing a nuke launch. Text: {Context.Message.Content} | Error: {Result.ErrorReason}");
await Context.Channel.SendMessageAsync($"{DateTime.Now} at Commands Something went wrong with executing a nuke launch. Text: {Context.Message.Content} | Error: {Result.ErrorReason}");
}
}
我已经尝试更多地定位问题,我认为它就在这里:
using (StreamReader sr = File.OpenText(path))
{
try
{
while ((s = sr.ReadLine()) != null)
{
DebugMessage("Checking this message: " + Message.Content);
if (Context.Message.Content.Contains(s))
{
messageid = Message.Id;
DebugMessage("added");
}
}
}
catch (Exception e)
{
DebugMessage("Something went wrong... Exception was sent to console!");
Console.WriteLine(e);
return;
}
sr.Close();
}
最后我得到了一些更具体的错误:
System.NullReferenceException: Object reference not set to an instance of an object. at discbot.Program.DebugMessage(String a) in C:\Users\Alex\source\repos\discbot\discbot\Program.cs:line 103 at discbot.Program.Client_CheckTicket(SocketMessage arg) in C:\Users\Alex\source\repos\discbot\discbot\Program.cs:line 222
第 103 行:await Client.GetGuild(593114638770634763).GetTextChannel(593430696874213384).SendMessageAsync(a);
第 222 行:await DebugMessage("No pass");
是的,这是一个 nullreference 错误,有很多答案,但我无法弄清楚..
Console.WriteLine(a);
await Client.GetGuild(593114638770634763).GetTextChannel(593430696874213384).SendMessageAsync(a);
WriteLine 有效,但 SenmessageAsync returns NullReferenceException.. 我检查了我的服务器 ID 和我的频道 ID 是正确的...
所以问题出在:
public async Task DebugMessage(string a)
{
Console.WriteLine(a);
await Client.GetGuild(593114638770634763).GetTextChannel(593430696874213384).SendMessageAsync(a);
}
空错误发生在:
Client.GetGuild(593114638770634763).GetTextChannel(593430696874213384).SendMessageAsync(a);
因为当我执行 await DebugMessage("No pass");
时 Console.WriteLine
有效但第二个无效?知道为什么它不起作用吗?
字符串 "a" 不是原因,因为我用这个替换了 a 变量:
await Client.GetGuild(593114638770634763).GetTextChannel(593430696874213384).SendMessageAsync("ok");
还是不行...
如果您仍然不明白哪个部分坏了,那就是:
await Client.GetGuild(593114638770634763).GetTextChannel(593430696874213384).SendMessageAsync(The variable is not causing this...);
现在我这样设置:
public async Task DebugMessage(string a)
{
Console.WriteLine("Getting server");
var server = Client.GetGuild(593114638770634763);
Console.WriteLine("Getting chat");
var chat = server.GetTextChannel(593430696874213384);
Console.WriteLine("Writing to Chat");
await chat.SendMessageAsync(a);
Console.WriteLine("Succeeded");
}
它失败了:
await chat.SendMessageAsync(a);
"Because chat is null... most likely because your ID is incorrect and no channel with a matching ID can be found in that guild." -Anu6is 这有效....