【问题标题】:Is there a way to make a Discord Dadbot that can find "I'm" later in a message?有没有办法让 Discord Dad Bot 可以在消息中找到“我来了”?
【发布时间】:2023-04-09 08:51:01
【问题描述】:

所以我正在我的 Discord 服务器上使用 Python 开发个人机器人,它可以做各种事情,其中​​之一是模仿常见的 Dadbot。这是执行此操作的代码:

if any(word in msg for word in imlist):      
    resp = message.content.split(" ",1)[1]
    await message.channel.send("Hi "+ resp +", I'm Pigeonbot")

if any(word in msg for word in iamlist):
    resp = message.content.split(" ",2)[2]
    await message.channel.send("Hi "+ resp +", I'm Pigeonbot")

这是两个列表:

imlist = ["I'm", "Im", "im", "i'm", "IM", "I'M"]
iamlist = ["i am", "I am", "I AM"]

如果您输入不和谐的消息,例如“我饿了”,它会回复“嗨,饿了,我是 Pigeonbot”。但是,如果你在“我是”之前说一些东西,例如“外面很热,我很饿”,它会返回“你好,外面很热,我很饿,我是 Pigeonbot”。 有没有办法让机器人在消息中找到 imlist 或 iamlist 的位置并从那里开始“嗨 [],我是 Pigeonbot”?谢谢你:)

【问题讨论】:

    标签: python list discord bots


    【解决方案1】:

    这是regular expressions 的一个很好的用例

    您可以使用正则表达式I(?:'| a)?m ([^\s]*)Demo

    解释:

    • I:匹配文字 I
    • (?:...)?:使内容成为可选的non-capturing group
    • '| a:非捕获组的内容:匹配撇号或空格和a
    • m :匹配一个m,然后是一个空格。
    • (...):创建一个capturing group
    • [^\s]*: 匹配除空格以外的任意字符,任意次数

    从演示中可以看出,这只会捕获I'm 之后的单个单词。如果您想在I'm 之后捕获所有内容,您可以将正则表达式更改为I(?:'| a)m (.*) Demo。这里,捕获组的内容匹配任意字符,任意次数

    import re
    
    expr1 = re.compile(r"I(?:'| a)m ([^\s]*)", re.IGNORECASE) # Use the re.IGNORECASE flag for case-insensitive match
    
    expr2 = re.compile(r"I(?:'| a)m (.*)", re.IGNORECASE)
    
    test_texts = ["I'm hungry", "It's hot and I'm parched", "I'm not stupid", "I'm going to go to bed now, see you tomorrow"]
    
    for s in test_texts:
        print(f"text: {s}")
        match = re.search(expr1, s)
        if match:
            resp = match.group(1)
            print(f"\texpr1: Hi {resp}, I'm pigeonbot")
    
        match2 = re.search(expr2, s)
        if match2:
            resp2 = match2.group(1)
            print(f"\texpr2: Hi {resp2}, I'm pigeonbot")
    

    这给了我们输出:

    text: I'm hungry
        expr1: Hi hungry, I'm pigeonbot
        expr2: Hi hungry, I'm pigeonbot
    text: It's hot and I'm parched
        expr1: Hi parched, I'm pigeonbot
        expr2: Hi parched, I'm pigeonbot
    text: I'm not stupid
        expr1: Hi not, I'm pigeonbot
        expr2: Hi not stupid, I'm pigeonbot
    text: I'm going to go to bed now, see you tomorrow
        expr1: Hi going, I'm pigeonbot
        expr2: Hi going to go to bed now, see you tomorrow, I'm pigeonbot
    

    【讨论】:

    • @CharacterLimit 没有线索,我对 discord.py 不是很熟悉
    • @CharacterLimit 很高兴为您提供帮助!如果这回答了您的问题,请考虑accepting the answer. :)
    【解决方案2】:

    试试类似的东西。

    all_list = imlist+iamlist
    message_word_text = message.content.split(" ")
    indices = []
    for word in all_list:
          try:
           indices.append(message_word_text[word])
          except:
           indices.append(100)
    noun = message_word_text[message_word_text.index(all_list[indices.index(min(indices)])+1]
    print(f'Hi {noun}')
    
    
    
    
    

    本质上,我们在这个程序中所做的是将每个单词的索引附加到与消息单词相关的 I'm 列表中。这些索引中最低的表示第一个单词。然后我们通过取最小值的索引找到名词,将它传递到我的列表中,然后在消息内容中找到它,最后我们找到之后的单词。

    【讨论】:

      猜你喜欢
      • 2021-03-13
      • 2021-06-20
      • 2021-10-26
      • 2019-08-14
      • 2023-04-10
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      • 2021-09-17
      相关资源
      最近更新 更多