-2

So basically I have this code:

import discord
import os

bot = commands.Bot(command_prefix = "!")
TOKEN = (os.getenv("TOKEN"))
client = discord.Client()


@client.event
async def on_message(message):
    if message.content.startswith('!help'):
    embedVar = discord.Embed(
        title="Help Page", description="Under development", color=0x00ff00)
    await message.channel.send(embed=embedVar)


@client.command()
@commands.has_any_role("Owner")
async def ban (ctx, member:discord.User=None, reason =None):
if member == None or member == ctx.message.author:
    await ctx.channel.send("You cannot ban yourself")
    return
if reason == None:
    reason = "Breaking Rules"
message = f"You have been banned from {ctx.guild.name} for {reason}"
await member.send(message)
# await ctx.guild.ban(member, reason=reason)
await ctx.channel.send(f"{member} is banned!")


@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')


client.run(TOKEN)

When I run this I get the sequent error:

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    bot = commands.Bot(command_prefix = "!")
NameError: name 'commands' is not defined

Can someone please help me?

Joël
  • 2,723
  • 18
  • 36
Davide
  • 1
  • 1
  • Does this answer your question? [NameError: name 'reduce' is not defined in Python](https://stackoverflow.com/questions/8689184/nameerror-name-reduce-is-not-defined-in-python) – Jeffrey Swan Jan 02 '21 at 02:20

2 Answers2

0

In the documentation for commands, it states to add this to your imports:

from discord.ext import commands
DapperDuck
  • 2,728
  • 1
  • 9
  • 21
  • Now it gives me this error: Traceback (most recent call last): File "main.py", line 18, in @client.command() AttributeError: 'Client' object has no attribute 'command' – Davide Jan 01 '21 at 22:30
  • change ```client``` to ```bot``` – DapperDuck Jan 01 '21 at 22:35
0

commands isn't defined because it isn't imported in your code put from discord.ext import commands at the top of your code. And, you don't need discord.Client() bc commands.Bot() is a subclass of it so please remove it for it is redundant. and finally, change client.command to bot.command to make the code work.

nonimportant
  • 406
  • 4
  • 15