[英]How would I respond to a message with what the user said?
I'm trying to make a discord bot that responds to user whenever they something like "I'm bored" with "Hi bored, I'm dad.".我正在尝试制作一个 discord 机器人,它可以在用户像“我很无聊”之类的“你好无聊,我是爸爸”之类的东西时做出回应。 How would I make the "bored" part be anything someone said after "I'm", Right now it's {sender} and it just says "Hi {sender}. I'm dad!"
我如何让“无聊”部分成为某人在“我是”之后所说的任何内容,现在它是 {sender},它只是说“嗨 {sender}。我是爸爸!” instead of my username.
而不是我的用户名。
Current bot code:当前机器人代码:
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!')
In case the other reaction isnt exactly what you're looking for:如果其他反应不是您想要的:
A way you could solve this is by splitting the message content slicing off the first element (or first two in case of I am, if you want to look for that too)您可以解决此问题的一种方法是拆分消息内容,从第一个元素中切出(或者如果您也想查找,则在我的情况下为前两个)
if message.content.startswith("I'm"):
await message.channel.send(f"Hi {' '.join(message.content.split()[1:])}, I'm dad!")
EDIT: some " and ' problems编辑:一些“和”问题
You can use.split() to separate the words by a space into a list.您可以使用 .split() 将单词用空格分隔成一个列表。 Then you can use.index() to find the index of the I'm in the list, then +1 to get the next word used.
然后你可以使用 .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.