我正在尝试使用spring-websocket在用户之间发送私人消息。 我发现以下内容: https://github.com/rstoyanchev/springx2013-websocket/blob/master/spring-messaging/slides.md 包括 ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我正在尝试制作一个 discord 机器人,它可以在用户像“我很无聊”之类的“你好无聊,我是爸爸”之类的东西时做出回应。 我如何让“无聊”部分成为某人在“我是”之后所说的任何内容,现在它是 {sender},它只是说“嗨 {sender}。我是爸爸!” 而不是我的用户名。
当前机器人代码:
import discord
from discord.utils import get
# imports library resources
client = discord.Client()
# connects to client
@client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
# prints in console the message when bot is turned on
@client.event
async def on_message(message):
if message.author == client.user:
# prevents the bot from responding to itself
return
if message.content.startswith('I\'m'):
await message.channel.send('Hi {sender}, I\'m dad!')
if message.content.startswith('im'):
await message.channel.send('Hi {sender}, I\'m dad!')
if message.content.startswith('Im'):
await message.channel.send('Hi {sender}, I\'m dad!')
if message.content.startswith('i\'m'):
await message.channel.send('Hi {sender}, I\'m dad!')
如果其他反应不是您想要的:
您可以解决此问题的一种方法是拆分消息内容,从第一个元素中切出(或者如果您也想查找,则在我的情况下为前两个)
if message.content.startswith("I'm"):
await message.channel.send(f"Hi {' '.join(message.content.split()[1:])}, I'm dad!")
编辑:一些“和”问题
您可以使用 .split() 将单词用空格分隔成一个列表。 然后你可以使用 .index() 来查找我在列表中的索引,然后 +1 来获取下一个使用的单词。
if "I\'m" in message.content:
word_list = message.content.split()
index = word_list.index("I\'m")
if index != len(word_list):
name = word_list[index+1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.