2017-12-23 21 views
1

まあ、私はPython3を使ってDiscordボットに取り組んでいますが、ある程度は動作しますが、数分ごとにクラッシュし続けます。それは私に "タスクは停止されましたが、保留中です"というエラーが表示されます。今、問題を探して、自分のレスポンス= request.get(url)を取り除き、それを "async with aiohttp.get(url)in response"に置き換えなければならないという情報を見つけました。今私はこれが好きなときに、私に "コルーチンの可用性が待たれていない"ということを与えます。これを解決するには、ある種のループを使用する必要があると思うが、私は非同期のものにはかなり新しいので、手がかりにはならない。Python discord botコルーチンは決して待たれていませんでした

import discord 
from discord.ext.commands import Bot 
from discord.ext import commands 
import asyncio 
import time 

import requests 
from bs4 import BeautifulSoup 
import smtplib 
import aiohttp 
import async_timeout 


async def availability(): 
    url = "some url" 
    headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'} 
    async with aiohttp.ClientSession().get(url, headers=headers) as response: 
     soup = BeautifulSoup(response.text, "lxml") 
     print(soup) 
     return soup 


Client = discord.Client() 
bot_prefix= "?" 
client = commands.Bot(command_prefix=bot_prefix) 


availible = True 


@client.event 
async def on_ready(): 
    print("Bot Online!") 
    print("Name: {}".format(client.user.name)) 
    print("ID: {}".format(client.user.id)) 

    bessie = 0 
    waittime = 0 

    while True: 
     time.sleep(1) 
     if wachttijd == 0: 
      if ("0 available") not in str(availability()): 
       bessie = bessie + 1 
       if bessie == 3: 
        await client.send_message(discord.Object(id='some id'), 
               '<@&some channel>some text!') 
        print("available") 
        bessie = 0 
        waittime = 10 
      else: 
       bessie = 0 
     else: 
      wachttijd = wachttijd - 1 



client.run("token") 

誰かがこれを手伝ってくれますか?

+2

'await availability()'を使う必要があります。 – dirn

答えて

0

私はこのことについて100%確信していませんが、いくつかの研究の後、私がthis stackoverflow threadで見たコードによれば、あなたがresponse.textを待っていないためかもしれません。 response.textの前でのawaitキーワードを追加

てみてください。discord.pyを使用した場合

soup = BeautifulSoup(await response.text(), "lxml") 

また「の代わりにあなたがawait asyncio.sleep(seconds)

を使用し、)(time.sleepは使用しないでくださいボットをフリーズさせる可能性があるため、できる限りブロックしないようにしてください。 "FAQ" section of the discord.py docsで詳細を読むことができます。

関連する問題