Python “多个输出”;对于…中的单词;
此代码支持扫描消息并尝试检测自我伤害。如果它检测到这种行为,那么它会发送一个链接到自杀预防网站,正如你在截图中看到的那样。但是,如果它检测到两个不同的关键字会触发响应,那么它会发布两次所需的消息Python “多个输出”;对于…中的单词;,python,python-3.x,discord,discord.py,Python,Python 3.x,Discord,Discord.py,此代码支持扫描消息并尝试检测自我伤害。如果它检测到这种行为,那么它会发送一个链接到自杀预防网站,正如你在截图中看到的那样。但是,如果它检测到两个不同的关键字会触发响应,那么它会发布两次所需的消息 suicide = ['wanna die', 'kill myself', 'kill me', ...] for word in suicide: if (message.content.lower().count(word) > 0) and (message.author.id =
suicide = ['wanna die', 'kill myself', 'kill me', ...]
for word in suicide:
if (message.content.lower().count(word) > 0) and (message.author.id == 712885407993561169):
await message.delete()
elif (message.content.lower().count(word) > 0) and (message.author.id != 712885407993561169):
#message for the sucicodal prevention link goes here
您应该循环遍历每个单词,并设置一个“Keyword detected”布尔值,并且仅在该值为真的情况下执行后续操作
suicide = ['wanna die', 'kill myself', 'kill me', ...]
delete_required = False
response_required = False
for word in suicide:
if (message.content.lower().count(word) > 0) and (message.author.id == 712885407993561169):
delete_required = True
elif (message.content.lower().count(word) > 0) and (message.author.id != 712885407993561169):
keyword_detected = True
if delete_required:
await message.delete()
elif response_required:
embed = discord.Embed(description='\n**National Suicide Prevention Lifeline**\nHours: Available 24 hours\nLanguages: English, Spanish\n\n:paperclip: [Chat](https://suicidepreventionlifeline.org/chat/)\n:globe_with_meridians: [Official Website](https://suicidepreventionlifeline.org/)', color=0x00ff00)
embed.set_author(name = 'Help is available\nSpeak with a counselor today', icon_url=message.author.avatar_url)
await message.channel.send(embed=embed)
您应该循环遍历每个单词并设置一个“Keyword detected”布尔值,并且仅在该值为true的情况下执行后续操作
suicide = ['wanna die', 'kill myself', 'kill me', ...]
delete_required = False
response_required = False
for word in suicide:
if (message.content.lower().count(word) > 0) and (message.author.id == 712885407993561169):
delete_required = True
elif (message.content.lower().count(word) > 0) and (message.author.id != 712885407993561169):
keyword_detected = True
if delete_required:
await message.delete()
elif response_required:
embed = discord.Embed(description='\n**National Suicide Prevention Lifeline**\nHours: Available 24 hours\nLanguages: English, Spanish\n\n:paperclip: [Chat](https://suicidepreventionlifeline.org/chat/)\n:globe_with_meridians: [Official Website](https://suicidepreventionlifeline.org/)', color=0x00ff00)
embed.set_author(name = 'Help is available\nSpeak with a counselor today', icon_url=message.author.avatar_url)
await message.channel.send(embed=embed)
试试这个:
suicide = ['wanna die', 'kill myself', 'kill me', ...]
contained = False
for word in suicide:
if (message.content.lower().count(word) > 0) and (message.author.id == 712885407993561169):
await message.delete()
elif (message.content.lower().count(word) > 0) and (message.author.id != 712885407993561169):
contained = True
if contained:
#message for the sucicodal prevention link goes here
它将只发送一次消息,因为它位于循环之外,因此如果包含2个或更多单词,它将不会被重复。请尝试以下操作:
suicide = ['wanna die', 'kill myself', 'kill me', ...]
contained = False
for word in suicide:
if (message.content.lower().count(word) > 0) and (message.author.id == 712885407993561169):
await message.delete()
elif (message.content.lower().count(word) > 0) and (message.author.id != 712885407993561169):
contained = True
if contained:
#message for the sucicodal prevention link goes here
它只会发送一次消息,因为它在循环之外,因此如果包含2个或更多单词,它将不会重复。一旦在消息字符串中检测到单词,您可以使用停止检查
if (message.author.id != 712885407993561169) and any(word in message.content.lower() for word in suicide):
在消息字符串中检测到单词后,可以使用停止检查
if (message.author.id != 712885407993561169) and any(word in message.content.lower() for word in suicide):
您应该循环遍历每个单词并设置一个“检测到的关键字”布尔值,然后仅基于此值为true执行操作。您应该循环遍历每个单词并设置一个“检测到的关键字”布尔值,然后仅基于此值为true执行操作。谢谢,也谢谢