让不和谐机器人在打字时踢某个成员(Python)

时间:2021-05-08 01:09:01

标签: python discord.py

我试图让我的不和谐机器人在某人开始打字时踢他们,我想知道这是否可行。或者,如果某个用户根本没有发送消息。想知道这是否可能。到目前为止,这是我的代码:

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

在此先感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

还有,


   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,所以我将其发布为答案

答案 1 :(得分:0)

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

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

参考文献: