我正在为不和谐编写一个机器人,如果字符串不区分大小写,我会喜欢这个,但我不知道如何

时间:2021-07-08 14:13:58

标签: python discord discord.py

这是我不希望区分大小写的行: 如果 message.content.startswith('.rich'): 我想把它变成小写,但我还没有想出如何在代码中实现它

import discord
from discord.ext import commands
import os
from keep_alive import keep_alive

client = discord.Client()

from discord.utils import find

@client.event
async def on_message(message):
  if message.content.startswith('.rich'):
    embed1 = discord.Embed(title='Richard I (Flag Defense)')
    embed1.set_image(url='https://cdn.rok.guide/wp-content/uploads/2019/09/richard-flag-defense-talent-build.jpg')
    embed1.set_author(name='Talent Builder', icon_url='https://cdn.discordapp.com/attachments/821829884967649291/860267265404305473/TB7.2t.png')

    embed2 = discord.Embed(title="Richard I (Garrison)")
    embed2.set_image(url='https://cdn.rok.guide/wp-content/uploads/2019/09/richard-i-garrison-talent.jpg')
    embed2.set_author(name='Talent Builder', icon_url='https://cdn.discordapp.com/attachments/821829884967649291/860267265404305473/TB7.2t.png')

    embed3 = discord.Embed(title="Richard I (Infantry)")
    embed3.set_image(url='https://cdn.rok.guide/wp-content/uploads/2019/09/richard-i-infantry-talent.jpg')
    embed3.set_author(name='Talent Builder', icon_url='https://cdn.discordapp.com/attachments/821829884967649291/860267265404305473/TB7.2t.png')

    await message.channel.send(embed=embed1)
    await message.channel.send(embed=embed2)
    await message.channel.send(embed=embed3)```

2 个答案:

答案 0 :(得分:1)

尝试编辑此行

if message.content.startswith('.rich'):

为此:

if message.content.lower().startswith('.rich'):

答案 1 :(得分:1)

使字符串区分大小写的一种简单方法是使用 .lower() 方法,如下所示:

if message.content.lower().startswith('.rich'):
# '.rich' must be lowercase for anything to return true

.lower() 将每个字符设置为其小写。这将允许任何大写或小写字母通过不区分大小写的 if 语句。