Refactoring a Python Bot [Live Coding Recap]

ninjabunny9000

šŸ’¾ bun9000

Posted on November 3, 2019

Refactoring a Python Bot [Live Coding Recap]

Streamed: 10/29 on Twitch

Topic(s): We hung out & refactored DeepThonk (the python bot's) code!
Language: Python, JSON
Tech & libs used: VSCode, TwitchIO

šŸ‘‡ You can find the notes and code from stream down below! :D

ā­ Star ā‡’ Project Repository
šŸ’œ Follow ā‡’ on Twitch and GitHub
šŸ”“ Watch ā‡’ Twitch VOD (replay)

During the stream we...

āœ” moved !project from FAQ's into it's own command
āœ” made !project settable from chat
āœ” moved all other commands to commands.py
āœ” cleaned up commands & commented uncommented funcs
āœ” created a command module and moved all commands in there
āœ” created a command to enable/disable shoutouts
āœ” created a !raider command to thank (and shoutout) raiders!

Here's some of the code we wrote...

Shoutouts can be a little too much sometimes when we're actively developing the bot, so we created a chat function to turn them on or off on the fly.

# in commands.cmds
@bot.command(name='shoutouts')
async def shoutouts(ctx):
    'toggles shoutouts on or off'
    if data.get_setting('shoutouts'):
        data.set_setting('shoutouts', False)
        await ctx.send('Shoutouts are off now.')
    else:
        data.set_setting('shoutouts', True)
        await ctx.send('Shoutouts are back on!')
Enter fullscreen mode Exit fullscreen mode

And, at the recommendation of chat, we created a command to shout out (and thank) raiders.

# in commands.cmds
@bot.command(name='raider')
async def raider(ctx):
    'shouts & thanks raiders'
    token = ctx.content.split(' ', 1)
    if token[1][0] == '@':  # strip @'s if they passed in user name arg with one
        raider = token[1][1:]
    else:
        raider = token[1]
    msg = f"ninjab1Bigups to @{raider} for the sick raid! Check them out at https://twitch.tv/{raider}"
    await ctx.send(msg)
Enter fullscreen mode Exit fullscreen mode
šŸ’– šŸ’Ŗ šŸ™… šŸš©
ninjabunny9000
šŸ’¾ bun9000

Posted on November 3, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related