首页 > 解决方案 > 当他们说出触发词时,如何让不和谐的机器人踢某人

问题描述

当他们说出触发词时,如何让不和谐机器人踢某人?例如,当有人说“白痴”时,他们会被踢出服务器。

这是我尝试过的:

**import discord
import time
from discord.ext import commands

client = discord.Client()

@client.event
async def on_ready():
    print("Bot is ready?")

@client.event
async def on_message(message):
    if message.content.find("lol") != -1:
        await message.channel.send("HI!")

@client.event
async def on_message(message):
    if message.content.find('matei') != -1:
        await kick('minionulLuiMatei#5893')
        return




client.run(token)**

标签: pythondiscorddiscord.py

解决方案


不能有多个on_message事件。你必须将它们合二为一。

一篇解释得很好的帖子:Why multiple on_message events will not work

现在回答你的问题。您可以使用两种方法来过滤单词并踢出成员:

第一种方法:过滤所有消息并查看单词是否Idiot出现在句子中的任何位置。

async def on_message(message):
    if "Idiot" in message.content:

第二种方法: 检查单词是否Idiot只出现在句子的开头。

async def on_message(message):
    if message.content.startswith("Idiot"):

然后踢一个成员,你使用以下函数:

await message.author.kick(reason=None)

您的整个代码将是:

@client.event
async def on_message(message):
    if "Idiot" in message.content: # Method 1
        await message.author.kick(reason=None) # Kick the author of the message, reason is optional

    if message.content.startswith("Idiot"): # Method 2
       await message.author.kick(reason=None)

推荐阅读