【问题标题】:How would I check if my bots message is <2000 char length我将如何检查我的机器人消息是否 <2000 字符长度
【发布时间】:2021-08-13 03:14:12
【问题描述】:
    @commands.command()
    async def emojify(self, ctx, *, text):
        emojis = []
        for s in text.lower():
            if s.isdecimal():
                num2emo = {'0':'zero', '1':'one', '2':'two',
                        '3':'three', '4':'four', '5':'five', 
                        '6':'six', '7':'seven', '8':'eight',
                        '9':'nine'}
                emojis.append(f':{num2emo.get(s)}:')
            elif s.isalpha():
                emojis.append(f':regional_indicator_{s}:')
            else:
                emojis.append(s)

        await ctx.send(''.join(emojis))

此命令将单词变成不和谐的表情符号(如 dank memer emojify)

如何让它在发送前检查消息字符长度的长度,以便它可以打印错误或继续?

【问题讨论】:

  • 存储''.join返回值并检查它的长度

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


【解决方案1】:

将字符串保存在变量中,并使用len()函数获取字符数

@commands.command()
async def emojify(self, ctx, *, text):
    emojis = []
    for s in text.lower():
        if s.isdecimal():
            num2emo = {'0':'zero', '1':'one', '2':'two',
                    '3':'three', '4':'four', '5':'five', 
                    '6':'six', '7':'seven', '8':'eight',
                    '9':'nine'}
            emojis.append(f':{num2emo.get(s)}:')
        elif s.isalpha():
            emojis.append(f':regional_indicator_{s}:')
        else:
            emojis.append(s)

    messageToSend = ''.join(emojis)
    if len(messageToSend) < 2000:
        await ctx.send(messageToSend)
    else:
        # error-handling

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-03
    • 2021-01-24
    • 2019-04-24
    • 2018-09-14
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多