如何在discord.py中使机器人不区分大小写?

时间:2020-05-03 21:00:42

标签: python python-3.x discord.py

这包括前缀和命令,以及您在Discord中键入的几乎所有内容。这是我的代码:

from discord.ext import commands
import discord.member
from dotenv import load_dotenv
import discord
from discord.utils import get

bot = commands.Bot(command_prefix="bot ")
TOKEN = "4893285903457897349857938275732985" #not a valid token by the way :)

@bot.event
async def on_ready():
    print(f'{bot.user} has connected to Discord!')


@bot.command(name='image', help='Example command')
async def image(ctx):
    #code for function goes here
    pass


bot.run(TOKEN)

1 个答案:

答案 0 :(得分:0)

Bot命令可以区分大小写 ,但是discord.py中没有使前缀区分大小写 的功能。但是,有一种解决方法。

使bot命令区分大小写

更改bot = commands.Bot(command_prefix="prefix!")

收件人:bot = commands.Bot(case_insensitive=True, command_prefix="prefix!")

为区分大小写的前缀

老实说,我不建议这样做,但是如果您确实需要区分大小写的 前缀,请遵循以下代码

创建一个名为mixedCase()

的函数
def mixedCase(*args):
  """
  Gets all the mixed case combinations of a string

  This function is for in-case sensitive prefixes
  """
  total = []
  import itertools
  for string in args:
    a = map(''.join, itertools.product(*((c.upper(), c.lower()) for c in       string)))
    for x in list(a): total.append(x)

  return list(total)

现在修改bot = commands.Bot(command_prefix="prefix!")

bot = commands.Bot(command_prefix=mixedCase("prefix!"))

最终代码

from discord.ext import commands
import discord.member
from dotenv import load_dotenv
import discord
from discord.utils import get

def mixedCase(*args):
  """
  Gets all the mixed case combinations of a string

  This function is for in-case sensitive prefixes
  """
  total = []
  import itertools
  for string in args:
    a = map(''.join, itertools.product(*((c.upper(), c.lower()) for c in string)))
    for x in list(a): total.append(x)

  return list(total)

bot = commands.Bot(case_insensitive=True, command_prefix=mixedCase("prefix" ))
TOKEN = "4893285903457897349857938275732985" #not a valid token by the way :)

@bot.event
async def on_ready():
    print(f'{bot.user} has connected to Discord!')


@bot.command(name='image', help='Example command')
async def image(ctx):
    #code for function goes here
    pass


bot.run(TOKEN)