user11773594
user11773594

Reputation:

How can I get my discord bot to mention a persons name then say correct after they guess a variable in the form of typing it in the chat

I don't have any code for this function but I have my bot in which I can implement this code into. I'm new to programming.

            import discord

            client = discord.Client()

            @client.event
            async def on_ready():
                print('We have logged in as {0.user}'.format(client))

            @client.event
            async def on_message(message):
                if message.author == client.user:
                    return

                if message.content.startswith('$hello'):
                    await message.channel.send('Hello!')

            client.run('my token is here')

Upvotes: 0

Views: 1809

Answers (1)

Researcher
Researcher

Reputation: 1046

As a new programmer I would advise breaking down your problem into logical steps and trying to solve them separately. You should also need define everything exactly, including all assumptions.

Once you've figured this out, attacking the individual pieces is a little easier. And this is where you want to read documentation.

Before you begin any of this though I would suggest reading an overview of programming concepts. Specifically:

  • Variables: including types such as integer, string, float, datetime etc
  • Operations on variables: - +,-,*,/
  • Objects
  • Operations on basic objects: len,append
  • Collections: list and dictionary
  • Boolean Logic - if statements, and, or
  • Looping for repetitive tasks - for statement, while statement
  • Functions

Overall System Design

A discord bot acts like a discord user, and thus can be connected to multiple servers (guilds) and channels at once. The event we are using from the API give you a message that could be from any guild or channel.

Singleplayer Verison

  1. Bot awaits user typing "!game". Records the IDs of guild, channel and user
  2. Output a message saying "Pick number between 1-10".
  3. Randomly generate a variable between 1-10. Store this in a collection which you can lookup with the 3 IDs from step 1.
  4. Scan for that users response. This will need validations to make sure they only enter number. This only triggers if game is actually running in a specific guild/channel.
  5. Compare user's response using if statement
  6. If they win announce game is over and delete the game from the collection.
  7. Otherwise tell them to try again.

Here's the documentation you would need to answer this question:

https://github.com/reactiflux/discord-irc/wiki/Creating-a-discord-bot-&-getting-a-token

https://discordapp.com/developers/docs/intro

https://discordpy.readthedocs.io/en/latest/api.html#message

Aaaaaaand here's the code.

import discord
import random

client = discord.Client()

players = dict()

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    # Check https://discordpy.readthedocs.io/en/latest/api.html#message
    print("Guild ID: ", message.guild.id)
    print("Channel ID: ", message.channel.id)
    print("User ID: ", message.author.id)

    # Unique set of properties for each game instance
    gameInstance = (message.guild.id, message.channel.id, message.author.id)

    # Bot can't talk to itself
    if message.author == client.user:
        return

    # Detect if user wants to play game
    if message.content == "!game":
        # Is user already playing game?
        if (gameInstance) in players:
            await message.channel.send("You are already playing game friend.")
            return

        # Generate random number
        randomNumber = random.randint(1, 10)

        # Save random number in dictionary against that game instance
        players[gameInstance] = randomNumber

        # Ask user for guess
        await message.channel.send("You want to play game?! Please enter a number between 1 and 10.")
        return

    # We only get here if user not entering "!game"
    if gameInstance in players:
        # Check if message only contains digits then convert it to integer and check guess
        # If match, we delete the game instance.
        if message.content.isdigit():
            guess = int(message.content)
            if guess == players[gameInstance]:
                del players[gameInstance]
                await message.channel.send("You Win!")
            else:
                await message.channel.send("Try, try, try, try, try again.")
        else:
            await message.channel.send("That's not a number.")    


token = "YOUR TOKEN"
client.run(token)

Upvotes: 1

Related Questions