Code Monkey home page Code Monkey logo

math-game-bot's Introduction

Math Game Bot

A Discord bot for a simple math game, written using Discord.py and packaged with Poetry.

Per PROJECTS.md,

A Discord.py math game bot inspired by those math worksheets where students use a set of numbers, and write equations for every number, in say one through twenty. Made so I can grasp Discord bot development a little more, and just code some Python, which I haven't used to make any large projects for a while.

This Discord bot comes complete with its own interpreter, supporting basic operations and factorials.

The interpreter was made with help from CodePulse's Simple Math Interpreter series, with some added functionality on my part.

Contributing/Executing

Install all dependencies using poetry install after cloning the repository. You will need to configure your IDE with the virtual environment it generates, or it may not detect the installed dependencies, or execute the run script with poetry run python run.py directly.

If you plan to use run.py, the MATH_BOT_TOKEN environment variable must be set with your bot's token, which can be created and found on the Discord Developer Portal.

Thank you for contributing!

Using the Bot

Method 1

Add this application to your Discord server. You may not be able to add it to certain servers depending on the permissions you have.

The application will not respond to any commands unless it is currently being executed on my hardware.

Method 2

Clone the repository, and follow the instructions for contributing. After exporting the token and executing run.py, the bot should go online.

Exporting an environment variable using the export command only lasts for the duration of the terminal session it was used, meaning the token could be lost. You may want to consider creating a .env file to store the token.

Maintenance

I likely will stop committing new functionality to the bot after it is working, meaning I will not work on any issues or bug fixes.

math-game-bot's People

Contributors

dacoder101 avatar

Stargazers

 avatar  avatar

Watchers

Kostas Georgiou avatar  avatar

math-game-bot's Issues

Fix `game_max` functionality in `is_solution` method

def is_solution(self):
    """Check if an equation is equal to self.max."""

    return (
        eval(self.equation) == self.game.game_max
    )

This checks if the equation is equivalent to the game max, but it should allow any number from 0 to the game max.

Redesign the entire validation process

Redesign the entire validation process. The current string processing method is (as said in the file) a temporary, buggy, and half-baked solution to validate user input.

Create another interpreter dedicated simply to validating user input (support for lists, dictionaries, single integers, strings?)

#3 allows for negative solutions

def is_solution(self):
    """Check if an equation is equal to self.max."""

    return (
        eval(self.equation) <= self.game.game_max
    )  # Temporary, will create a custom parser later

Allows for a number less than 0, if the equation meets other requirements.

`ValueError` should be replaced with another exception

ValueError should be replaced with another exception, such as a custom exception for incorrect class arguments of some sort, or possibly another exception for an invalid equation solution, which could be caught by the command (when developed) or the MathGame class.

`Lexer` class should only except `Equation` type equations

Lexer class should only accept Equation type equations, which allows the class to be further utilized.

It is also a good idea to remove redundant equation checking from the equation class itself, as the lexer should do that automatically.

Translate possible user combinations into a dictionary

This issue outlines how the verification feature (verifying possible types of user input) will convert into a type dictionary before being supplied to the MathGame class. This is a general plan for myself on implementing this checking functionality, most likely integrated into a separate class and file.

Important. All values passed as user input will be of type string. Depending on various criteria will determine if that string is considered valid regardless of the fact it can be represented as a numerical value.

All keys must be between 0-9. All values must be greater than 0.

Key values will be supplied to a method that outputs a boolean value, representing if the integer key is between 0 and 9. Values will do the same, instead of checking if the integer is greater than 0.


If a string value supplied to the get_numerical_form method is representable as type float, it will return exactly that, otherwise raising an exception.

If a supplied value is just of type int, it will be checked via the key criteria and converted into the following dictionary: {x: 1}.


If a user were to supply a list-like format (such as [1, 2, 3]), each item would be appended to the dictionary's keys and given a value of the number of its occurrences. Various types will be handled.

Dictionaries will be handled "as is", only raising an exception if the key or value does not follow the key and value criteria. Types will be handled,


I've already thought of possible issues during the development of this and will add to this as needed.

I plan to add further ways of expressing this in the future, and some functionality may not be necessary if discord provides a simple way to do this automatically.

`ping` and `server_count` commands do not use `Embed`

@bot.tree.command(name="ping", description="Display the ping in milliseconds.")
async def ping(interaction: Interaction):
    """Display the ping in milliseconds."""

    latency = bot.latency * 1000
    await interaction.response.send_message(f"Pong! {latency:.2f}ms")

@bot.tree.command(
    name="server-count", description="Display the number of servers the bot is in."
)

This should return an Embed as it looks much cleaner on Discord GUI as opposed to plaintext.

@bot.tree.command(
    name="server-count", description="Display the number of servers the bot is in."
)
async def server_count(interaction: Interaction):
    """Display the number of servers the bot is in."""

    message = (
        f"I'm in {len(bot.guilds)} servers"
        if len(bot.guilds) > 1
        else f"I'm in {len(bot.guilds)} server"
    )

    await interaction.response.send_message(message)

server_count() should also return an Embed.

Default operators in `validate_operations` method do not correlate to their tokens

Default operators in validate_operations method do not correlate to their tokens. This isn't necessarily a problem now, or might not ever be an issue. However, operators should utilize a universal token throughout the entire project, the one to be used in the lexer and parser specifically. It should be a simple fix.

Possibly make a method that converts user operators into their tokens. This could be integrated into the lexer somehow.

It may be even nicer to instead of allowed operators, to instead to disallowed operators for simplicity. I want the defaults to include every possible operator and keyword anyway, so it seems cleaner.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.