简体   繁体   中英

How I can ban someone with Python and discord.py?

I'm trying to implement some feature where if someone sends a Discord invite, they get banned, but I get the same error every time:

'BotLibertarin' object has no attribute 'ban'

Here is the code:

import discord
from discord.ext import commands


class BotLibertarin(discord.Client):
    client = commands.Bot(command_prefix='.')
    @client.event   
    async def on_ready(self):
       print(f"Logged on as {self.user}")

    @client.event
    async def on_message(self, message):
        print(f"message from {message.author} what he said {message.content} and the id is {message.author.id}")
       if message.author == client.user:
           return
      #ban for invinte on discord
    if message.content.startswith("https://discord.gg/"):
        for member in client.get_all_members():
            print("entrou dentro do if")
            if member.id == message.author.id:
                print("vai tomar ban KKKKKK")
                banned = member.id

                try:
                    await client.ban(banned,delete_message_days=2)
                    await message.channel.send(f"User {banned} sended a discord invite")
                except Exception as e:
                    print(f"you got error {e}")
client = BotLibertarin()
client.run("")

You're using syntax from a version of discord.py, v0.16, that isn't supported anymore.
See the guide for migrating to v1 , specifically the Models are Stateful section .

You should use Member.ban or Guild.ban rather than Client.ban .
In this case, the relevant line would be await member.ban(delete_message_days=2) .

Also, in the future, when asking for help, you should provide the full traceback.

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