【问题标题】:how to make bot respond to if there is more then four uppercase letter in message.content discord.py如果 message.content discord.py 中有超过四个大写字母,如何让机器人响应
【发布时间】:2022-01-16 20:04:28
【问题描述】:

请帮帮我,我试过这样做,但我不知道我的代码有什么问题,它不起作用。

msgs_up=message.content.upper

msg_count=msgs_up.count(msgs_up,0,200)

if ( msg_count >= 4 ):
  await message.channel.send('Woah woah. stop the cap')

【问题讨论】:

    标签: discord discord.py


    【解决方案1】:

    您可以使用正则表达式来查找字符串中大写字母的数量。

    import re
    
    regex = "[A-Z]" # Matches only capital letters
    
    string1 = "Not too many caps here"
    string2 = "FULL of CAPS"
    

    使用findall 方法获取所有匹配项

    >>> re.findall(regex, string1)
    ['N']
    
    >>> re.findall(regex, string2)
    ['F', 'U', 'L', 'L', 'C', 'A', 'P', 'S']
    

    这将返回一个包含所有匹配项的list。您现在可以通过检查列表的长度是否大于 4 来检查字符串是否包含四个以上的大写字母。

    但是,请注意,检查字母的百分比是否为大写而不是固定数量通常更好。

    >>> string3 = "This string might appear to not contain too many Capital Letters. But, it still triggers the response since the number of Capital Letters is greater than 4.
    
    >>> re.findall(regex, string3)
    ['T', 'C', 'L', 'B', 'C', 'L'] # 6 capital letters
    

    【讨论】:

      猜你喜欢
      • 2018-07-09
      • 2021-04-15
      • 2021-08-18
      • 2021-11-03
      • 2019-02-09
      • 2019-12-12
      • 2018-10-17
      • 2021-01-20
      • 1970-01-01
      相关资源
      最近更新 更多