gpt4 book ai didi

c# - 有人知道为什么我在尝试处理 Reaction 时会收到此 NullReferenceException 错误

转载 作者:太空宇宙 更新时间:2023-11-03 16:58:39 30 4
gpt4 key购买 nike

<分区>

所以我正在为我的 Discord 服务器创建一个 Discord Bot,但我不明白为什么它不起作用。我认为问题出在我的 ReactionHandler 中,但我不确定是什么原因造成的?

我试图修改一些代码..但我无法弄明白所以我想问问是否有人知道我的代码有什么问题......我的代码可能很乱,但我希望它不介意你。我不会展示我的“命令”脚本,因为它与这个问题无关!

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.

我希望当将特定 react 添加到“票证”时,机器人会向特定 channel 发送消息

没有“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");

会不会是当我分配/创建一个新变量时,它会因为内存使用而覆盖另一个变量?

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 返回 NullReferenceException。我检查了我的服务器 ID 和我的 channelid 是正确的...

所以问题出在:

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 变量:等待 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);

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com