如何在discord.py中使bot不区分大小写? [英] How can I make a bot not case sensitive in discord.py?

查看:0
本文介绍了如何在discord.py中使bot不区分大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这包括前缀和命令,以及您在不一致时键入的几乎任何内容。以下是我的代码:

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)

推荐答案

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)

这篇关于如何在discord.py中使bot不区分大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆