使discordbot在键入(Python)时踢开某个成员



如果有人开始打字并怀疑这是否可能,我正试图让我的discord机器人踢他们。或者某个用户发送了一条消息。我想知道这是否可能。这是我到目前为止的代码:

#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')

提前感谢您的帮助。

此外,


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")

可以简化为

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

这应该是一个评论,但由于它不支持Markdown,我将其作为回答发布

首先,事件中不能有事件,所以将on_message移动到on_ready之外。当您这样做时,当成员说Valorant时,代码应该踢他们。然后,在on_typing中,只需在kick(reason="He said the word")之前添加user,然后测试它是否是成员对象:

@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") 

所以你的最终代码是:

#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')

参考文献:

  • member.kick

最新更新