spark-c / SparksieBOT

A Discord bot that provides upkeep, utility, and some fun commands for my personal discord server.
1 stars 0 forks source link

Abstract implementation of commands into separate non-Discord files. #16

Open spark-c opened 3 years ago

spark-c commented 3 years ago

I'd like to add tests to this project. However, setting everything up as-is would require one of two (undesirable) paths forward:

  1. Building a second bot to feed input and test responses,
  2. Using another testing library ( like dpytest ) which is still in development and not clearly documented yet

I prefer a third option, which is to move all of the "work" code into non-discord, easily-testable files. For example:

# before
@commands.command()
async def add_numbers(self, ctx, *args):
    result = sum(args)
    ctx.channel.send(f"The sum is {result}")

# after
import myCommands

@commands.command()
async def add_numbers(self,  ctx, *args):
    result = myCommands.add_numbers(args)
    ctx.channel.send(f"The sum is {result}")

With the latter implementation, we can now implement test_add_numbers() without needing to worry about any of the async/websocket Discord api stuff.