Just-Some-Bots / MusicBot

:musical_note: The original MusicBot for Discord (formerly SexualRhinoceros/MusicBot)
https://just-some-bots.github.io/MusicBot/
MIT License
3.1k stars 2.35k forks source link

Append custom commands? #114

Closed osilkin98 closed 8 years ago

osilkin98 commented 8 years ago

I want to be able to create and append my own commands to the bot, and was wondering how to add custom commands that are recognized by the bot. I created a tester command called handlequote, assuming that the part after "handle" is what actually gets recognized as a callable function.

async def handle_quote(self):
        sheet = open(r'path:\to\file.txt', 'r')
        hold = sheet.read()
        sheet.close()
        qtsT = tuple(hold.split(';'))
        qt = choice(qtsT)
        msg = "Random Quote:\n*" + qt + "*"
        return Response(qt, reply=True, delete_after=60)

Is there someplace to specify the keyword for the function name within the code? I'm not too big on Python Object Oriented programming, let alone event handlers so help would be greatly appreciated.

imayhaveborkedit commented 8 years ago

Functions that start with handle_ are considered commands, yes (this is going to change though). That bit of code should work, but you returned the quote, not the message with the quote. It could be rewritten like this:

async def handle_quote(self):
    '''
    copypasta a docstring and edit it
    '''
    with open(r'path:\to\file.txt', 'r') as sheet:
        hold = sheet.read()

    msg = "Random Quote:\n* %s *" % choice(hold.split(';'))
    return Response(msg, reply=True, delete_after=60)
osilkin98 commented 8 years ago

Thanks for pointing that out, I actually didn't catch that as it's been a while since I've touched Python. Now that I have the command in place, how would I go about actually making it callable? Since when I enter !quote, it still doesn't render it as a command. I've noticed that it doesn't update the code since I've tried to debug it and add print() functions within certain parts that it should have reached and executed, but didn't.

What I'm asking I guess, is how would I recompile the code in order to implement any new command created.

imayhaveborkedit commented 8 years ago

You shouldn't need to do anything really, other than save the file and restart the bot.

osilkin98 commented 8 years ago

I did that however the functions still act the same from before I even started editing. I verified this by editing the messages of other commands and called them after restarting, and the text displayed was still the same.

imayhaveborkedit commented 8 years ago

You're going to have to figure out what you're doing wrong on your end. Editing the wrong file, running the wrong thing, etc

osilkin98 commented 8 years ago

Well I was finally able to get it to work after I nuked it as suggested. I'm not sure what the issue was though the first time around, I was editing in eclipse so I'm 100% sure that the error was that I created a copy of the project in the IDE instead of linking it which is what I usually do. I retried the process after nuking it and resolving some indentation errors, and was able to get the command to execute as specified. Thank you!