websocket 警告不和谐,逃跑“不和谐,网关:碎片ID无心跳被阻止的时间超过10秒,”

u3r8eeie  于 2022-11-11  发布在  其他
关注(0)|答案(1)|浏览(197)

我试着写一个Discord bot,它可以将消息从一个服务器转发到另一个服务器。但是我收到了一个警告,它会断开我与discord网关的连接。我读到这是可能的,因为有了tyme.slep(),但是如果没有它,连接总是会中断。据我所知,DDOS保护是由于大量的请求而激活的。

  1. import asyncio
  2. import websocket
  3. import json
  4. from threading import Thread
  5. import discord
  6. import requests
  7. from io import BytesIO
  8. import time
  9. from discord.ext import tasks
  10. # Bot
  11. bot_token = "anything"
  12. user_token = "anything"
  13. client = discord.Client(intents=discord.Intents.default())
  14. # Channels list
  15. f = open('channels.txt', 'r')
  16. channels_file = f.read()
  17. channels_array = channels_file.strip().split('\n')
  18. # Connect func
  19. def send_json_request(ws, request):
  20. ws.send(json.dumps(request))
  21. def receive_json_response(ws):
  22. response = ws.recv()
  23. if response:
  24. return json.loads(response)
  25. # WebSoket
  26. def on_closed(ws):
  27. ws.connect("wss://gateway.discord.gg/?v=6&encording=json")
  28. ws = websocket.WebSocket(on_close=on_closed)
  29. ws.connect("wss://gateway.discord.gg/?v=6&encording=json")
  30. def receive_json_response(ws):
  31. response = ws.recv()
  32. if response:
  33. return json.loads(response)
  34. def get_attachment_media(media):
  35. media_array = []
  36. for i in media:
  37. response = requests.get(i['url'])
  38. im = BytesIO(response.content)
  39. print(im)
  40. media_array.append(discord.File(im))
  41. return media_array
  42. def get_channel(id):
  43. for i in channels_array:
  44. if i == id:
  45. return True
  46. return False
  47. # Heartbeat
  48. def heartbeat(interval):
  49. print("Heartbeat begin")
  50. while True:
  51. time.sleep(interval)
  52. heartbeatJSON = {
  53. "op": 1,
  54. "d": "null"
  55. }
  56. send_json_request(ws, heartbeatJSON)
  57. print("Heartbeat sent")
  58. @tasks.loop(seconds=0.5)
  59. async def main():
  60. channel = client.get_channel(anything)
  61. event = receive_json_response(ws)
  62. try:
  63. if event['d']['author']['id'] == 'anything':
  64. return
  65. id_channel = event['d']['channel_id']
  66. id_guild = event['d']['guild_id']
  67. if get_channel(id_channel):
  68. content = event['d']['content']
  69. attachment_media = get_attachment_media(event['d']['attachments'])
  70. await channel.send(content, files=attachment_media)
  71. op_code = event('op')
  72. if op_code == 11:
  73. print('Heartbeat recieved')
  74. except:
  75. pass
  76. @client.event
  77. async def on_ready():
  78. event = receive_json_response(ws)
  79. heartbeat_interval = event['d']['heartbeat_interval'] / 1000
  80. send_json_request(ws, {
  81. "op": 2,
  82. "d": {
  83. "token": user_token,
  84. "properties": {
  85. "$os": 'linux',
  86. "$browser": 'chrome',
  87. "$device": 'pc'
  88. }
  89. }
  90. })
  91. main.start()
  92. asyncio.run(heartbeat(heartbeat_interval))
  93. client.run(bot_token)
w7t8yxp5

w7t8yxp51#

我建议您检查这个答案,并根据您的代码进行调整。
然而,如果你只是想让你的机器人复制一个服务器发送的消息的内容,然后发送到另一个服务器,你可以使用on_message()事件,这是一个更简单的方法。这是整个代码,它也可以防止任何警告(除非机器人试图在短时间内发送太多的消息):

  1. import discord
  2. intents = discord.Intents.default()
  3. intents.message_content = True # You are missing the message_content intent! (needed to read the content of the guild's messages)
  4. client = discord.Client(intents=intents)
  5. TOKEN = "Your Token"
  6. guild_id = 123456789 # The guild you want your bot to send the messages to
  7. channel_id = 987654321 # The channel of the guild you want your bot to send the messages to
  8. guild = None
  9. channel = None
  10. @client.event
  11. async def on_ready():
  12. global guild, channel, guild_id, channel_id
  13. await client.wait_until_ready()
  14. guild = client.get_guild(guild_id)
  15. channel = guild.get_channel(channel_id)
  16. print("Logged")
  17. @client.event
  18. async def on_message(message : discord.Message):
  19. if message.author == client.user: # You don't want to send the own bot messages
  20. return
  21. if message.guild.id == guild_id: # You don't want to send the messages from the own guild your bot is sending the messages to
  22. return
  23. await channel.send(f"{message.author}: {message.content}") # Add attachments if you want
  24. client.run(TOKEN)
展开查看全部

相关问题