ban in discord.py code example

Example 1: discord.py ban

@commands.command() # uses command decorators, in this case inside a cog
@commands.has_permissions(ban_members=True) # only people that have permissions to ban users can use this command
async def ban(self, ctx, user: discord.Member, *, reason): # The person banning someone has to ping the user to ban, and input a reason. Remove self if you are outside a cog.
    await ctx.guild.ban(user, reason=reason) # Bans the user.
    await user.send(f"You have been banned in {ctx.guild} for {reason}") # Private messages user.
    await ctx.send(f"{user} has been successfully banned.") # messages channel to tell everyone it worked

Example 2: how to kick and ban members with discord.py

#this was in a cog   
# The below code bans player.
@commands.command()
@commands.has_permissions(ban_members=True)
  async def ban(self, ctx, member: discord.Member, *, reason=None):
     await member.ban(reason=reason)
     await ctx.send(f'User {member} has been kick')

# The below code unbans player.
@commands.command()
@commands.has_permissions(administrator=True)
  async def unban(self, ctx, *, member):
     banned_users = await ctx.guild.bans()
     member_name, member_discriminator = member.split("#")

        for ban_entry in banned_users:
            user = ban_entry.user

            if (user.name, user.discriminator) == (member_name, member_discriminator):
                await ctx.guild.unban(user)
                await ctx.send(f'Unbanned {user.mention}')
                return

# The below code kicks player
@commands.command()
@commands.has_permissions(kick_members=True)
    async def kick(self, ctx, member: discord.Member, *, reason=None):
        await member.kick(reason=reason)
        await ctx.send(f'User {member} has been kick')

Example 3: discord.py

# Discord.py is a API wrapper for python. 
Docs = "https://discordpy.readthedocs.io/en/latest/"
PyPI = "pip install -U discord.py"

# --- A simple bot ---

import discord
from discord.ext import commands 

client = commands.Bot(comand_prefix='bot prefix here') # You can choose your own prefix here

@client.event()
async def on_ready(): # When the bot starts
    print(f"Bot online and logged in as {client.user}")

# A simple command
@client.command(aliases=["ms", "aliases!"]) # You make make the command respond to other commands too
async def ping(ctx, a_variable): # a_variable is a parameter you use in the command
    await ctx.send(f"Pong! {round(client.latency * 1000)}ms. Your input was {a_variable}")

client.run('your token here') # Running the bot