【发布时间】:2018-07-24 17:31:10
【问题描述】:
我很好奇如何每 10 秒循环一次我的代码的这一部分。
我将如何做到这一点,而不是 Discord API 在 !price 执行时私下向您发送价格,而不是通常在频道中发送消息?
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("PULSAR 4 LIFE")
print('------')
price = print('Price:', data['last'])
pulsar = float(data['last'])
pulsarx = "{:.9f}".format(pulsar)
await client.change_presence(game=discord.Game(name="PLSR Price: " + pulsarx))
@client.event
async def on_message(message):
if message.content.startswith('!price'):
url = 'https://cryptohub.online/api/market/ticker/PLSR/'
response = requests.get(url)
data = response.json()['BTC_PLSR']
price = print('PLSR Price:', data['last'])
pulsar = float(data['last'])
pulsarx = "{:.9f}".format(pulsar)
await client.send_message(message.channel, 'Price of PULSAR: ' + pulsarx)
【问题讨论】:
-
你想循环哪一部分?
-
我想在它从 API URL 请求信息的地方循环。所以基本上信息每隔一段时间就会更新一次。
-
基本上每 10 秒发送一次用户的 dms 垃圾邮件? o.o
-
哦,没有。它显示在右上角的“正在播放”状态下。它不会向任何人发送垃圾邮件,我只是想要它,所以当他们输入 !price 时,它会将价格发送给他们,而不是在公共聊天中。我希望它反复循环,以便它不断准确地更新价格。
标签: python python-3.x discord