python - 当他们说出触发词时,如何让不和谐的机器人踢某人
问题描述
当他们说出触发词时,如何让不和谐机器人踢某人?例如,当有人说“白痴”时,他们会被踢出服务器。
这是我尝试过的:
**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)**
解决方案
您不能有多个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)
推荐阅读
- elasticsearch - logstash输入jdbc如何填充多值字段?
- php - Paypal API 在不同机器上显示不同的行为
- mips - 将 MIPS 函数值更改为整数值并在 QtSpim 中执行
- django - 如何将社交登录添加到 Saleor.io?Django 和 graphQL
- pubnub - 获取 Pubnub 中某个频道的订阅者数量
- python-3.x - Matplotlib中基于多种条件的时间序列数据绘制不同颜色的面积图
- jmeter - 面临在 JMeter 中存储变量结果的问题
- google-colaboratory - 将自己的数据集结合到 ImageNet
- reactjs - 无法从 Date 中获取值
- javascript - 打字稿类型:具有静态/固定键和动态键的对象