简体   繁体   English

我将如何用用户所说的内容回复消息?

[英]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.

相关问题 当特定用户在频道中发送消息时,如何让我的 discord 机器人响应? - How do I make my discord bot respond when a specific user sends a message in the channel? 如何让我的机器人在没有 on_message 事件的情况下响应用户提及? - How do I make my bot respond to a user mention without on_message event? 在Y / NI上希望小写y能够准确响应大写Y - on the Y/N I would like a lowercase y to be able to respond exactly how an uppercase Y would 如何检查 Discord 消息中的图像? - How would I check a Discord message for an image? 我如何将消息发送到随机频道? - How would I send a message to a random channel? 如果option为true,如何响应消息 - How to respond with message if option is true 如何使我的代码响应用户输入 - How can I make my code respond to user input 如果特定消息来自特定用户,我希望我的不和谐机器人做出响应 - I want my discord bot to respond if a specific message is coming from a specific user 您如何使Discord机器人删除该消息,并在前缀“ .say”之后说出您所说的内容 <text> ”在Python中? - How do you make a Discord bot delete the message and it says what you said after the prefix like “.say <text>” in Python? 如何让 Discord 机器人在 on_message 中使用自定义表情符号响应消息 - How do I make a Discord bot respond to a message with a custom emoji in on_message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM