【问题标题】:Make code uncaseSensitive, Python, Discord Bot使代码不区分大小写、Python、Discord Bot
【发布时间】:2019-01-13 09:19:54
【问题描述】:

我怎样才能使这段代码具有caseSensitive?我试过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)

【问题讨论】:

    标签: python bots discord discord.py


    【解决方案1】:

    尝试运行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 返回元组而不是字符串,所以我们需要将它们加入来造词。

    【讨论】:

      猜你喜欢
      • 2020-09-04
      • 1970-01-01
      • 2021-04-12
      • 2021-12-27
      • 2018-05-31
      • 2019-08-12
      • 2018-01-22
      • 2020-03-24
      • 2012-04-04
      相关资源
      最近更新 更多