【问题标题】:My Discord Bot Returns "AttributeError: 'str' object has no attribute"我的 Discord Bot 返回“AttributeError:‘str’对象没有属性”
【发布时间】:2018-03-20 22:35:03
【问题描述】:

我正在 Python 上构建一个简单的不和谐机器人,以便在输入的每个字符和其他一些东西之间放置空格。

我的代码在这里:

import discord
import asyncio

client = discord.Client()

def bos(a):
    a=a.upper()
    a=a.replace("0", "SIFIR")
    a=a.replace("1", "BİR")
    a=a.replace("2", "İKİ")
    a=a.replace("3", "ÜÇ")
    a=a.replace("4", "DÖRT")
    a=a.replace("5", "BEŞ")
    a=a.replace("6", "ALTI")
    a=a.replace("7", "YEDİ")
    a=a.replace("8", "SEKİZ")
    a=a.replace("9", "DOKUZ")
    a=" ".join(a)

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')

@client.event
async def on_message(message):
    if message.content.startswith('*'):
        a = message.content[1:]
        await client.send_message(message.channel, a.bos(a))
        #counter = 0
        #tmp = await client.send_message(message.channel, 'Calculating messages...')
        #async for log in client.logs_from(message.channel, limit=100):
        #    if log.author == message.author:
        #        counter += 1

        #await client.edit_message(tmp, 'You have {} messages.'.format(counter))
    #elif message.content.startswith('!sleep'):
        #await asyncio.sleep(5)
        #await client.send_message(message.channel, 'Done sleeping')

client.run('token')

但是当我在 discord 上写 *test 时,终端返回:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/client.py", line 307, in _run_event
    yield from getattr(self, event)(*args, **kwargs)
  File "bot2.py", line 32, in on_message
    await client.send_message(message.channel, a.bos(a))
AttributeError: 'str' object has no attribute 'bos'

有人可以帮我修复这个代码吗?

【问题讨论】:

  • a.bos(a) 是什么意思?
  • 它告诉你a 是一个字符串。字符串没有您的函数 bos 作为属性。目前尚不清楚您要使用 a.bos(a) 完成什么
  • 我尝试调用该函数,我事先声明了。但它没有用。

标签: python discord discord.py


【解决方案1】:

bos 不是str 上的方法;这是一个全局范围的函数。而不是a.bos(a),只需使用bos(a)

【讨论】:

    【解决方案2】:

    bos 是一个常规函数,而不是 str 方法。此外,在函数内部分配给a 不会影响调用外部的参数;你需要返回一个新的字符串。

    def bos(a):
        return " ".join(a.upper().
                          replace("0", "SIFIR").
                          replace("1", "BİR").
                          replace("2", "İKİ").
                          replace("3", "ÜÇ").
                          replace("4", "DÖRT").
                          replace("5", "BEŞ").
                          replace("6", "ALTI").
                          replace("7", "YEDİ").
                          replace("8", "SEKİZ").
                          replace("9", "DOKUZ"))
    
    
    ...
    await client.send_message(message.channel, bos(a))
    #                                          ^^^^^^
    

    【讨论】:

    • 感谢您的出色回答。它对我帮助很大。
    猜你喜欢
    • 1970-01-01
    • 2021-11-29
    • 2018-06-15
    • 2021-07-10
    • 2021-08-01
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    相关资源
    最近更新 更多