简体   繁体   中英

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}\nVá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)

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 .

There are a multitude of ways to make your bot, and it seems you tried to mash 2 ways of making it together.

Option 1: using the pre-made commands bot class

client = commands.Bot(command_prefix = '?')

client.command()
async def command_name(ctx, argument):
    #command function

client.run(token)

Option 2: making you own subclass of client

class MyClient(discord.Client):
    async def on_message(self, message):
        if  message.content.startswith('command_name'):
            #command functionality

client = MyClient()
client.run()

You can use either of the two options, but not both at the same time (you could actually do that, but not in the way you did)

Also I would advice staying away from discord.py to learn python as asyncio is pretty complex

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM