johnmaguire / Cardinal

A Python IRC bot, designed to make adding functionality quick and simple. (est. 2013)
MIT License
100 stars 38 forks source link

Wiki decorator examples missing imports #114

Closed zerocarbthirty closed 8 years ago

zerocarbthirty commented 8 years ago

Hi, so I've been struggling to write what I think is a very simple plugin which simply opens a different URL depending on what command is received in IRC so if someone types !thingy it will open URL/thingy if they type whee it will open URL/whee (or whatever urls i define per command). it's a way to tie IRC into an API basically.

anyway, I can't get it to work at all so I tried using the code from the wiki:

class HelloWorldPlugin(object):
    @command(['hello', 'hi'])
    @help("Responds to the user with a greeting.")
    @help("Syntax: .hello [user to greet]")
    def hello(self, cardinal, user, channel, msg):
        nick, ident, vhost = user.group(1), user.group(2), user.group(3)
        cardinal.sendMsg(channel, "Hello %s!" % nick)

def setup():
    return HelloWorldPlugin()

that code above actually doesn't work apparently you have to add : from cardinal.decorators import command, help to the top of it in order for @command and @help to function but even with that in there typing hello or hi in IRC doesn't do anything.

I also tried simply modifying the 'ping' plugin as such:

from cardinal.decorators import command, help
class zctPlugin(object):
    @command(['wonka', 'crazy', 'pup'])
    def zct(self, cardinal, user, channel, msg):
            cardinal.sendMsg(channel, "%s: Pong." % user.group(1))
def setup():
    return zctPlugin()

anyway, if anyone knows what i'm doing wrong I would greatly appreciate it.

The thing I can't figure out is why the default "ping" command works fine.. driving me crazy

zerocarbthirty commented 8 years ago

Hi, I think I've gotten a little further along in this the only thing I need to know now is how i can use the command that called the plugin in the plugin for example say you have @command(['wonka', 'crazy']) how can I use that like this:

cardinal.sendMsg(channel, "%s: The command issued was" % command)

johnmaguire commented 8 years ago

Thanks for pointing out that the decorator imports are not documented on the wiki. I will update it tonight.

To see what command was called, you should inspect msg. The msg variable contains the complete message that the command triggered on. An easy way of getting only the command (including the . prefix) is to use command = msg.split(' ')[0]. Then just remove the first character to see it without the .

However, give this page another read: https://github.com/JohnMaguire/Cardinal/wiki/Writing-Plugins

You'll notice that the @command decorator is specifically for creating commands with the . prefix. To respond to a custom string, you'll probably want to look at this section here: https://github.com/JohnMaguire/Cardinal/wiki/Writing-Plugins#responding-to-regular-expressions

And since you mentioned the ping plugin, here's the relevant line: https://github.com/JohnMaguire/Cardinal/blob/master/plugins/ping/plugin.py#L8

zerocarbthirty commented 8 years ago

thanks, I ended up changing the . to a ! because . appears to be a 'special character' to twitch's chat

johnmaguire commented 8 years ago

:+1: I'd like to make that a configurable option one day. Thanks for pointing out a use case where it's actually necessary!