1

I have looked at others code and haven’t figured it out, what I want is something that will send a message whenever a number is sent, every time said number is sent the number gets increased by one. Here is my code:

counter = 0

if message.channel.startswith(counter):
    await message.channel.send("test, this will be changed later")
    counter +=1
Dank Darko
  • 11
  • 1
  • 3
  • 1
    Hi, is this a chatbot you are trying to build? – IODEV Apr 19 '21 at 08:08
  • 2
    What's going wrong? Please provide an error or what happens differently than expected. – 12944qwerty Apr 19 '21 at 20:04
  • No errors are happening, the bot responds to me saying “1” (which it didnt before) but still doesnt do anything for me saying 2, 3 or any of that. Oh yea, I also changed counter = 0 to counter = 1 – Dank Darko Apr 20 '21 at 06:45

4 Answers4

0

You might need to convert to string first....

counter = 0

if message.channel.startswith(str(counter)):
    await message.channel.send("test, this will be changed later")
    counter += 1
0

What does this return to the channel?

counter = 0

await channel.send(f'Start waiting for counter: {counter}')

if message.channel.startswith(str(counter)):
    counter += 1
    await channel.send(f'waiting for new counter: {counter}')
IODEV
  • 1,706
  • 2
  • 17
  • 20
0

Ok, now i know what your problem is. You're using discord.py and everytime when you write a message you trigger the event onMessage so you create everytime a new Variable named "counter" with 0 and then you increase it to 1 but the counter int is never saved. A fix for this is some kind of data source. You could try to save the number everytime e.g. in a txt file. That should make it work (Code):

counter = 0

# What the Message should start with
if message.content.startswith('Text'):
    # Try to read the file, if it is not existing create it with 0 as number in it.
    try:
        f = open("counter.txt", "r")
        # Set the content of the text file as counter int
        counter = int(f.readline())
    except:
        f = open("counter.txt", "a")
        f.write(str(0))

    # Add a Number to the Counter
    counter += 1
    # Overwrite the current Number with the New Number
    f = open("counter.txt", "w")
    f.write(str(counter))

    # Send a Message with a certain String (Test! in this case and the Int 
    # that got counted up)
    await message.channel.send('Test! ' + str(counter))
mlhmz
  • 47
  • 5
  • well that works, BUT no matter what command i do, even if its just a simple respond to a message thing i always get this error at the end of the if statement: Unindent does not match any other indentation level – Dank Darko Apr 20 '21 at 18:43
  • Could it be that your def Function is not async? You may have to put a `async` before the function and when you're sending a Message you need to put an `await` before it. – mlhmz Apr 20 '21 at 20:14
  • Im pretty sure it is async, my code matches what your saying – Dank Darko Apr 21 '21 at 06:15
  • just found the error, one line in my code was a space off which messed up my code only like 30 lines later, weird – Dank Darko Apr 21 '21 at 06:38
-2

+= doesnt work in Python, use "var = var + 1" instead

mlhmz
  • 47
  • 5