archangelic / pinhook

the pluggable python framework for IRC bots and Twitch bots
https://archangelic.github.io/pinhook/
MIT License
31 stars 4 forks source link

Command Prefix Parameter #52

Closed ixxie closed 5 years ago

ixxie commented 5 years ago

If multiple pinhook bots sharing ops are in the same room, conflicts could arise if commands are shared. While plugin command prefixes are controlled by the user, the prefixes for the builtins !join, !help, !reload and most critically !quit are hardcoded.

Doing something like:

class Bot(irc.bot.SingleServerIRCBot):
    def __init__(self, channels, nickname, server, cmd_prefix='!', **kwargs):

        ...    

       self.cmd_prefix = cmd_prefix

        ...

    def call_internal_commands(self, channel, nick, cmd, text, arg, c):
        output = None
        if nick in self.ops:
            op = True
        else:
            op = False
        if cmd == self.cmd_prefix + 'join' and op:
            c.join(*arg.split())
            self.logger.info('joining {} per request of {}'.format(arg, nick))
            output = self.output_message('{}: joined {}'.format(nick, arg.split()[0]))
        elif cmd == self.cmd_prefix + 'quit' and op:
            self.logger.info('quitting per request of {}'.format(nick))
            c.quit("See y'all later!")
            quit()
        elif cmd == self.cmd_prefix + 'help':
            output = self.call_help()
        elif cmd == self.cmd_prefix + 'reload' and op:
            self.logger.info('reloading plugins per request of {}'.format(nick))
            self.load_plugins()
            output = self.output_message('Plugins reloaded')
        return output

This would resolve this at least for the builtins, but I am not sure how you would like to handle plugin commands. Ideally, this prefix would be applied to them as well, but of course this change is not backwards compatible with existing plugins (which is why I didn't submit a PR).

archangelic commented 5 years ago

Yeah, this is a problem i've been thinking about for a while. I think the solution might be to require mentioning the bot pinhook: !quit that way it is very explicit without having to remember the command prefix.

ixxie commented 5 years ago

Normally during the lifetime of a bot being used in a channel the prefix would seldom be changed so it would be easy to remember. Personally consistency across commands combined with the ability to change the prefix globally would be valuable, so I could deploy a pinhook bot to a channel which already has one.

Note how this helps when I want to deploy my bot to multiple channels where I want different prefixes:

bots = [
  Bot(
    channels=['#philosophy', '#nixos'],
    nickname='seriousbot',
    cmd_prefix='>'
    server='irc.freenode.net'
  ),
  Bot(
    channels=['#discord', '#fnord'],
    nickname='sillybot',
    cmd_prefix='.',
    server='discord.maddshark.com'
  )
]
for bot in bots:
    bot.start()
archangelic commented 5 years ago

Okay, I can totally see and respect that. how does adding a cmd_prefix and also for backwards compatibility making a use_prefix_for_plugins that will default to False for the current version of pinhook?

ixxie commented 5 years ago

That sounds like a great solution! Would you like me to make a PR or would you rather do it yourself?

archangelic commented 5 years ago

If it's all the same, I am going to do this because it gives me the excuse to change some stuff in __init__ I have been putting off 😝

ixxie commented 5 years ago

Less hassle for me ^^ thanks for responding so swiftly!

archangelic commented 5 years ago

This is now fixed and added in pinhook v1.6.0!