我使用套接字,pawn和discord.py的组合在Discord和SAMP服务器之间来回通信。我收到此错误。
Task was destroyed but it is pending!
task: <Task pending coro=<Client._run_event() done, defined at
C:\Users\alexr\AppData\Local\Programs\Python\Python36\lib\site-
packages\discord\client.py:304> wait_for=<Future pending cb=
[BaseSelectorEventLoop._sock_connect_done(1476)(), <TaskWakeupMethWrapper
object at 0x000001437C5B2E28>()]>>
我运行该程序,每当我的代码进入尝试将验证码发送给任何客户ID的阶段时,都会导致上述错误。有关更多信息,请参见'elif full_text [0] ==“代码”:'。
希望您能帮助我解决我的问题,谢谢!
#!/usr/bin/env python3
import socket
import discord
from discord.ext import commands
from random import choice, randint
import string
bot = commands.Bot(command_prefix='+')
token = 'no'
HOST = '127.0.0.1' # Standard loopback interface address (localhost)
PORT = 1700 # Port to listen on (non-privileged ports are > 1023)
def generate_forum():
code = ''
for i in range(0, 16):
if i > 3: code += f'{choice(string.ascii_letters + string.punctuation)}'
else: code += f'{choice(string.ascii_letters)}'
return code
@bot.event
async def on_ready():
print('Bot online.')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
try:
data_encoded = conn.recv(1024)
except ConnectionResetError:
print('Connection closed by the Remote Host, exiting script!')
return exit()
if not data_encoded:
break
data = data_encoded.decode()
text = data.split('|')
text2 = text[2].split('\x00')
full_text = []
full_text.append(text[0])
full_text.append(text[1])
full_text.append(text2[0])
if full_text[0] == "gamemsg":
await bot.send_message(bot.get_channel('526009593877233664'), f'{full_text[1]} says: {full_text[2]}')
elif full_text[0] == "verification":
#await bot.send_message(member, "if this works then ur cool m8 8)")
print(data)
print(full_text)
if len(full_text[2]) != 18:
conn.send(f'{full_text[1]}|invalid|{full_text[2]}'.encode())
if discord.utils.get(bot.get_all_members(), id=full_text[2]) is None:
conn.send(f'{full_text[1]}|invalid|{full_text[2]}'.encode())
else:
conn.send(f'{full_text[1]}|valid|{full_text[2]}'.encode())
elif full_text[0] == "code":
print(f"code called, person's id is {full_text[1]}")
print(f"verification code is: {full_text[2]}")
member = discord.utils.get(bot.get_all_members(), id=full_text[1])
await bot.send_message(member, f'Your verification code is: {full_text[2]}')
elif full_text[0] == "verified":
member = discord.utils.get(bot.get_all_members(), id=full_text[2])
role = discord.utils.get(bot.get_server("523827814131171349").roles, name='Verified')
await bot.add_roles(member, role)
await bot.send_message(member, f'Hey {full_text[1]}! You are now verified.')
elif full_text[0] == "checkverified":
print('Checking if verified...')
member = discord.utils.get(bot.get_all_members(), id=full_text[2])
rolefound = False
for role in member.roles:
if role.name == "Verified":
rolefound = True
if rolefound == False: conn.send(f'checkverified|no|{full_text[2]}'.encode()), print('Not verified, sending msg back')
else: conn.send(f'checkverified|yes|{full_text[2]}'.encode()), print('Verified, sending msg back')
bot.run(token)