简体   繁体   中英

discord.py the massban command doesn't work?

so I've made this simple script to test it on a test server, the code gives no errors / logs yet it doesn't function, bans no one to be exact. Any ideas? (Only for testing and educational purposes only.)

from discord.ext import commands
import random
import colorama
from discord import Permissions
from colorama import Fore, Style
import asyncio

token = "token"


client = commands.Bot(command_prefix="y!")


@client.event
async def on_ready():
  print('''
  
  READY
  ''')
  await client.change_presence(activity=discord.Game(name="test"))


@client.command()
async def bonk(ctx):
  for user in ctx.guild.members:
    try:
      await user.ban()
    except:
        pass

client.run(token, bot=True)```

It looks like Intents are missing as you stated that you did not import them. Without Intents you can't catch all the members in a guild.

Make sure to turn on all the necessary ones in the Discord Developer Portal for your application.

To implement them to your code you can use the following:

intents = discord.Intents.all() # Imports all the Intents
client = commands.Bot(command_prefix="YourPrefix", intents=intents)

You can also read the docs for more information.

Your full code would be:

Imports shortened

token = "token"

intents = discord.Intents.all()
client = commands.Bot(command_prefix="y!", intents=intents)


@client.event
async def on_ready():
  print('''READY''')
  await client.change_presence(activity=discord.Game(name="test"))


@client.command()
async def bonk(ctx):
  for user in ctx.guild.members:
    try:
      await user.ban()
    except:
        pass

client.run(token, bot=True)

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