【问题标题】:How do i make a discord bot not case-sensitive? discord.py我如何使不和谐的机器人不区分大小写?不和谐.py
【发布时间】:2021-07-29 16:11:07
【问题描述】:

如何让 Discord 机器人不区分大小写?我目前使用的方法不起作用。我见过其他类似的任务,但它们与我正在做的事情无关。

import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio

bot = commands.Bot(command_prefix='', case_insensitive=True) #ive tried both true/false. neither works 

print("Starting...")

@bot.event
async def on_ready():
  print("Logged on as {0.user}".format(bot))

  @bot.event
  async def on_message(message):
    if message.author == bot.user:
      return

    elif 'hello' in message.content:
        await message.delete()
        await message.channel.send(‘hi')
bot.run("Token")

我希望用户能够说“你好”或“你好”,它会起作用。关于我将如何去做的任何想法?

【问题讨论】:

  • elif 'hello' in message.content.lower(): 要使比较区分大小写,只需将要比较的两个字符串强制为小写(或大写,只要您对两个字符串使用相同的大小写)。跨度>
  • 您希望不区分大小写的@bot.command 在哪里?
  • 这能回答你的问题吗? How do I do a case-insensitive string comparison?
  • 我没有使用命令,我只是检测是否发送了消息
  • 顺便说一句谢谢

标签: python discord.py


【解决方案1】:

我自己对此很陌生,所以我知道的方法是您可以在比较时使用 .lowercase() 。这将做的是将整个字符串转换为小写,无论用户是否写了“Hello”、“hello”、“HEllO”等。所有这些字符串都会在 .使用小写()。

这是我的第一个答案,所以希望我能够正确解释。附上代码供您参考。

import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio

bot = commands.Bot(command_prefix='', case_insensitive=True) #ive tried both true/false. neither works 

print("Starting...")

@bot.event
async def on_ready():
  print("Logged on as {0.user}".format(bot))

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return

    elif 'hello' in message.content.lower():
        await message.delete()
        await message.channel.send(‘hi')

bot.run("Token")

让我知道它是否有效!

【讨论】:

    猜你喜欢
    • 2021-05-24
    • 2021-07-07
    • 2020-06-20
    • 2020-10-08
    • 1970-01-01
    • 2018-03-20
    • 2021-09-26
    • 2020-12-18
    • 2021-09-30
    相关资源
    最近更新 更多