dictionary - C# - Code not running and I can't figure out why -
i'm creating discord bot using discord.net, don't think matters here, , i'm trying checks dictionaries , i've run problem code doesn't run , can figure out why. let me try , summarize code.
private dictionary<server, dictionary<user, chatterbotsession>> sessions = new dictionary<server, dictionary<user, chatterbotsession>>(); if (sessions.any()) { if (sessions[e.server].any()) { await e.channel.sendmessage("sorry, i'm talking on server.").configureawait(false); return; } } chatterbotfactory factory = new chatterbotfactory(); chatterbot.chatterbot bot = factory.create(); chatterbotsession session = bot.createsession(); dictionary<user, chatterbotsession> sessiondic = new dictionary<user, chatterbotsession>(); sessiondic.add(e.user, session); sessions.add(e.server, sessiondic); await e.channel.sendmessage("added dicionary entry server '" + e.server.name + "' , user '" + e.user.name + "'");
so when code first gets executed, goes through , sends message "await e.channel.sendmessage(...);". , if execute code on same discord server, recognizes server in dictionary , stops @ "already talking someone" message. that's fine , dandy. if go server, goes through "sessions.any()" if , "if(sessions[e.server].any())" , since isn't server, doesn't go if, stops! way see it, should continue outside "sessions.any()" if.
what missing here?
i manage find solution. got suggestion wrap try , catch , found out key not present. reason, didn't tell me that. checked if key present , i'm handling better rest of code getting executed.
here's of updated code.
private dictionary<server, chatsession> sessions = new dictionary<server, chatsession>(); if (sessions.any()) { if (sessions.containskey(e.server)) { if(sessions[e.server].user == e.user) { await e.channel.sendmessage(e.user.mention + " i'm talking you! :p"); return; } else { await e.channel.sendmessage(e.user.mention + " i'm talking here. try again later."); return; } } else { await e.channel.sendmessage("this server not stored, continuing."); } }
Comments
Post a Comment