Discord Bot:我将如何使这项工作?

时间:2018-02-13 20:03:24

标签: python discord discord.py

我目前有一个python discord bot脚本,它抓取某个硬币价格的API,但是,每当我尝试将它放入'client.change_presence'时,它给我一个“必须是str,而不是一个列出“错误。

我尝试将其转换为字符串,正如您将在我的源代码中看到的那样,但这也不起作用,提出:“TypeError:只能加入Iterable”

import requests
import discord
import asyncio

url = 'https://cryptohub.online/api/market/ticker/PLSR/'
response = requests.get(url)
data = response.json()['BTC_PLSR']

client = discord.Client()

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

    list = data['last']
    price = print('PLSR Price:', data['last'])
    string = ''.join(list)

    await client.change_presence(game=discord.Game(name="PLSR Price: " + string))

@client.event
async def on_message(message):
    if message.content.startswith('!test'):
        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('Removed Token for Security Reasons')

我该如何使这项工作?谢谢! (Python 3.6.4)

1 个答案:

答案 0 :(得分:1)

data['last']是一个浮点数,而不是一些可迭代的字符串。只需使用str(data['last'])

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

    last = str(data['last'])
    price = print('PLSR Price:', last)

    await client.change_presence(game=discord.Game(name="PLSR Price: " + last))