DeSinc / SallyBot

AI Chatbot coded in Discord.net C#
MIT License
299 stars 51 forks source link

sallybotMatch "System.NullReferenceException: 'Object reference not set to an instance of an object.'" when message contains bot name. #19

Closed Jakoski360 closed 1 year ago

Jakoski360 commented 1 year ago

image

Cloned repo and merged bot and guild variables from previously working commit 2 days ago.

sallybotMatch is null throwing System.NullReferenceException, continuing after break sends "Object reference not set to an instance of an object." to console output

ErnestoTheGamer commented 1 year ago

yea i encountered the same issue, the regex statement are not the best and at least for me do not work properly, which causes null to be returned.

I've made my own fix by writing some poorly written regex statements, hopefully it works for you too.


                // detect when a user types the bot name and a questionmark, or the bot name followed by a comma.
                // Examples: 
                // ok ->sally,<- it's go time. tell me a story
                // how many miles is a kilometre ->sallybot?<-
                // hey ->sallybot,<- are you there?
                bool sallybotMatch = Regex.IsMatch(inputMsg, @$"(?:.*{botName}.*\?.*|{botName}\s*,)", RegexOptions.IgnoreCase);

                bool startsWithSallybot = Regex.IsMatch(inputMsg, @$"(?:^{botName})", RegexOptions.IgnoreCase);

                bool shortSallybotMatch = Regex.IsMatch(inputMsg, @$"(?:.*{botName}.*)", RegexOptions.IgnoreCase) && Msg.Content.Length < 25;

                if (Msg.MentionedUsers.Contains(MainGlobal.Server.GetUser(botUserId))
                    || sallybotMatch // sallybot, or sallybot? query detected
                    || startsWithSallybot
                    || (lastLineWasSallyBot && Msg.Content.EndsWith("?")) // if last msg was sallybot and user followed up with question
                    || shortSallybotMatch) // or very short sentences mentioning sally```
DeSinc commented 1 year ago

Update your bot from the repo. I left old code in by mistake and fixed it just now

It was trying to grab nickname and when there was no nickname present it was failing.

Now it grabs nickname if present, and falls back to username instead if no nickname is set. It does this in the MainLoop.cs file. Make sure you update BOTH program.cs and mainloop.cs

Shizzkizz commented 1 year ago

Issue still seems to be happening even with the updated code. Will have to try again as it could be my end.

DeSinc commented 1 year ago

make sure you get all files, I updated every file almost except functions.cs pretty much.

I also updated it again like hours ago so download all files again if you haven't.

if you still get that object reference not set to instance of object error, send it here.

Jakoski360 commented 1 year ago

I'm not getting the issue anymore with the latest files.

DeSinc commented 1 year ago

thought so - fixed a lot of those stray bugs today. glad to hear it