【问题标题】:Making a discord bot kick a certain member on typing (Python)让不和谐的机器人在打字时踢某个成员(Python)
【发布时间】:2021-05-09 00:53:38
【问题描述】:

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

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

提前感谢您的帮助。

【问题讨论】:

  • 您必须将 on message 事件放在 on_ready 事件之外。您还需要在 on_typing 事件中使用 user.kick()。

标签: python discord.py


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

【讨论】:

    【解决方案2】:

    首先,事件中不能有事件,所以将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')
    

    参考资料:

    【讨论】:

    • 好的,我现在明白了。非常感谢您的帮助!
    猜你喜欢
    • 2019-02-22
    • 2020-09-23
    • 2021-06-08
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 2021-08-08
    • 1970-01-01
    • 2021-06-08
    相关资源
    最近更新 更多