Home > Blockchain >  discord.py bot having async def load error
discord.py bot having async def load error

Time:03-10

So I've been trying to fix this problem for a few hours but whenvever I do I just pop out more errors so I've resigned to asking the internet for help this is my code that is having the problem. this is the errorasync def load(extension): ^SyntaxError: invalid syntax

Here is the file thats having the problem

import discord
from discord.ext import commands
import asyncio
import os
from dotenv import load_dotenv
import json
bot = commands.Bot(command_prefix="!")
extensions=('music, level, Help, youtubedownload')
TOKEN=('DISCORD_TOKEN')

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

@bot.event
async def on_ready():
    print('The bot is logged in.')
    await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=f"{len(bot.guilds)} servers!"))

@bot.event
async def on_ready():  
  print("We're in "(len(bot.guilds)  "Servers!")


@bot.command()
async def load(extension):
  try:
  bot.load_extension(extension)
    print('Loaded{}'.format(extension))
  except Exception as error:
    print('{} cannot be loaded. [{}]'.format(extension, error))

@bot.command()
async def unload(extension):
  try:
    bot.unload_extension(extension)
    print('Unloaded{}'.format(extension))
  except Exception as error:
    print('{} cannot be Unloaded. [{}]'.format(extension, error))




if __name__ == '__main__':
  for extension in extensions:
    try:
      bot.load_extension(extension)
    except Exception as error:
      print('{}connot be loaded.[{}]'.format(extension, error))
    
bot.run("TOKEN")```

CodePudding user response:

I think you are trying to load cogs. so basically in the same directory make a folder named cogs and then put all of your extensions in there. The extension does need to be in a class with commands.Cog from discord.ext.commands. At the end of the extension, you need a setup function which is a simple def setup(bot): bot.add_cog(nameOfTheClass(bot)) Then in the main file, for each cog/extension just write bot.load_extension("cogs.extensionName")

CodePudding user response:

It is pretty obvious that the indentation in your code is kinda messed up. Here's what you should do in your load() function.

@bot.command()
async def load(extension):
  try:
    bot.load_extension(extension)
    print('Loaded{}'.format(extension))
  except Exception as error:
    print('{} cannot be loaded. [{}]'.format(extension, error))

You must nest everything under try, otherwise, it will return indentation error/syntax error.

  • Related