-3

我正在 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')

但是当我在不和谐上写字*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'

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

4

2 回答 2

0

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))
#                                          ^^^^^^
于 2017-10-09T16:42:32.670 回答
0

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

于 2017-10-09T16:41:26.860 回答