Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

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

Good afternoon! I am new to Python , and I am working on a discord bot. I keep suffering from this error: AttributeError: 'Client' object has no attribute 'command'. I tried everything to repair this, but I did not know. Any help would be fine. Please help me!

Here is the code:

import discord
import random
from discord.ext import commands




class MyClient(discord.Client):
    client = commands.Bot(command_prefix = '?')


# Start

async def on_ready(self):
    print('Logged on as', self.user)

# Latency
client = discord.Client()
@client.command()
async def ping(ctx):
    await ctx.send(f'Pong! {round(client.latency * 1000)}ms')


# 8ball

@client.command(aliases=['8ball'])
async def _8ball(ctx, *, question):
    responses = ['Biztosan.',         
                 'Nagyon kétséges.']
    await ctx.send(f'Kérdés: {question}
Válasz: {random.choice(responses)}')

# Clear

@client.command()
async def clear(ctx, amount=5):
    await ctx.channel.purge(limit=amount)
    await ctx.send(f'Kész!')

async def on_message(self, message):
        word_list = ['fasz', 'kurva', 'anyad', 'anyád', 'f a s z', 'seggfej', 'buzi', 'f.a.s.z', 'fa sz', 'k U.rv@ any@dat']

       
        if message.author == self.user:
            return

        messageContent = message.content
        if len(messageContent) > 0:
            for word in word_list:
                if word in messageContent:
                    await message.delete()
                    await message.channel.send('Ne használd ezt a szót!')
            
        messageattachments = message.attachments
        if len(messageattachments) > 0:
            for attachment in messageattachments:
                if attachment.filename.endswith(".dll"):
                    await message.delete()
                    await message.channel.send("Ne küldj DLL fájlokat!")
                elif attachment.filename.endswith('.exe'):
                    await message.delete()
                    await message.channel.send("Ne csak parancsikont küldj!")
                else:
                    break

client = MyClient()
client.run(token)
question from:https://stackoverflow.com/questions/65917366/python-attributeerror-client-object-has-no-attribute-command

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Why don't you simply define client like this?

client = commands.Bot(...)

Also you have defined it a couple of times in the code, delete them all and define it only ONCE at the top of the code after the imports. This is a really bad question, you should learn a lot more python before diving deeper into discord.py.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...