首页 文章

使代码uncaseSensitive,Python,Discord Bot

提问于
浏览
1

我怎样才能使这个代码成为icaseSensitive?我试过 bot = commands.Bot(command_prefix=';', case_insensitive=True 我在重写分支上的代码是什么?我知道如何在 async def on_message 但是听说使用 async def on_message 不是那么聪明

码:

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


bot = commands.Bot(command_prefix=';', case_insensitive=True)

@bot.command(pass_context=True)
@commands.has_role("Admin")
async def info(ctx, user: discord.Member):
    embed = discord.Embed(title="{}'s info:".format(user.name), description="Here is his description. Bounty is around 1000 Dubloons", color=0x00ff00)
    embed.add_field(name="Navn", value=user.name, inline=True)
    embed.add_field(name="ID", value=user.id, inline=True)
    embed.add_field(name="Status", value=user.status, inline=True)
    embed.add_field(name="Høyeste rolle", value=user.top_role)
    embed.add_field(name="Ble med", value=user.joined_at)
    embed.set_thumbnail(url=user.avatar_url)
    await bot.say(embed=embed)

编辑:

from itertools import product

def aliases(info):
    return [''.join(item) for item in product(*((c.lower(), c.upper()) for c in info))]

@bot.command(pass_context=True, aliases=aliases('info'))
@commands.has_role("Admin")
async def info(ctx, user: discord.Member):
    embed = discord.Embed(title="{}'s info:".format(user.name), description="Here is his description. Bounty is around 1000 Dubloons", color=0x00ff00)
    embed.add_field(name="Navn", value=user.name, inline=True)
    embed.add_field(name="ID", value=user.id, inline=True)
    embed.add_field(name="Status", value=user.status, inline=True)
    embed.add_field(name="Høyeste rolle", value=user.top_role)
    embed.add_field(name="Ble med", value=user.joined_at)
    embed.set_footer(text="© Solmester123456 // Thomas")
    embed.set_thumbnail(url=user.avatar_url)
    await bot.say(embed=embed)

1 回答

  • 0

    尝试运行 import discord; print(discord.__version__) . 重写分支是1.0,异步分支是0.16 . 我怀疑你在异步分支上 .

    权宜之计解决方案是将每个混合资本化注册为别名,例如

    from itertools import product
    
    def aliases(word):
        variations = (''.join(item) for item in product(*((c.lower(), c.upper()) for c in word)))
        return [item for item in variations if item != word]
        # registering the name of the coroutine as an alias causes it to fail
    
    @bot.command(pass_context=True, aliases=aliases('info'))
    @commands.has_role("Admin")
    async def info(ctx, user: discord.Member):
        ...
    

    编辑:这之前没有工作,因为 product 返回元组而不是字符串,所以我们需要加入它们来制作单词 .

相关问题