Mensch
Mensch

Reputation: 5

Making a discord bot kick a certain member on typing (Python)

I am trying to make my discord bot kick someone if they start typing and was wondering if it's possible. Or if a certain user sends a message at all. Was wondering if this is possible. Here is my code so far:

#Import Discord
import discord

#Client And Pemrs
client = discord.Client()

intents = discord.Intents.default()
intents.members = True

client = discord.Client(intents=intents)
@client.event
async def on_typing(channel, user, when,):
    if user.id == 574638576155754498: 
        general_channel = client.get_channel(784127484823339031)
        await kick(reason="He said the word") 

@client.event
async def on_ready():
#What The Bot Doing
  @client.event
  async def on_message(message):
   if "Valorant" in message.content:
        await message.author.kick(reason="He said the word") 
   if "valorant" in message.content:
        await message.author.kick(reason="He said the word") 
   if "VALORANT" in message.content:
        await message.author.kick(reason="He said the word") 

#Run The Client
client.run('token')

Thanks in advance for any help.

Upvotes: 0

Views: 153

Answers (2)

LoahL
LoahL

Reputation: 2613

First of all, you can't have events in events, so move on_message to outside of on_ready. When you did this, the code should kick members when they say Valorant. Then, in on_typing, simply add user before kick(reason="He said the word"), and before that, test if it is a member object:

@client.event
async def on_typing(channel, user, when,):
    if user.id == 574638576155754498: 
        general_channel = client.get_channel(784127484823339031)
        if isinstance(user, discord.Member): #when this is not true, the typing occured in a dm channel, where you can't kick.
            await user.kick(reason="He said the word") 

So your final code would be:

#Import Discord
import discord

#Client And Pemrs
client = discord.Client()

intents = discord.Intents.default()
intents.members = True

client = discord.Client(intents=intents)

@client.event
async def on_typing(channel, user, when,):
    if user.id == 574638576155754498: 
        general_channel = client.get_channel(784127484823339031)
        if isinstance(user, discord.Member): #when this is not true, the typing occured in a dm channel, where you can't kick.
            await user.kick(reason="He said the word") 

@client.event
async def on_ready():
    print("Client is ready") #the on_ready function is called only when the client is ready, so we just print in there that the client is ready

@client.event
async def on_message(message):
    if "valorant" in message.content.lower(): #use the changes dank tagg suggested
        await message.author.kick(reason="He said the word") 

#Run The Client
client.run('token')

References:

Upvotes: 0

dank tagg
dank tagg

Reputation: 259

Also,


   if "Valorant" in message.content:
        await message.author.kick(reason="He said the word") 
   if "valorant" in message.content:
        await message.author.kick(reason="He said the word") 
   if "VALORANT" in message.content:
        await message.author.kick(reason="He said the word")

Can be simplefied into

   if "valorant" in message.content.lower():
        await message.author.kick(reason="He said the word") 

This should be a comment, but since it doesn't support Markdown i posted it as an answer

Upvotes: 1

Related Questions