【问题标题】:Discord Bot: How would I make this work?Discord Bot:我将如何进行这项工作?
【发布时间】:2018-07-24 06:58:56
【问题描述】:

我目前有一个 python discord 机器人脚本,它可以获取某个硬币价格的 API,但是,每当我尝试将其放入“client.change_presence”时,它都会给我一个“必须是 str,而不是列表”错误。

我尝试将其转换为字符串,正如您将在我的源代码中看到的那样,但这也不起作用,提出:“TypeError: Can Only Join An 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)

【问题讨论】:

  • 在您的问题中以正确的格式发布您的代码,以便人们无需访问第三方站点即可查看,并且仍然可以复制和粘贴
  • PSLR 行打印是什么?
  • 一切正常,我只想将 Discord 状态设置为:PLSR 价格:+ API 的价格。
  • 您还应该知道使用requests 和异步代码can cause problems。你应该使用aiohttp 来避免不必要的阻塞。
  • name="PLSR 价格:{}".format(last)

标签: python discord discord.py


【解决方案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))

【讨论】:

    猜你喜欢
    • 2019-02-04
    • 2020-11-18
    • 2022-10-17
    • 2020-08-24
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 2020-11-15
    • 2018-02-22
    相关资源
    最近更新 更多