开发者

SmartIRC4Net won't connect/shows no activity

开发者 https://www.devze.com 2023-03-27 18:26 出处:网络
I\'m making a bot in c#, using the SmartIRC4Net library (http://www.meebey.net/projects/smartirc4net/). If you aren\'t familiar with that library, feel free to tell me an alternative.

I'm making a bot in c#, using the SmartIRC4Net library (http://www.meebey.net/projects/smartirc4net/). If you aren't familiar with that library, feel free to tell me an alternative.

I used it because that is the most supported library I could find. I read the "test" example bot, and tried to strip it down to its basics by removing the querying and response input.

I programmed it to try to connect to their web channel for lack of a better test one, and it doesn't seem to connect. Nothing shows up on my client when I debug the bot (I'm on their channel right now). The console also doesn't show any IRC error message or exception, only the pause I put at the end. Code:

public static IrcClient irc = new IrcClient();

public static void Main(string[] args)
{

        //Setup
        irc.Encoding = System.Text.Encoding.UTF8;
        irc.SendDelay = 200;
        irc.ActiveChannelSyncing = true;

        //Event Handlers
        irc.OnError += new ErrorEventHandler(irc_OnError);
        irc.OnConnected += new EventHandler(irc_OnConnected);
        irc.OnRawMessage += new IrcEventHandler(irc_OnRawMessage);

        try
        {
            //Connect, log in, join channel
            irc.Connect("irc.freenode.org", 6667);
            irc.Login("HGPBot", "HGP Bot");
            irc.RfcJoin("#smartirc");
        }
        catch (Exception e)
        {
            Console.WriteLine("Could not connect, exception:" + Environment.NewLine
     开发者_开发知识库           + e.Message + Environment.NewLine
                + e.ToString());
        }

        //pause
        Console.WriteLine("Press any key to continue");
        Console.ReadKey(true);

        //Disconnect
        irc.Disconnect();

        //Exit
        Environment.Exit(0);
    }

    static void irc_OnRawMessage(object sender, IrcEventArgs e)
    {
        Console.WriteLine("irc_OnRawMessage initiated");
    }

    static void irc_OnConnected(object sender, EventArgs e)
    {
        Console.WriteLine("Connected");
        irc.SendMessage(SendType.Message, "#smartirc", "Connected");
    }

    static void irc_OnError(object sender, ErrorEventArgs e)
    {
        Console.WriteLine("IRC Error: " + e.ErrorMessage);
    }

[Update: Added irc_OnConnected event as suggested by @Russ C. The event is triggered and "Connected" is recorded on the console. Still, nothing happens on the channel. I will add a sendmessage line and see what happens.]

[Update2: Added SendMessage and OnRawMessage event. No output appears on the channel, and the text under the OnRawMessage event isn't written to console. (Am I using the right event for OnMessage? The "OnMessage" event doesn't exist, and the test bot says that OnMessage will "get all IRC messages".)]


Ok; like all event based logic (read Asynchronous logic here) you need to subscribe to an event so that the library will notify you when there's something to do. Because your test code isn't subscribing/attaching to any events from the SmartIRC library, the library is simply sitting still doing nothing.

You're doing part of it with the irc.OnError line, but you need to add these methods too:

irc.OnQueryMessage += new IrcEventHandler(OnQueryMessage);
irc.OnRawMessage += new IrcEventHandler(OnRawMessage);

Then a couple of methods:

// this method we will use to analyse queries (also known as private messages)
public static void OnQueryMessage(object sender, IrcEventArgs e)
{
    switch (e.Data.MessageArray[0]) {
        case "hello":
           // this is where you decipher private messages posted to the bot.
           // if someone does "/privmsg HGPBot hello" this will reply "Hello!"
           irc.SendMessage(SendType.Message, "HGPBot, "Hello!");
           break;
        default:
           break;
    }
}

// this method will get all IRC messages
public static void OnRawMessage(object sender, IrcEventArgs e)
{
    System.Console.WriteLine("Received: "+e.Data.RawMessage);
}

If you put a break point on this System.Console line, you should start seeing things coming through from the bot. If that doesn't seem to work, you can try making your own channel on the IRC server.

Also, don't forget: A user can be connected to IRC without being in a channel, if you're sure that the username your bot is using, is unique and is working (ie you can log in to it yourself via mirc or whatever) just trying sending a /privmsg command to your bot once the program appears to be connected.

edit: Also, I just noticed your program doesn't have a loop. You need to add irc.Listen(); before your pause statement. This will put the irc bot into listen mode and is a blocking loop, so the only way to quit your program at that point is to end the task, but at least it'll show you it working.

Edit 2: make the bot listen:

// here we tell the IRC API to go into a receive mode, all events
// will be triggered by _this_ thread (main thread in this case)
// Listen() blocks by default, you can also use ListenOnce() if you
// need that does one IRC operation and then returns, so you need then 
// an own loop 
irc.Listen();
//pause
Console.WriteLine("Press any key to continue");
Console.ReadKey(true);
0

精彩评论

暂无评论...
验证码 换一张
取 消