-4

So I usually code in C++ and this is my first time using python. I'm trying to code a Discord bot and I'm following along with a tutorial. It said to add this line to the top of my code

py -3 -m pip install -U discord.py

I get four syntax errors under pip and discord.py all saying "unexpected token". I looked at multiple other tutorials and the one I'm following along with and none of them mention this. I'm using python version 3.7.8 so I'm pretty sure that that's not the issue. I tried switching to the Repl.it IDE and following a different tutorial that told me to use

import discord

That didn't work for reasons I understood less than this. All and all this is what my code looks like

py -3 -m pip install -U discord.py

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 bot's token)

I have no clue what I'm doing wrong.

1 Answers1

0

Make sure you have pip installed, which you can do in your terminal. If you're on Unix/macOS, try python -m pip install -U pip. If you're on Windows, try py -m pip install -U pip. Then, at the beginning of your code, add this:

try:
    import discord
except ImportError:
    import pip
    pip.main(['install', 'discord'])
    import discord
Aditya Tomar
  • 1,601
  • 2
  • 6
  • 25