Discord.py error AttributeError: 'Client' object has no attribute 'command' - discord.py

why it no work
`
#Importing stuff
import discord
import os
import random
from discord.ext import commands
#Variables
command_prefix = '!'
intents = discord.Intents.all()
token = 'MTAzOTA3ODMyODcxMzM1NTMwNA.GMKlpL.gLzQ9H6UCaAE7PNeb0Nv-I2cb-e8p-NgZZJydY'
#Client Setup
bot = discord.Client(command_prefix=command_prefix, intents=intents)
#On ready function (startup)
#bot.event
async def on_ready():
print('Online')
#Status Setup
activity = discord.Game(name="Put Status Here", type=3)
await bot.change_presence(activity=activity)
#bot.command()
async def help(ctx):
await ctx.send('baller')
#Run
bot.run(token)
`
i tried a bunch of other stuff but it no worky pls help me
Error:
AttributeError: 'Client' object has no attribute 'command'

Related

Discord.py load_extension Enable tracemalloc to get the object allocation traceback"

While running the main.py file i get this error "main.py:15: RuntimeWarning: coroutine 'BotBase.load_extension' was never awaited
client.load_extension(f"commands.{filename[:-3]}")
RuntimeWarning: Enable tracemalloc to get the object allocation traceback"
Main.py File
import discord
from discord import *
from discord.ext import commands
import os
import asyncio
client = commands.Bot(command_prefix="-", help_command=None,intents=discord.Intents.all())
token = os.getenv("TOKEN")
#client.event
async def on_ready():
print("Rhino Bot is online")
for filename in os.listdir('./commands'):
if filename.endswith('.py'):
client.load_extension(f"commands.{filename[:-3]}")
print(f"{filename[:-3]} loaded")
client.run(token)
commands/ban.py
import discord
from discord.ext import commands
import asyncio
import random
class ban(commands.Cog):
def __init__(self, client):
self.client = client
#commands.command()
#commands.cooldown(1,4,commands.BucketType.user)
#commands.has_permissions(ban_members = True)
async def ban(self, ctx,member : discord.Member,*,reason= "No Reason Provided"):
guild = ctx.guild
try:
embed=discord.Embed(title="Ban 🔨", description=(f"You Have Been Banned From the {guild.name}, Because:"+reason), color=discord.Color.random())
embed.set_author(name='Rihno Bot',
icon_url='https://cdn.discordapp.com/attachments/935411433380917309/938391346283163678/images.png')
await member.send(embed=embed)
await member.ban(reason=reason)
except:
embed=discord.Embed(title="Banned 🔨", description=(member.mention + f" has been banned from the server, Because:"+reason), color=discord.Color.random())
embed.set_author(name='Rihno Bot',
icon_url='https://cdn.discordapp.com/attachments/935411433380917309/938391346283163678/images.png')
embed=discord.Embed(title='Error', description=("dms are closed so i couldn't dm them ❌"))
embed.set_author(name='Rihno Bot',
icon_url='https://cdn.discordapp.com/attachments/935411433380917309/938391346283163678/images.png')
await ctx.send(embed=embed)
await member.ban(reason=reason)
def setup(client):
client.add_cog(ban(client))
What should i do? discord.py version using 2.1.0
and platform is replit.................
It's because you added load_extension part without await it so that's why it's causing coroutine error. You can fix this in two ways:
Just add this to your place where you added client.load_extension(f"commands.{filename[:-3]}")
await client.load_extension(f"commands.{filename[:-3]}")
Can even do something like this, if you don't wanna await:
for filename in os.listdir('./commands'):
if filename.endswith('.py'):
client.load_extension(f"commands.{filename[:-3]}")
#client.event
async def on_ready():
print("Rhino Bot is online")

discord.py will not execute a command for some reason

whenever I try to type something in discord, the bot will do nothing.
I checked on the discord developer portal if the message intents was on and it was and I also enabled it in the code as you can see above but it still doesn't work
import os
import discord
import random
from keep_alive import keep_alive
from discord.ext import commands
token = 'token'
intents = discord.Intents.all()
intents.members = True
command_prefix = '!'
bot = commands.Bot(command_prefix, intents = intents)
#bot.event
async def on_ready():
await bot.change_presence(activity=discord.Activity(
type=discord.ActivityType.watching, name='butterflys fly by'))
#bot.command(name='99')
async def nine_nine(ctx):
print("hell world")
brooklyn_99_quotes = [
'I\'m the human form of the 💯 emoji.',
'Bingpot!',
(
'Cool. Cool cool cool cool cool cool cool, '
'no doubt no doubt no doubt no doubt.'
),
]
response = random.choice(brooklyn_99_quotes)
await ctx.send(response)
#bot.event
async def on_member_join(member):
guild = bot.get_guild(781230819414769765)
welcome_channel = guild.get_channel(783371093570355211)
await welcome_channel.send(
f'Welcome to **{guild.name}**, {member.mention} have fun here! ')
keywords = ["e"]
#bot.event
async def on_message(msg):
for i in range(len(keywords)):
if keywords[i] in msg.content:
await msg.channel.purge(limit=1)
await msg.author.send("don't say that!")
keep_alive()

discord.py add_roles function not working

Attempting to create a jail bot that sends user to the "APOS zone" when given the role "APOS", but the bot is unresponsive when the command is sent
import os
import discord
import random
from discord.ext import commands
import keep_alive
from discord.utils import get
Bot_Token = os.environ['Bot_Token']
bot = discord.Client()
Bot = commands.Bot(command_prefix='AGOP~')
#startup
#bot.event
async def on_ready():
guild_count = 0
for guild in bot.guilds:
print(f"- {guild.id} (name: {guild.name})")
guild_count = guild_count + 1
print("AGOP_Bot is in " + str(guild_count) + " guilds.")
#APOS
#Bot.command()
#commands.has_role("APOS")
async def APOS(ctx, user: discord.Member, role: discord.Role):
role = get(user.server.roles, name="APOS")
await user.add_roles(user, role)
await ctx.send(f"{user.name} has been sent to the shadow realm")
bot.run(Bot_Token)
keep_alive.py
The discord.Member class doesn't have a server attribute. user.guild.roles is what you most likely are looking for. https://discordpy.readthedocs.io/en/stable/api.html#discord.Member

#bot.command() doesn't run - No errors show up

import discord
from discord.ext import commands
bot = commands.Bot(command_prefix=commands.when_mentioned_or('!'))
intents = discord.Intents.all()
client = discord.Client(intents=intents)
#bot.command()
async def hi(ctx):
await ctx.send("hi")
client.run(TOKEN)
I'm trying to get my bot to respond "hi" when a user runs command "!hi". The problem is that the command doesn't run and no errors show up as well. The code within the .py file is exactly as it is shown (except TOKEN is replaced with an actual Token)
You're running client, and decorating the command with bot.command(), you have to either use discord.Client or commands.Bot not both.
import discord
from discord.ext import commands
intents = discord.Intents.all()
bot = commmands.Bot(command_prefix=command.when_mentioned_or("!"), intents=intents)
#bot.command()
async def hi(ctx):
await ctx.send("Hi")
# You can also use events with `commands.Bot`
#bot.event
async def on_ready():
print("Ready")
bot.run("TOKEN")
The problem here is that you're using both discord.Client and commands.Bot
here's how to fix it:
import discord
from discord.ext import commands
TOKEN = "Your Token"
Intents=discord.Intents.all()
bot = commands.Bot(command_prefix=commands.when_mentioned_or('!'), Intents=Intents)
#bot.command()
async def hi(ctx):
await ctx.send("hi")
client.run(TOKEN)

"Error 'Self' is not defined" in member.edit

I'm having issues with my code saying
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: edit() missing 1 required positional argument: 'self'
when I run the command. Self shouldn't need to be defined, right?
Also, when I do add self I get an issue with ctx.
The code:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="/")
#client.command(pass_context=True)
async def join(ctx, member=discord.Member):
channel = ctx.author.voice.channel
await channel.connect()
await member.edit(mute=True)
#client.command(pass_context=True)
async def leave(ctx):
await ctx.voice_client.disconnect()
client.run("Token")
I Managed to find the problem and fixed it.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="/")
#client.command()
async def join(ctx):
channel = ctx.author.voice.channel
await channel.connect()
await ctx.author.edit(mute=True)
#client.command()
async def leave(ctx):
await ctx.voice_client.disconnect()
client.run("Token")
the problem is, you included member on your funcion. If you want it to send, edit, or do something with the message author just do ctx.author and it will be set on the author of the message.

Resources