因此,我正在为Discord Server创建Discord Bot,但我不知道为什么它不起作用。我认为问题出在我的ResponseHandler中,但是我不确定是什么原因造成的?
我试图修改一些代码。但是我无法弄清楚,所以我想问别人是否知道我的代码有什么问题...我的代码可能很乱,但是我希望不介意你。我不会显示“命令”脚本,因为它与该问题无关!
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
我从输出调试中得到的错误:
引发的异常:discbot.dll中的'System.NullReferenceException'
抛出异常:System.Private.CoreLib.dll中的'System.NullReferenceException'
在控制台上,我得到:
26.6.2019 17.20.10,网关A ReactionAdded处理程序引发了未处理的异常。
我希望当特定的反应添加到“票证”时,机器人会向特定的频道发送消息
没有“ 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:对象引用未设置为对象的实例。 在discbot.Program.DebugMessage(String a)在C:\ Users \ Alex \ source \ repos \ discbot \ discbot \ Program.cs:line 103 在C:\ Users \ Alex \ source \ repos \ discbot \ discbot \ Program.cs:line 222中的discbot.Program.Client_CheckTicket(SocketMessage arg)中
第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和通道ID是否正确...
因此问题出在
public async Task DebugMessage(string a)
{
Console.WriteLine(a);
await Client.GetGuild(593114638770634763).GetTextChannel(593430696874213384).SendMessageAsync(a);
}
并且null错误发生在:
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);
答案 0 :(得分:-1)
“因为聊天为空,很可能是因为您的ID不正确,并且在该行会中找不到匹配ID的频道。” -Anu6is 这有效。...