#Error (Trying to make slash cmds in python)

158 messages · Page 1 of 1 (latest)

violet merlin
#

So i am trying to make slash commands for a bot this is my code: ``` @client.event
async def on_ready():
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name='To CodeFreak'))
print("Bot is now running")
try:
synced = await client.tree_sync()
print(f'{len(synced)} Commands')
except Exception as e:
print(e)
print('Worked')

@client.hybrid_command(description='Say hello to our bot!')
async def hello(ctx):
await ctx.send('Hi') ``` but it gave this error: File "main.py", line 15
try:
^
IndentationError: unindent does not match any outer indentation level

remote shell
#
    print("Bot is now running") # 4 space indent
  try: # 2 space indent
#

Need to have both with the same indents

violet merlin
#

wait ill try

remote shell
#

You got the rest of the code block indented with a 2 space indent after the print

#

You can't change the indentation randomly throughout your code

#

Python will throw the IndentationError for all of those

#

So, either change everything to use a 2 space indent, or a 4 space indent

violet merlin
#

But how

remote shell
#

What's your IDE set to do for indentations if you press tab?

#

Cause the first 2 lines are using a 4 space indent/tab

violet merlin
#

OH thx

#

huh

#

wait

#

wat

#

at wich do i need to tab

remote shell
#
 @client.event
async def on_ready():
    await client.change_presence([...]) # 4 space indent
    print("Bot is now running") # 4 space indent
  try: # 2 space indent, and everything below this is using a 2 space indent
    synced = await client.tree_sync()
    print(f'{len(synced)} Commands')
  except Exception as e:
    print(e)
    print('Worked')
#

Also weird leading space in your code, but I'm gonna assume that's due to copying the text to Discord

violet merlin
#

ye

remote shell
#

Does the code work now that you fixed your indentation?

violet merlin
#

it gaves the same error...

remote shell
#
def this_wont_work():
    print("something 1")
    print("something 2")

  if some_variable:
    print("something 3")

def this_will_work():
  print("something 1")
  print("something 2")

  if some_variable:
    print("something 3")
violet merlin
#

where do i need to copy that

remote shell
#

Copy what?

#

You'd have to fix the indentation in your code

#

Unindent everything, and do the indentation again

violet merlin
#

i deleted the whole thing

remote shell
#

You can't change between using a 2 space tab, to a 4 space tab

#

Python will throw an IndentationError every time you try to do that

violet merlin
#

so i need to type it again

remote shell
#

Or unindent the code so every single line doesn't have any indents, and indent everything again

violet merlin
#

okay

#

so how many times do i need to tab on each line

remote shell
#

Aka, go from this:

@client.event
async def on_ready():
    await client.change_presence([...])
    print("Bot is now running")
  try:
    synced = await client.tree_sync()
    print(f'{len(synced)} Commands')
  except Exception as e:
    print(e)
    print('Worked')

to this:

@client.event
async def on_ready():
await client.change_presence([...])
print("Bot is now running")
try:
synced = await client.tree_sync()
print(f'{len(synced)} Commands')
except Exception as e:
print(e)
print('Worked')

And do the indentation again, without changing the amount of spaces you use between lines

violet merlin
#

okey i did that

remote shell
#

You'd have to pick one single format for indentation

#

What indentation do you use for the rest of the code, 2 space or 4 space?

#

Also, you somehow had 5 spaces instead of 4

remote shell
#

Highlight the indents, and check

violet merlin
#

and then

remote shell
#

Is it 2 or 4 spaces?

violet merlin
#

4

remote shell
#

Then you'd use that for the code you had, not 2, and not 5

#

Does hitting tab also do 4 spaces?

violet merlin
remote shell
#

How'd you manage to get 4 space indents for the rest of the code then?

#

Did you hit tab twice each time?

violet merlin
#

uh no

remote shell
#

Then you accidentally have changed some setting in between when you wrote code to now

violet merlin
#

oh

#

and how do i fix that

remote shell
#

What IDE are you using?

violet merlin
#

?

#

im coding on replit

remote shell
#

Should be somewhere in the sidebar in the settings then

violet merlin
#

at tools?

#

Okey it worked

#

byt

#

but

#

now i have a other eror

#

oh wait

#

File "main.py", line 16
synced = await client.tree_sync()
^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: 'await' outside function

remote shell
#

Your try block is outside the function

#

Cause it's unindented

#

Indent those two into the function

violet merlin
#

so what now

remote shell
#

Indent the try/except blocks?

violet merlin
#

what

remote shell
#
async def function_1():
  print("Hello") #Inside the function
print("Not hello") #Outside the function
violet merlin
#

OHHHH

#

so i need to space 1 more time?

remote shell
#

Yeap

#

The try/except block seems to be properly indented, so you'd have to indent every single line of both of those once more

violet merlin
#

now i have the same error

#

the next line...

remote shell
#

Have you made functions before?

violet merlin
#

No

remote shell
#

Probably not a bad idea to take a look at how those work before jumping into a large project

violet merlin
#

and then?

remote shell
#

IndentationError = Your tab use is causing errors

#

What does the code look like?

violet merlin
#

@client.event
async def on_ready():
  await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name='To CodeFreak'))
  print("Bot is now running")
try: 
  synced = await client.tree_sync()
   print(f'{len(synced)} Commands')
except Exception as e:
   print(e)
   print('Worked')```
remote shell
#
@client.event
async def on_ready():
  await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name='To CodeFreak'))
  print("Bot is now running")
try: # Not inside the function
  synced = await client.tree_sync() # 3 spaces instead of 4, but inside the Try-block, but not in the function
   print(f'{len(synced)} Commands') # 4 spaces, inside the Try-block, but outside the function
except Exception as e: # Not inside the function
   print(e)
   print('Worked')
violet merlin
#

just copy that and past it?

remote shell
#

I'm showing you where you have errors...

violet merlin
#

oh oke

#

whats taht second error mean

remote shell
#

The try/except needs to be at the same level as the print()

#
def function_1():
  print("Hello from inside the function")

if something:
  print("Hello from the if-block outside the function")
#

The if-print() is not inside the function

#

But it's got correct indentation in the example I gave you

#
def function_1():
  print("Hello from inside the function")
  if something:
    print("Hello from the if-block inside the function")
#

Here the if-block is inside the function

violet merlin
#

why is this so hard

remote shell
#

Discord bots are intermediate projects

#

So if you don't know Python too well, you're gonna be struggling

violet merlin
remote shell
#

....they still use the same Python syntax, and how they're written

violet merlin
#

okay so now i only have the second error

remote shell
#

What's the second error?

violet merlin
#

4 spaces, inside the Try-block, but outside the function

#

that

remote shell
#

Indent it once more...

#

Look at the latest examples I sent

#

What's the difference between those?

violet merlin
#

the space

remote shell
#

How'd you change your code to make it match the examples?

violet merlin
#

Uh

#

idk 😭

remote shell
#

If-block outside the function:

def function_1():
  print("Hello from inside the function")
if something:
  print("Hello from the if-block outside the function")

If-block inside the function:

def function_1():
  print("Hello from inside the function")
  if something:
    print("Hello from the if-block inside the function")
violet merlin
#

i see the difference

#

but idk how i can change my code

remote shell
#

See the same difference in your own code?

violet merlin
remote shell
#
@client.event
async def on_ready():
  print("Bot is now running")

try: # Indent EVERYTHING after this
   synced = await client.tree_sync()
   print()
except:
   print()

See how the try: is on the same level as the async def?

#

You have to indent those once more...

violet merlin
#

YESSS

#

ITS WORKS

#

URGHH im so dumb

#

but i dont see the slash command in the server :/

#

oh i see why

#

'Bot' object has no attribute 'tree_sync'

violet merlin
remote shell
#

What discord library are you using?

violet merlin
#

wdym

#

and where can i check that

remote shell
#

What's the import?

violet merlin
#
@client.hybrid_command(description='Say hello to our bot!')
async def hello(ctx):
  await ctx.send('Hi')```
remote shell
#

Import...

violet merlin
#

??

remote shell
#

Top of the file?

violet merlin
#
@client.event
async def on_ready():
  await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name='To CodeFreak'))
  print("Bot is now running")
  try: 
    synced = await client.tree_sync()
    print(f'{len(synced)} Commands')
  except Exception as e:
   print(e)
   print('Worked')```
remote shell
#

Very top of the file

violet merlin
#
import os, asyncio, discord, datetime
from discord.ext import commands
from discord import app_commands
from discord.ui import Button, View```
remote shell
violet merlin
#

ye

remote shell
#

That is what I asked

violet merlin
#

oh

#

but how do i fix that error

remote shell
#

Did you copypaste the code from somewhere?

violet merlin
remote shell
#

ChatGPT?

violet merlin
remote shell
#

Discord.py client nor the Discord.ext bot contain the function named tree_sync()

violet merlin
#

i dont undderstand

remote shell
#

The tree_sync() does not exist in either Discord.py or Discord.ext

#

Where did you find the code that uses that?

#

The code that uses that is from the package discord-class-commands

#

app_commands contains sync() though

#

Is that what you're trying to use?

violet merlin
violet merlin
#

I'm stupid