Discord Bot cannot Kick/Ban members - discord.py

The kick command in the discord bot I made is not working. I used the "-kick #user" command to do so. But it does nothing. No error, nothing.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='-')
#client.command()
#commands.has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member, *, reason=None):
await member.kick(reason=reason)
await ctx.send(f'User {member} has been kicked')
client.run('Token')

Because member is None, you have to enable intents.members in order it to work:
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix='-', intents=intents)
Also make sure to enable them in the developer portal
docs

Related

discord.py will not execute a command for some reason

whenever I try to type something in discord, the bot will do nothing.
I checked on the discord developer portal if the message intents was on and it was and I also enabled it in the code as you can see above but it still doesn't work
import os
import discord
import random
from keep_alive import keep_alive
from discord.ext import commands
token = 'token'
intents = discord.Intents.all()
intents.members = True
command_prefix = '!'
bot = commands.Bot(command_prefix, intents = intents)
#bot.event
async def on_ready():
await bot.change_presence(activity=discord.Activity(
type=discord.ActivityType.watching, name='butterflys fly by'))
#bot.command(name='99')
async def nine_nine(ctx):
print("hell world")
brooklyn_99_quotes = [
'I\'m the human form of the 💯 emoji.',
'Bingpot!',
(
'Cool. Cool cool cool cool cool cool cool, '
'no doubt no doubt no doubt no doubt.'
),
]
response = random.choice(brooklyn_99_quotes)
await ctx.send(response)
#bot.event
async def on_member_join(member):
guild = bot.get_guild(781230819414769765)
welcome_channel = guild.get_channel(783371093570355211)
await welcome_channel.send(
f'Welcome to **{guild.name}**, {member.mention} have fun here! ')
keywords = ["e"]
#bot.event
async def on_message(msg):
for i in range(len(keywords)):
if keywords[i] in msg.content:
await msg.channel.purge(limit=1)
await msg.author.send("don't say that!")
keep_alive()

How do I DM a person with Discord.py??? Why won't my code work?

import discord
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
#client.event
async def on_ready():
print('Running...')
#client.event
async def on_message(message):
while True:
user = client.get_user('user')
await user.send('hi')
I have tried a million variations of this and it won't work.
Why won't send work????????
I just tested this with the latest version of pycord and it works.
import discord
import os
import asyncio
TOKEN = "TOKEN_HERE"
intents = discord.Intents.default()
intents.messages = True
intents.message_content = True
intents.members = True
client = discord.Client(intents=intents, members = True, messages = True, message_content = True)
#client.event
async def on_ready():
print("Running...")
#client.event
async def on_message(message):
user_id = message.author.id
user = await client.fetch_user(user_id)
await user.send("HI")
client.run(TOKEN)
I don't know why you had a while loop in there, I don't think it was needed. I used client.fetch_user as the method for getting the member object and included the intents for messages and members otherwise the bot won't be able to read messages or access member objects. You will need to make sure you have the intents enabled in your developer account for this to work.

How do I make my bot reply to users on discord.py VSC?

Ok, so I have tried over 674 types of code, and none of them work. All I want is to make my bot reply to a message if a user says "?Pizza" and it replies "Wassuuuuuup"
Any help?
discord.ext.commands:
import discord
from discord.ext import commands
token = ""
bot = commands.Bot(command_prefix="?")
#bot.command()
async def pizza(ctx):
await ctx.send("Wassuuuuuup")
bot.run(token)
on_message():
#bot.event
async def on_message(msg):
if msg.author.id == bot.user.id:
return
if msg.content.startswith("?pizza"):
await msg.channel.send("Wassuuuuuup")

#bot.command() doesn't run - No errors show up

import discord
from discord.ext import commands
bot = commands.Bot(command_prefix=commands.when_mentioned_or('!'))
intents = discord.Intents.all()
client = discord.Client(intents=intents)
#bot.command()
async def hi(ctx):
await ctx.send("hi")
client.run(TOKEN)
I'm trying to get my bot to respond "hi" when a user runs command "!hi". The problem is that the command doesn't run and no errors show up as well. The code within the .py file is exactly as it is shown (except TOKEN is replaced with an actual Token)
You're running client, and decorating the command with bot.command(), you have to either use discord.Client or commands.Bot not both.
import discord
from discord.ext import commands
intents = discord.Intents.all()
bot = commmands.Bot(command_prefix=command.when_mentioned_or("!"), intents=intents)
#bot.command()
async def hi(ctx):
await ctx.send("Hi")
# You can also use events with `commands.Bot`
#bot.event
async def on_ready():
print("Ready")
bot.run("TOKEN")
The problem here is that you're using both discord.Client and commands.Bot
here's how to fix it:
import discord
from discord.ext import commands
TOKEN = "Your Token"
Intents=discord.Intents.all()
bot = commands.Bot(command_prefix=commands.when_mentioned_or('!'), Intents=Intents)
#bot.command()
async def hi(ctx):
await ctx.send("hi")
client.run(TOKEN)

on_typing not responding to typing discord.py

I am attempting to have a bot recognise when a user is typing and if that user is me, it sends a message. I don't know if I'm using this wrong but I read the API and at still doesn't make sense to me. the bot is not responding to any typing that I do in the default chat of my discord test server. I tried simplifying it to make sure that it actually wasn't seeing my typing and it still doesn't work, any ideas. the code is below.
import discord
from discord.ext import commands
token = ('~my token~')
client = commands.Bot(command_prefix = '.')
client.case_insensitive = (True)
#client.event
async def on_ready():
print('Ready')
#client.event
async def on_member_join(user):
print(f'~Temp~ {user} joined the server.')
#client.event
async def on_guild_join(server):
print(f'~Temp~ {server} joined')
#client.event
async def on_typing(channel, user, when):
print('Ready')
'''
if user.id == id1:
await channel.send('1')
elif user.id == id2:
await channel.send('2')
else:
await channel.send('3')
'''
#client.event
async def on_message(message):
print('~Temp~ Message found')
client.run(token)
You need to enable intents in order to work
intents = discord.Intents.default()
client = commands.Bot(.., intents=intents)
A Primer to Gateway Intents

Resources