当前位置: 首页 > 知识库问答 >
问题:

无法建立新连接Discord.py

时浩波
2023-03-14

你好在我的代码它打破了我的请求,我尝试了几次,但1-2小时后bot状态不再改变。

我在ftp服务器上托管这些文件。

import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
import random
from discord import Game
import math, time
import requests

Client = discord.client
client = commands.Bot(command_prefix = '!')
Clientdiscord = discord.Client()

@client.event
async def on_ready():

client.loop.create_task(scheduler())
print('project ready')

async def scheduler():
while True:
    # sleep until the next whole second
    now = time.time()
    await asyncio.sleep(math.ceil(now) - now)

    await client.change_presence(game=discord.Game(name='steady', type=3))
    await asyncio.sleep(5)
    await client.change_presence(game=discord.Game(name='ready', type=3))
    await asyncio.sleep(5)
    await client.change_presence(game=discord.Game(name='go', type=3))
    await asyncio.sleep(5)
    await client.change_presence(game=discord.Game(name='Total Used : '+requests.get('http://username.mydomain/project/total_visit/count.txt').text, type=3))
    await asyncio.sleep(10)
    await client.change_presence(game=discord.Game(name='Online : '+requests.get('http://username.mydomain/project/total_online/Live.php').text, type=3))
    await asyncio.sleep(10)
    await client.change_presence(game=discord.Game(name='Total Users : '+requests.get('http://username.mydomain/project/total_users/total.php').text, type=3))
    await asyncio.sleep(10)
    await client.change_presence(game=discord.Game(name='return again', type=3))

    #i tired to kill all request to start clear again.
    client.logout()
    client.close()
    await asyncio.sleep(20)


    client.run("client_token")

以后从未检索到任务异常:异常=ConnectionError(MaxRetryError(“HTTPConnectionPool(host='username.mydomain',port=80”):url超过最大重试次数:/project/total_visit/count.txt(由NewConnectionError引起)(':未能建立新连接:[WinError 10061]无法建立连接,因为目标计算机主动拒绝了它“,)”,),)

在处理上述异常期间,发生了另一个异常:

回溯(最后一次调用):文件“C:\Users\Administrator\AppData\Local\Programs\Python 36-32\lib\site packages\urllib3\connectionpool.py”,urlopen chunked=chunked中的第600行)文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python 36-32\lib\site packages\urllib3\connectionpool.py”,第354行,在“发出请求”(方法,url,**httplib\u请求\u kw)文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\http\client.py”,第1239行,在请求self中。发送请求(方法,url,正文,头,编码块)文件“C:\Users\Administrator\AppData\Local\Programs\Python36-32\lib\http\client.py”,第1285行,在发送请求self.endheaders中(正文,encode\u chunked=encode\u chunked)文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\http\client.py”,第1234行,在endheaders self中。发送输出(消息正文,encode\u chunked=encode\u chunked)文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\http\client.py”,第1026行,在发送输出self.send中(msg)文件“C:\Users\Administrator\AppData\Local\Programs\Python36-32\lib\http\client.py”,第964行,在发送self.connect()文件“C:\Users\Administrator\AppData\Local\Programs\Python36-32\lib\site packages\urllib3\connection.py”中,第181行,在connect conn=self.\u new\u conn()文件中“C:\Users\Administrator\AppData\Local\Programs\Python36-32\lib\site packages\urllib3\connection.py”在“新建连接”中的第168行未能建立新连接:%s“%e)urllib3.exceptions.NewConnectionError::未能建立新连接:[WinError 10061]无法建立连接,因为目标计算机主动拒绝连接

在处理上述异常期间,发生了另一个异常:

回溯(最近一次调用):文件“C:\Users\Administrator\AppData\Local\Programs\Python 36-32\lib\site packages\requests\adapters.py”,第449行,在发送超时=超时文件“C:\Users\Administrator\AppData\Local\Programs\Python 36-32\lib\site packages\urllib3\connectionpool.py”中,urlopen\u stacktrace=sys.exc\u info()[2]第638行)文件“C:\Users\Administrator\AppData\Local\Programs\Python 36-32\lib\site packages\urllib3\util\retry.py”,第398行,增量raise MaxRetryError(_pool,url,error或ResponseError(cause))urllib3.exceptions.MaxRetryError:HTTPConnectionPool(host='username.mydomain',port=80):url:/project/total_visit/count.txt超过了最大重试次数(由NewConnectionError引起(':未能建立新连接:[WinError 10061]无法建立连接,因为目标计算机主动拒绝了它',))

在处理上述异常期间,发生了另一个异常:

回溯(最近一次调用):文件“C:\Users\Administrator\Desktop\bot.py”,第33行,在调度程序等待客户端。更改\u状态(game=discord.game(name='Total Used:'requests.get')http://username.mydomain/project/total_visit/count.txt').text,type=3)文件“C:\Users\Administrator\AppData\Local\Programs\Python36-32\lib\site packages\requests\api.py”,第75行,在get返回请求('get',url,params=params,**kwargs)文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site packages\requests\api.py”,第60行,在请求返回会话.request中(method=method,url=url,**kwargs)文件“C:\Users\Administrator\AppData\Local\Programs\Python36-32\lib\site packages\requests\sessions.py”,第533行,在request resp=self.send(prep,**send\u kwargs)文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site packages\requests\sessions\sessions.py“,第646行,在send r=adapter.send(request,**kwargs)文件“C:\Users\Administrator\AppData\Local\Programs\Python36-32\lib\site packages\requests\adapters.py”中,第516行,在send raise ConnectionError(e,request=request)requests.exceptions.ConnectionError:HTTPConnectionPool(host='username.mydomain',=80):url:/project/total_visit/count.txt超过了最大重试次数(由NewConnectionError引起(':未能建立新连接:[WinError 10061]无法建立连接,因为目标计算机主动拒绝了它',))

共有1个答案

田冥夜
2023-03-14

肯定你必须得到错误,,你做了很多get.requests.

尝试(关闭请求)/(重启Bot[后台命令])

嗯,我还不够好,但你可以试试我的舞步

1)添加完整(urllib3库)

import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
import random
from discord import Game
import math, time
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
import urllib3
import urllib3.contrib.pyopenssl
import certifi

2) add(Backgroundworker)命令[每次都将重新启动get.request。]

Client = discord.client
client = commands.Bot(command_prefix = '!')
Clientdiscord = discord.Client()

urllib3.contrib.pyopenssl.inject_into_urllib3()

@client.event
async def on_ready():
print('Project Runing..!')


async def task():
await client.wait_until_ready()
while True:

   await client.change_presence(game=discord.Game(name='Loading..!', type=3))

   print('Get_Url-Refresh')
   await asyncio.sleep(1)
   print('Total_Used')
   one = requests.get('http://username.mydomain/project/total_visit/count.txt', timeout=5)
   print('Total_Online')
   two = requests.get('http://username.mydomain/project/total_online/Live.php', timeout=5)
   print('Total_Users')
   three = requests.get('http://username.mydomain/project/total_users/total.php', timeout=5)
   await asyncio.sleep(1)
   print('Refresh Request')

   await asyncio.sleep(10)



   print('Running')
   await client.change_presence(game=discord.Game(name='steady', type=3))
   await asyncio.sleep(5)
   await client.change_presence(game=discord.Game(name='ready', type=3))
   await asyncio.sleep(5)
   await client.change_presence(game=discord.Game(name='go', type=3))
   await asyncio.sleep(5)
   print('Total Used..!')
   await client.change_presence(game=discord.Game(name='Total Used : ' + one.text, type=3))
   await asyncio.sleep(5)
   print('Online..!')
   await client.change_presence(game=discord.Game(name='Online : ' + two.text, type=1))
   await asyncio.sleep(5)
   print('Total Users..!')
   await client.change_presence(game=discord.Game(name='Total Users : ' + three.text, type=3))
   await asyncio.sleep(5)
   print('Refresh..!')

   def handle_exit():
print("Handling")
client.loop.run_until_complete(client.logout())
for t in asyncio.Task.all_tasks(loop=client.loop):
    if t.done():
        t.exception()
        continue
    t.cancel()
    try:
        client.loop.run_until_complete(asyncio.wait_for(t, 5, loop=client.loop))
        t.exception()
    except asyncio.InvalidStateError:
        pass
    except asyncio.TimeoutError:
        pass
    except asyncio.CancelledError:
        pass


   while True:
   @client.event
   async def on_message(m):
    if m.content == 'die':
        print("Terminating")
        raise SystemExit

client.loop.create_task(task())
try:
    client.loop.run_until_complete(client.start('You_Token_Here'))
except SystemExit:
    handle_exit()
except KeyboardInterrupt:
    handle_exit()
    client.loop.close()
    print("Program ended")
    break

print("Bot restarting")
client = discord.Client(loop=client.loop)

归功于开发者I.A(玩家控制)。

 类似资料:
  • 问题内容: 我正在学习Android编程,现在已经尝试了几天。我正在编写应该连接到XMPP服务器的Android应用。我一直都在犯同样的错误,并且真的不知道我在做什么错。我已经尝试过使用Google找到的示例代码,但也无法与它们建立连接。 } 这是我得到的错误: 我还检查了防火墙是否允许连接,并尝试连接到hosted.im服务而不是家庭服务器,结果相同。 问题答案: 解决方案是将连接代码放入单独的

  • 问题内容: 我正在尝试从MS AD中检索一些信息:特定分支机构的成员,部门名称,职位等。 我使用了很多示例,包括Apache Directory LDAP API 和UnboundID,但是我无法与AD建立连接。 RDN: 为了进行搜索,我使用以下过滤器: 当我使用中的数据创建目录上下文时,出现异常: 如果未指定密码,则会出现以下异常: 我已验证我的帐户未锁定。 根据常见的活动目录LDAP绑定错误

  • 问题内容: 我通过添加laravel安装了redis , 然后为了测试,我添加了以下代码: 在app / config / database.php中,我有: 它引发以下错误: 我用于该项目。使用。 问题答案: 首先,通过打开powershell并键入(确保可能需要在功能中启用此命令或以某种方式安装此命令),以确保Redis实际上在该端口上进行侦听。如果正在侦听,请检查防火墙设置。如果不监听,请确

  • 我在docker中运行了elasticsearch 8.1,其中包含以下docker compose文件: 我正在尝试使用org.elasticsearch.client.RestClient向es集群发出一个简单的GET请求。 请求: Rest客户端初始化: 主要方法: buildClusterHosts()方法正确地构建了一个HttpHost数组(在本例中只有一个),并将其提供给rest客户端

  • 我正在遵循下一个教程:https://docs.microsoft.com/en-us/azure/azure-functions/functions-twitter-email.我在Chrome连接到推特时遇到了一些问题,切换到边缘,并能够进行连接。 然而,对于本教程前面创建的情绪分析,我在两种浏览器中都不断收到消息 请检查您的帐户信息和/或权限,然后重试。 首先,我认为服务需要一段时间才能启动

  • 我正在尝试在服务器上实现基于GraphQLWebSocket的@订阅(使用NestJS@subscription)。服务器托管在AWS ECS上,位于ALB后面。我们目前通过VPC链接与ALB建立了AWS API GW连接。 我试图用我们在HTTP API GW中使用的相同的VPC链接构建一个专用的Websocket API GW。我还试图在我们的ECS上安装一个新的NLB(网络负载平衡器),并在